Skip to main content

Cloud

Creating (provisioning) a list instance in SharePoint programmatically using the object model

Provisioning a list instance in the object model is so much better than doing it using CAML.

In CAML you must copy a schema.xml file (hundreds of lines of code) from an existing list (say, from the CustomList feature) and then, without understanding the entire code, make changes to it. I don’t know about you, but it does not “sit well” with me when I have to modify and use code in my project that I don’t understand completelty (unless, of course, it is a “black box”, like a thrid party class library).

Additionally, you must duplicate the definitions of all the site columns that you already defined in the content types that go into your list. Good luck if you accidentaly create a discrepancy! Quite frankly, I don’t understand why Microsoft did it like that. As a friend of mine (a good software developer) used to say in situations like that: “A man with two watches never knows what time it is.” 🙂

Provisioning a list through an object model is beautifully simple compared to CAML, plus you don’t have to redefine columns already defined in the content types — the object model takes care of that for you. Use the code below as an example.

If you still need to do it through CAML (which is like pulling teeth, as far as I am concerned), I found this blog to be very helpful: http://ari.provoke.co.nz/archive/2007/04/18/creating-a-custom-sharepoint-2007-list-definition.aspx.

public static SPList CreateList(SPWeb web, string listName, string listDescription, bool onQuickLaunch, bool enableVersioning, List<SPContentType> contentTypes, bool createDafultView)

{

//now let’s instantiate the list and configure it

Guid listId = web.Lists.Add(listName, listDescription, SPListTemplateType.GenericList);

SPList list = web.Lists[listId];

list.ContentTypesEnabled = true;

list.EnableAttachments = false;

list.OnQuickLaunch = onQuickLaunch;

list.EnableVersioning = enableVersioning;

list.Update();

//add support for content type and save

foreach (SPContentType contentType in contentTypes) list.ContentTypes.Add(contentType);

list.Update();

//remove standard Item content type, if can’t find, ignore exception

try { list.ContentTypes["Item"].Delete(); }

catch (ArgumentException) { }

//create a default view consisting of all of the columns

if (createDafultView == true)

{

//let’s add a column we just added to the list to the view

SPView view = list.Views["All Items"];

//view.ViewFields.Delete("LinkTitle"); //delete title that is alredy there

foreach (SPContentType contentType in contentTypes)

{

foreach (SPFieldLink column in contentType.FieldLinks)

{

if (column.Name != "ContentType" && column.Name != "Title") view.ViewFields.Add(column.Name);

}

}

view.Update();

}

web.Update();

return list;

}

The only thing is that, if you changed the name of the “Title” column in your content type, it would revert it to “Title” since you start with a generic list which already contains “Title”. If you want to keep the changed name, the following simple code will take care of it for you:

public static void RenameTitle(SPWeb web, SPList list, string newName)

{

SPField titleField = list.Fields["Title"];

titleField.Title = newName;

titleField.Update();

web.Update();

}

As always, if you see any mistakes or inaccuracies in this blog, please let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.