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
  • Thanks, do you know if there is any reference material regarding what errors can come back from an exception? Commented Jan 12, 2015 at 14:25
  • 2
    I think this is what I'm looking for github.com/OfficeDev/PnP/tree/master/Samples/Core.Throttling Commented Jan 12, 2015 at 14:28
  • You can look for HTTP Response Error codes and use that. Commented Jan 12, 2015 at 14:29
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 you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.