JavaScript Object Model is an asynchronous programming model, which enables the communication with SharePoint in the client side Browser. As with any programming constructs, JavaScript Object Model also has the exception handling techniques. JavaScript provides native try/catch to handle the exceptions. Though JSOM is based on JavaScript, using Try/Catch will not really do exception handling in JSOM. This is because of the asynchronous nature of JSOM. In JSOM, all the operations are sent as a batch to ‘client.svc’ WCF Service in the Server, which would process the request.
Exception Handling Scope
SharePoint, however provides an exception handling mechanism, similar to JavaScript try/catch of the format startTry() and startCatch(). Its implementation is similar to try/catch.
The main object, which handles the exception in JSOM Is called ‘ExceptionHandlingScope’. It provides the methods, given below, to perform an exception handling.
- startScope
- startTry
- startCatch
- startFinally
Let’s see, how it works:
Internal Implementation
With any JSOM implementation, we create the base object, which is the client context. Afterwards, we create an exception handling scope object, using the client context. Once the exception handling scope object is created, we call it’s ‘startScope’ method. This method indicates that the entire code within the block will be eligible for an exception handling. Let’s take an example code, which will try to update a list, which does not exist. This would generate an exception, which will be handled by the exception handling scope.
Once the exception handling scope starts, we can initiate the try block, using the ‘startTry’ method. Any code written within the ‘startTry’ method will be checked for exceptions and in the event of an exception, the control will be passed to the exception handling block , which is the catch block. The code block, which began with ‘startTry’ will be ended, using the dispose method. The exception handling block, which will be called in case of any error in the try block is the catch block, which is instantiated, using ‘startCatch’ method. Within this block, we can add the code, which will resolve the issue, that happened in the try block. Similar to try block, catch block will also be ended, using the dispose method.
The last code block of the exception handling paradigm is finally a block, which is instantiated, using the ‘startFinally ’ method. This block will have all the code, which needs to be run, regardless of an error, which might occur. The dispose method can be called to indicate the end of the finally block.
We can see, how an exception handling paradigm can be used in a real word scenario. Let’s see, we are trying to update a list, which does not exist in SharePoint environment. This will ideally throw an exception, as the object is not present. We will write the erroneous list update code in try block and handle the exception in the catch block by creating the list. In the finally block, we will again try to update the list. This time, the list will be updated without any exceptions.
Script
The full script for the exception handling scope implementation is shown below:
- <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function() {
- SP.SOD.executeOrDelayUntilScriptLoaded(CheckandCreateList, "sp.js");
- });
-
- function CheckandCreateList() {
-
- var clientContext = new SP.ClientContext.get_current();
- var oWeb = clientContext.get_web();
-
- var exceptionScope = new SP.ExceptionHandlingScope(clientContext);
- var startScope = exceptionScope.startScope();
-
- var tryScope = exceptionScope.startTry();
-
- var oList = clientContext.get_web().get_lists().getByTitle("ExceptionList");
- oList.set_description("Updated Description");
- oList.update();
-
- tryScope.dispose();
-
- var catchScope = exceptionScope.startCatch();
-
- var newListCreationInfo = new SP.ListCreationInformation();
- newListCreationInfo.set_title("ExceptionList");
- newListCreationInfo.set_description("Catch Updated Description");
- newListCreationInfo.set_templateType(SP.ListTemplateType.genericList);
- clientContext.get_web().get_lists().add(newListCreationInfo);
-
- catchScope.dispose();
-
- var finallyScope = exceptionScope.startFinally();
-
- var oList = clientContext.get_web().get_lists().getByTitle("ExceptionList");
- oList.set_description("Finally Updated Description");
- oList.update();
-
- finallyScope.dispose();
-
- startScope.dispose();
-
- clientContext.executeQueryAsync(Success, Failure);
- }
-
- function Success() {
- console.log("The list has been created successfully.");
- }
-
- function Failure(sender, args) {
- console.log('Request failed - ' + args.get_message());
- }
- </script>
Output: Console output after implementing the exception handling mechanism is given below:
Summary: Thus, we have seen, how to implement an exception handling in JavaScript Object Model programming in SharePoint 2016 and Office 365. As a best practice, it is advised to add the exception handling blocks in JSOM code to take care of the unexpected road blocks during code execution.