Thursday, July 16, 2015

Exception Handling in Client Object Model

The CSOM provides a class named "ExceptionHandlingScope" that is specifically defined to support such situations and avoid executing multiple queries against the server .


ClientContext ctx = new ClientContext("http://SP2013.local/");

ExceptionHandlingScope scope = new ExceptionHandlingScope(ctx);

using (scope.StartScope()) {

using (scope.StartTry()) {

// Try to do something on the server side
}
using (scope.StartCatch()) {

// Do something else in case of failure on the server side
}
using (scope.StartFinally()) {

// Execute this code, whatever is the result of previous code blocks
}
}

ctx.ExecuteQuery();

The server will begin executing the code inside the StartTry block, and then in case of failure,

it will execute the code in the StartCatch block. if there is any  exceptions occur in the

StartTry block, the server will finally execute the code in the StartFinally block.

No comments:

Post a Comment