Introduction
Much of the SharePoint code we write in C# today is done in CSOM (Client Side Object Model), a Microsoft-generated collection of libraries that serve as a proxy to the server-side object model.
I encountered two problems during a job:
- I was unable to catch errors that occurred in SharePoint Online (SPO).
- The default load size in SPO is 2 MB. So I hit a limit and therefore no longer received any data over the line.
The solution to the first problem is to work with an ExceptionHandlingScope.
The solution to the second problem is to send the data over the line in batches.
Below I will elaborate on both solutions in an example in which a large amount of source data is used to create list items in SharePoint Online from a remote event receiver.
What do you need
The Microsoft SharePoint CSOM assemblies, of which the following assemblies are the most important:
- Microsoft.SharePoint.Client
- Microsoft.SharePoint.Client.Runtime
You will receive this as standard when creating a new SharePoint provider-hosted app project. Otherwise you can also easily install them via the NuGet Package Manager console with the command: Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.3912.1204
Implementation
In the code below we are actually simply going to say to the software:
“ Perform my actions per set of items and only then let me do the error handling”.
To achieve this, a batch construction is built where the error handling is stored in the client context.
Construction of a dictionary of ExceptionHandlingScopes in the same ClientContext
It can be seen that a dictionary with ExceptionHandlingScopes is first created on line 142. Subsequently, an ExceptionHandlingScope is added with the same ClientContext for each item to be processed. The index of the row to be processed is also given as a key, so that it can later be traced which scope belongs to which row.
Just as with C# you use the try/catch/finally pattern for error handling in server-side code, with CSOM this is done from line 150 with a StartScope/StartTry/StartCatch pattern.
Note that nothing is done in the StartCatch on line 167, because we now do all the error handling per batch afterwards.
Please note: runtime errors in the client code must still be caught with a regular try/catch/finally pattern. If this is not done and an error occurs within the StartTry code block, the following message will occur: “Incorrect Usage of Exception Handling Scope”.
Now that the preparation has been made, it is time to actually send everything over the line to SharePoint Online. Since the “payload” of what can be sent over the line is 2 MB per request by default (depending on the settings), we do this in batches, as detailed in the code below.
See what it does: send and receive
Line 178 shows ExecuteQuery being called on the client context, sending the delta of changes to SharePoint Online. As soon as this call has been processed, any errors that have occurred can be immediately retrieved from the batch scopes dictionary
The interesting thing about this is that for each processed item, the ExceptionHandlingScope is updated in the client context. If it has a ServerErrorCode other than -1, the Key and ErrorMessage can be read.
Due to the batch design, don’t forget to send the remainder to SharePoint Online and handle it. This happens with rule 175. We also pay attention to the little ones.
Finally, the logging shows what exactly went wrong for which item in SharePoint Online.
The attentive reader could already see that line 160 would cause an error.
Output of the error handling
This makes it a piece of cake to transfer large amounts of data to SharePoint Online and subsequently accurately identify any errors that have occurred.
Geef een reactie