Creating Lists and updating the default view programatically in SharePoint 2007

Recently a question came across a distribution list that I am a member of about updating the default view through code in SharePoint 2007.  Whenever I answer a question like this I try to remember it and blog about it.  Hopefully the next time someone is looking for the answer they will search and find this blog entry!  So here is a small code sample that creates a list and updates the “All Items” view through code.


private const string MultiLanguageHelloListName = “Multi-Language Hello”;
SPSite currentSite = properties.Feature.Parent as SPSite;
SPWeb currentWeb = currentSite.RootWeb;
uint language = currentWeb != null ? currentWeb.Language = 1033;
SPList list = null;

if (!ListExists(currentSite.RootWeb, MultiLanguageHelloListName))
Guid listId = currentSite.RootWeb.Lists.Add(MultiLanguageHelloListName,
SPUtility.GetLocalizedString(“$Resources:hello,HelloSettingsDesc;”, “hello”, language),

list = currentSite.RootWeb.Lists[listId];

list.Fields.Add(“ToolTip”, SPFieldType.Text, true);
SPField tooltip = list.Fields[“ToolTip”];
tooltip.ShowInDisplayForm = true;
tooltip.ShowInEditForm = true;
tooltip.ShowInListSettings = true;
tooltip.ShowInNewForm = true;
tooltip.ShowInViewForms = true;

SPView defaultView = list.Views[“All Items”];

list.ContentTypesEnabled = false;
list.OnQuickLaunch = true;
list.EnableAttachments = false;
list.EnableVersioning = false;
list.NoCrawl = true;


The first section of this piece of code verifies that the list doesn’t exist, because this piece of code was from a feature receiver in the FeatureActivated event.  Next I create the list using currentSite.RootWeb.Lists.Add(<listname>, <list description>, SPListTemplateType).  After the creation of the list I go on to add a field called “ToolTip” to the list.  I then want to show this field in the deafult view so I get a reference to the “All Items” view and add it to the ViewFields collection.  Then update the view and update the list and your done!


Hope this is useful.

Comments (4)

  1. Claire says:


    That worked a treat

  2. kanid says:

    Can i hide create view menu after update default view ?

  3. Denny says:

    I have a list that has multiple views. When you are in another view besides the default view – and you edit a record, when you save, it always jumps you back to the default view and you have to reselect the other view again if you want to continue editing records etc.

    Is there a way to have it go back to the view you were editing the record in rather than always jumping back to the default view? I know I can create a seperate list, but that is more maintenance prone if changes have to be made. I would rather maintain on large "shared" list with multiple views, but the above sitaution has my userbase asking to stay in their sort rather than always jumping back to the default after editing a record.

    Is there a way to do this?

  4. Denny says:

    sorry – didn't leave my email address on the last post. I'm concerned about finding this again :-) – could you email me with answers?

    my email address is