When
we perform the operations on client side, we need to make a require to
execute some operations that depend on the presence of an exception or
not. We all have used try and catch blocks so far.
Lets take an example to understand: if you want to access a specific list in the SharePoint 2010 site in order to modify its property to allow Folder creation and versioning.
public void AllowFolderAndVersionCreation(string listName)
{
ClientContext context = new ClientContext("http://anmol-pc");
using (context)
{
List myList = null;
try
{
myList = context.Web.Lists.GetByTitle(listName);
context.Load(myList);
context.ExecuteQuery();
}
catch
{
ListCreationInformation info = new ListCreationInformation();
info.Title = listName;
info.TemplateType = (int)ListTemplateType.GenericList;
myList = context.Web.Lists.Add(info);
context.ExecuteQuery();
}
finally
{
myList.EnableVersioning = true;
myList.EnableFolderCreation = true;
myList.Update();
context.ExecuteQuery();
}
}
}
Issue with Try and Catch Block
If you see the above i am first trying to get a list by Title in try block, and if list not found in catch block i tried to create a new list and Then in finally block i updated "EnableFolderCreation", "EnableVersioning" properties of list. To acheive this I require to send three commands to the server. So in SharePoint 2010 Client Object Model it is not convenient to use the try and catch blocks in the operations. The reason why is very easy to understand: we are in a client-side context and we have to pay attention to the number of queries to the server in order to execute our operations.
How ExceptionHandlingScope comes to rescue?
SharePoint 2010 Client object Model provides a class ExceptionHandlingScope for exception management. It exposes basic four methods StartScope(), StartTry(), StartCatch() and StartFinally().
Exception Handling scope begins with Startscope() and other three as three difrent sections inside
public void AllowFolderAndVersionCreation(string listName)
{
var context = new ClientContext("http://anmol-pc");
var _ExceptionHandlingScope = new ExceptionHandlingScope(context);
using (_ExceptionHandlingScope.StartScope())
{
using (_ExceptionHandlingScope.StartTry())
{
// the code you want to try
}
using (_ExceptionHandlingScope.StartCatch())
{
//code to run on error
}
using (_ExceptionHandlingScope.StartFinally())
{
// the code that will execute finally
}
}
}
lets place our example code inside this structure
public static void AllowFolderAndVersionCreation(string listName)
{
var context = new ClientContext("http://anmol-pc");
var _ExceptionHandlingScope = new ExceptionHandlingScope(context);
List myList = null;
using (_ExceptionHandlingScope.StartScope())
{
using (_ExceptionHandlingScope.StartTry())
{
myList = context.Web.Lists.GetByTitle(listName);
context.Load(myList);
// context.ExecuteQuery() not required here
}
using (_ExceptionHandlingScope.StartCatch())
{
ListCreationInformation info = new ListCreationInformation();
info.Title = listName;
info.TemplateType = (int)ListTemplateType.GenericList;
myList = context.Web.Lists.Add(info);
// context.ExecuteQuery() not required here
}
using (_ExceptionHandlingScope.StartFinally())
{
myList.EnableVersioning = true;
myList.EnableFolderCreation = true;
myList.Update();
// context.ExecuteQuery() not required here
}
}
context.ExecuteQuery();
}
So you can notice that same thing can be acheived using ExceptionHandlingScope using less calls to the server.
Use of the "Try and catch" block:
Lets take an example to understand: if you want to access a specific list in the SharePoint 2010 site in order to modify its property to allow Folder creation and versioning.
public void AllowFolderAndVersionCreation(string listName)
{
ClientContext context = new ClientContext("http://anmol-pc");
using (context)
{
List myList = null;
try
{
myList = context.Web.Lists.GetByTitle(listName);
context.Load(myList);
context.ExecuteQuery();
}
catch
{
ListCreationInformation info = new ListCreationInformation();
info.Title = listName;
info.TemplateType = (int)ListTemplateType.GenericList;
myList = context.Web.Lists.Add(info);
context.ExecuteQuery();
}
finally
{
myList.EnableVersioning = true;
myList.EnableFolderCreation = true;
myList.Update();
context.ExecuteQuery();
}
}
}
Issue with Try and Catch Block
If you see the above i am first trying to get a list by Title in try block, and if list not found in catch block i tried to create a new list and Then in finally block i updated "EnableFolderCreation", "EnableVersioning" properties of list. To acheive this I require to send three commands to the server. So in SharePoint 2010 Client Object Model it is not convenient to use the try and catch blocks in the operations. The reason why is very easy to understand: we are in a client-side context and we have to pay attention to the number of queries to the server in order to execute our operations.
How ExceptionHandlingScope comes to rescue?
SharePoint 2010 Client object Model provides a class ExceptionHandlingScope for exception management. It exposes basic four methods StartScope(), StartTry(), StartCatch() and StartFinally().
Exception Handling scope begins with Startscope() and other three as three difrent sections inside
public void AllowFolderAndVersionCreation(string listName)
{
var context = new ClientContext("http://anmol-pc");
var _ExceptionHandlingScope = new ExceptionHandlingScope(context);
using (_ExceptionHandlingScope.StartScope())
{
using (_ExceptionHandlingScope.StartTry())
{
// the code you want to try
}
using (_ExceptionHandlingScope.StartCatch())
{
//code to run on error
}
using (_ExceptionHandlingScope.StartFinally())
{
// the code that will execute finally
}
}
}
lets place our example code inside this structure
public static void AllowFolderAndVersionCreation(string listName)
{
var context = new ClientContext("http://anmol-pc");
var _ExceptionHandlingScope = new ExceptionHandlingScope(context);
List myList = null;
using (_ExceptionHandlingScope.StartScope())
{
using (_ExceptionHandlingScope.StartTry())
{
myList = context.Web.Lists.GetByTitle(listName);
context.Load(myList);
// context.ExecuteQuery() not required here
}
using (_ExceptionHandlingScope.StartCatch())
{
ListCreationInformation info = new ListCreationInformation();
info.Title = listName;
info.TemplateType = (int)ListTemplateType.GenericList;
myList = context.Web.Lists.Add(info);
// context.ExecuteQuery() not required here
}
using (_ExceptionHandlingScope.StartFinally())
{
myList.EnableVersioning = true;
myList.EnableFolderCreation = true;
myList.Update();
// context.ExecuteQuery() not required here
}
}
context.ExecuteQuery();
}
So you can notice that same thing can be acheived using ExceptionHandlingScope using less calls to the server.
Use of the "Try and catch" block:
- Definetly more than 1 call
- The debug operations are more complicated.
- Easily understandable code as we are known to this type of code.
- With one call to server we can acheive our goal.
- The debug operations are much easier.
- The code may not look that clear.
No comments:
Post a Comment