Sep 19, 2009

Programmatically create a generic list

Here we are creating a list programmatically in the sharepointe site and setting some of the default properties.

Here is sample code:

SPSite site = new SPSite("Site Url");

SPWeb oSubWeb = site.OpenWeb();

oSubWeb.AllowUnsafeUpdates = true;

//Here we are getting the list template type.If we want to create list of some other type,select some other template type.

SPListTemplateType templateType = SPListTemplateType.GenericList;

Guid listId = oSubWeb.Lists.Add("Sample List", "This is a sample List", templateType);

//Creating custom fields(coloumn) in the list

documentLibrary.Fields.Add("coloumn1", SPFieldType.Text, false);

documentLibrary.Fields.Add("coloumn2", SPFieldType.Number, false);

SPList list = oSubWeb.Lists[listId];

list.EnableVersioning = true;

list.OnQuickLaunch = true;

list.Update();

This way we can create a document library programmatically having custom columns for which quicklaunch ,Versioning enabled.

Enjoy!!!

No comments:

Post a Comment