SharePoint - Client Side Object Model(CSOM) - Managed - Create a List using Generic List Template - With ExceptionHandlingScope Implementation
Problem Definition
Create a list form Generic Template using Managed Code and Client Side Object Model(CSOM) for SharePoint 2013. What will happen if there is an error say list already exists.
Couple of ways to get around that:
Couple of ways to get around that:
- Check if list already exists and do not attempt to create the list. There is a flaw that can be corrected; checking the list and then creating the list will be two server calls and this can be corrected using the second approach.
- This approach is using ExceptionHandlingScope, this is very helpful because this works exactly like "try catch finally". When your request enters the scope your request is already on the server and rest of the code will execute on the server as one request.
Prerequisites
Code Example
/// Create a List from Generic List Template with ExceptionScope
/// </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;
// Define Exception scope for the site context.
var exScope = new ExceptionHandlingScope(spContext);
//Create list object
List list;
//Start Exception Scope
using (exScope.StartScope())
{
//Try Scope - Will Execute always
using (exScope.StartTry())
{
list = web.Lists.GetByTitle(title);
spContext.Load(list);
}
//Catch Scope - Will Execute only if there is an error; in this case will be that List DOES NOT exists.
using (exScope.StartCatch())
{
// 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
list = web.Lists.Add(lci);
}
//Finally Scope - Will Execute always
using (exScope.StartFinally())
{
list = web.Lists.GetByTitle(title);
spContext.Load(list);
}
}
// Execute the query to finalize your add
spContext.ExecuteQuery();
//Check if scope has an exception; in case of an exception a list was created and loaded otherwise.
var oprPerformed = exScope.HasException ? ($"{title} Created Successfully!!!") : ($"{title} Loaded Successfully...");
// Display a success message
Console.WriteLine(oprPerformed);
}
}
catch(Exception ex)
{
// Print error message
Console.WriteLine(ex.ToString());
}
}
Code Result
Results if list does not exists:
Results if list already exists:
Comments
Post a Comment