2

What is the best way to handle errors for ClientContext is there a reference material for errors thrown. When using the HTTPWebRequest you can check the status ie 403, 500. is there anything similar for the ClientContext.

I would like to implement some kind of retry etc or re authenticate if anything fails

2 Answers 2

2

You can follow the normal pattern

try {
    ...
}
catch (Exception) {
    //Get error code and act
}
finally {
    ...
}

Here is a nice blog which explains

  1. Problem with this try/catch/finally approach
  2. How to use ExceptionHandlingScope?
  3. Comparison
3
1

For server side exception handling in SharePoint 2010 and onwards you can use an ExceptionHandlingScope to provide server side fallback behaviour.

MSDN has a guide on how to use it which can be found here: https://msdn.microsoft.com/en-us/library/office/ee534976(v=office.14).aspx

To summarize:

  • Instantiate an ExceptionHandlingScope object, and within a using scope of it's StartScope method
    • Place your CSOM query within a using scope of StartTry
    • Place your exception handling code within a using scope of StartCatch
    • Place your finalize handling code within a using scope of StartFinally
  • Finally, after the StartScope using scope, call ExecuteQuery

e.g.

ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");            
ExceptionHandlingScope scope = new ExceptionHandlingScope(clientContext);            
using (scope.StartScope())
{
  using (scope.StartTry())
  {
    // CSOM query code which may fail here
  }
  using (scope.StartCatch())
  {                    
    // CSOM fallback code here          
  }
  using (scope.StartFinally())
  {                    
    // CSOM finally code here                           
  }
}
clientContext.ExecuteQuery();   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.