Pages

Search This Blog

Monday, December 19, 2011

Using exception handling scope to handle errors in client object model : SharePoint 2010

While using client object model in SharePoint 2010, many a times we come across situations where in we call the
ctx.ExecuteQuery() method and some error occurs on the server due to which we get a 'ServerException' which does not give us the opportunity to make corrections in the operation that we are trying to do.

The ExceptionHandlingScope class models a try/catch behaviour for such situations.When we add the code within this scope,it is processed as if its present in a try/catch and gives the opportunity to correct the error in the catch/finally block.

For eg. lets take an example where in we are trying to update the description of a list using managed client object model.If the list does not exist in the target site, it will throw an exception.Now, in the catch block of the exception handling scope , we can create the list in the site and then update its description.

The code below demonstrates this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;

namespace Sharepoint2010TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {

           using (ClientContext ctx = new ClientContext("http://br-pc-341:2222"))
            {
                //Set up error handling
                ExceptionHandlingScope xScope = new ExceptionHandlingScope(ctx);

                using (xScope.StartScope())
                 {
                    using (xScope.StartTry())
                    {
                        //Try to update the description of a list named "Test List"
                        List testList = ctx.Web.Lists.GetByTitle("Test List");
                        testList.Description = "Test List Description";
                        testList.Update();
                    }
                    using (xScope.StartCatch())
                    {
                        //Fails if the list "Test List" does not exist
                        //So, we'll create a new list
                        ListCreationInformation testListCI = new ListCreationInformation();
                        testListCI.Title = "Test List";
                        testListCI.TemplateType = (int)ListTemplateType.GenericList;
                        testListCI.QuickLaunchOption = Microsoft.SharePoint.Client.QuickLaunchOptions.On;
                        List list = ctx.Web.Lists.Add(testListCI);
                    }
                    using (xScope.StartFinally())
                    {
                        //Try to update the list now if it failed originally
                        List testList = ctx.Web.Lists.GetByTitle("Test List");
                        if (testList.Description.Length == 0)
                        {
                            testList.Description = "Test List Description";
                            testList.Update();
                        }
                    }
                }
                //Execute the entire try-catch as a batch!
                ctx.ExecuteQuery();

                Console.WriteLine("Description Updated !!");
            }

        }
    }
}


This console application updates the description of the list named 'Test List'. If the list does not exist , the list is
created in the catch block and then the description is updated.



The list with the updated description can be seen on the sharepoint site:



No comments:

Post a Comment