SharePoint - Client Side Object Model(CSOM) - Managed - Create a List using Generic List Template
Problem Definition
Create a list form Generic Template using Managed Code and Client Side Object Model(CSOM) for SharePoint 2013.
Prerequisites
Code Example
/// <summary>
/// Create a List from Generic List Template
/// </summary>
/// <param name="title">Title of the List</param>
/// <param name="desccription">Description about the list</param>
public static void CreateAList(string title,string desccription)
{
try
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var web = spContext.Web;
// Set up a list creation information object
var lci = new ListCreationInformation();
// Set title to the list
lci.Title = title;
// Set description to the list
lci.Description = desccription;
// from MSDN:
// Off - Do not display a link to the list in the Quick Launch. Value = 0.
// On - Display a link to the list in the Quick Launch.Value = 1.
// Default - Display a link to the list in the Quick Launch if the OnQuickLaunch attibute
// of the ListTemplate element in the Feature's element XML file equals TRUE. Value = 2.
lci.QuickLaunchOption = QuickLaunchOptions.On;
// from MSDN:
// GenericList - Custom list. The value = 100.
lci.TemplateType = (int)ListTemplateType.GenericList;
// Add list to your web
var list = web.Lists.Add(lci);
// Execute the query to finalize your add
spContext.ExecuteQuery();
// Display a success message
Console.WriteLine("New List Created Successfully!");
}
}
catch(Exception ex)
{
// Print error message
Console.WriteLine(ex.ToString());
}
}
Comments
Post a Comment