Thursday, June 4, 2009

Create a list programatically in sharepoint

Hi friends
Here i will show u how to create list of type Custom List.

SPWeb web = SPContext.Current.Web; //Get current site
string listName = "My New List";

//We r purposely putting it in try/catch block because if list not present in the site then code will throw an exception not return null.
SPList oldList = web.Lists[listName]; // Check if list is exist
catch (ArgumentException ex)
web.AllowUnsafeUpdates = true;
// Create a list
Guid listGuid = web.Lists.Add(listName, "This is my first custom list.", SPListTemplateType.GenericList);

// Set list properties
SPList list = web.Lists[listGuid];
list.OnQuickLaunch = true;

web.AllowUnsafeUpdates = true;

You can create any type of list by changing list template (i.e- SPListTemplateType.GenericList ).

1 comment: