Raghu's SharePoint Corner: How To Create List Programmatically using Custom List Template (OOB)

My blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
http://www.sharepointcolumn.com
and update your bookmarks.

September 15, 2008

How To Create List Programmatically using Custom List Template (OOB)

How To Create List Programmatically using Custom List Template (OOB)

There are many ways to create a list as follows
1. Creating list Using OOB UI.
2. Creating list Using List Templates
3. Creating list Using Features
4. Creating list programmatically or by a code behind approach

Programmatically a list can be created by writing a console application or by creating exe. The advantage of creating list programmatically will also help you tp retain lookup column in the same list or some other list. I will be discussing regarding lookup column in my next article
This article is written to create a list using out of the box (OOB) custom list template using a code behind approach.The snippet belows shows how to create a list.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("siteurl”))
{
SPWeb web = site.OpenWeb();

web.AllowUnsafeUpdates = true;
Console.WriteLine("Creating Menu List..");
Guid listGuid = web.Lists.Add("ListName”, "Menu Contents For Intranet", SPListTemplateType.GenericList);
}
});
Console.WriteLine("List Created...");

The SPListTemplateType has all the OOB cusotm list template such has Announcements, Tasks, Comments etc. The GenericList is a custom list template which I have used in my code

1 comment:

Unknown said...

dont forget to add web.AllowUnSafeUpdate = false;