SharePoint 2013 client library code with exception catching

Use an exception handling scope to catch exceptions

This example shows how to create and use an exception handling scope with an ExceptionHandlingScope object. The scenario is to update the description of a list and also enable folder creation. There is a possibility that the list might not exist.

// Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl"); ExceptionHandlingScope scope = new ExceptionHandlingScope(context); using (scope.StartScope()) { using (scope.StartTry()) { List fooList = context.Web.Lists.GetByTitle("Sample"); fooList.Description = "In Try Block"; fooList.Update(); } using (scope.StartCatch()) { // Assume that if there's an exception, // it can be only because there was no "Sample" list. ListCreationInformation listCreateInfo = new ListCreationInformation(); listCreateInfo.Title = "Sample"; listCreateInfo.Description = "In Catch Block"; listCreateInfo.TemplateType = (int)ListTemplateType.Announcements; List fooList = context.Web.Lists.Add(listCreateInfo); } using (scope.StartFinally()) { List fooList = context.Web.Lists.GetByTitle("Sample"); fooList.EnableFolderCreation = true; fooList.Update(); } } context.ExecuteQuery();

To view or add a comment, sign in

More articles by Ramesh N.

Explore content categories