Sep 25, 2019 1 min read

Grab the correlation id from SharePoint Online CSOM exceptions

Learn how to get the Correlation ID from ServerExceptions that might occur when working with SharePoint CSOM or PnP Core dll's.

Grab the correlation id from SharePoint Online CSOM exceptions

This week some of our code (that was running without issue for the last month) started misbehaving. Our code was using SharePoint CSOM and PnP Core calls inside an Azure Function, and intermittently we were catching exceptions saying Connection to SPO DS failed. with our local stacktrace.

Due to the nature of the issues, I wanted Microsoft involved and was redirected to the SP-Dev-Docs repository to discus this in the open. I was asked to provide the correlation id for the requests that were failing, and was provided a very helpful article to get it for requests you do with the browser in SharePoint (and by extension, when doing REST API calls).
For requests done through CSOM, you can probably use Fiddler to capture the requests, but wouldn't it be a lot easier if we could get this from the actual exception and properly log this?

Luckily, all that information is provided through the ServerException class provided with the SharePoint CSOM package. We adjusted our code to catch this specific exception, and log all relevant additional properties that it provides:

            try
            {
                //code that creates the exception
            }
            catch (ServerException e)
            {
                log.LogError($"Message: {e.Message}");
                log.LogError($"ServerErrorCode: {e.ServerErrorCode}");
                log.LogError($"ServerErrorDetails: {e.ServerErrorDetails}");
                log.LogError($"ServerErrorTraceCorrelationId: {e.ServerErrorTraceCorrelationId}");
                log.LogError($"ServerErrorTypeName: {e.ServerErrorTypeName}");
                log.LogError($"ServerErrorValue: {e.ServerErrorValue}");
                log.LogError($"ServerStackTrace: {e.ServerStackTrace}");
                log.LogError($"Source: {e.Source}");
                log.LogError($"StackTrace: {e.StackTrace}");

                throw;
            }

Of course, in the mean time the error stopped occurring 🙄 At least, we now have the correct logging in place if/when it starts showing again 😀.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Yannick Reekmans.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.