SharePoint 2013 : Batch Exception Handling
Batch Exception Handling in JSOM is a new concept in SharePoint 2013 that enables us to execute Try Catch and Finally code blocks in the same way as we do in Server Side Code i.e. executing with in the same Server Call.
For instance, while working with Server Side code if we need to create a new list then we need to first check its existence and if the list does not exists only then code will create it.
Point to notice here is this whole logic will gets executed in the same Server Call.
On the other hands if we try to perform the same action while working with JavaScript Client Object Model (JSOM) we need to issue two separate calls to the server, one is to check for the existence and other is to load or create the list.
Using new Batch Exception Handling constructs “startScope, startTry, startCatch, and startFinally” we can achieve this in single JSOM Call to the Server.
In this article we will see the implementation details for Batch Exception Handling using SharePoint Hosted App.
- Create a new project using “App for SharePoint 2013” Project Template
https://howtodowithsharepoint.files.wordpress.com/2015/09/19.png?w=300&h=206
- Specify the Host Web URL
- Specify “SharePoint Hosted” as Hosting Model
https://howtodowithsharepoint.files.wordpress.com/2015/09/21.png?w=300&h=216
- Wait while Visual Studio configure Project for you
https://howtodowithsharepoint.files.wordpress.com/2015/09/31.png?w=300&h=134
- Prepare UI using HTML Elements decorated the desired CSS using Default.aspx (App Start Page)
Step 1: Specify the Page Title
Step 2: Add HTML to present the UI Elements for “Result Panel” and decorate the element with the required CSS of your choice
Step 3: Adding container to hold the button to execute Batch Exception Handling Process
Step 4: Add a HTML Button, specify its Label and decorate it with the required CSS
https://howtodowithsharepoint.files.wordpress.com/2015/09/41.png?w=300&h=116
- Write code to implement the logic for Batch Exception Handling by modifying App.js JavaScript File
Step 5: Bind the event handler with the Button using dynamic binding in “document. Ready” function
https://howtodowithsharepoint.files.wordpress.com/2015/09/51.png?w=300&h=43
Inside the Event Handler function “batchExceptionHandling”
Step 6: Instantiate SharePoint Context and get the instance of the current web (all old school thing)
Step 7: Instantiate the Exception Handling Scope
Step 8: Start a Scope block by calling startScope() method
Step 9: Start a Try block by calling startTry() method
Step 10: Inside the Try Block, try to load the instance of the list in our case list is “My-Custom-List”
Step 11: Dispose the Try Block by calling dispose() method
Step 12: Start the Catch Block by calling startCatch() method
Note: This catch block will be executed only when action in try block gets failed, like in this case if we try to load “My-Custom-List” and it is not available in the SharePoint Site the operation will get failed and catch block gets executed.
Step 13: Since we get an exception while loading the list that means list is not available, so adding the new list with the same name
Step 14: Dispose the Catch Block by calling dispose() method
Step 15: Start the Finally Block by calling startFinally() method
Step 16: Load the list now, since by this time we will be having list available anyways (Created if not exists or loaded if exists)
Step 17: Dispose the Finally Block by calling dispose() method
Step 18: Dispose Scope Block by calling dispose() method
https://howtodowithsharepoint.files.wordpress.com/2015/09/61.png?w=300&h=174
Step 19: In the success callback function check for the exception occurred during the function call by calling “get_hasException()” method, if the method returns True that means we had created the list else we already had a list that we have loaded.
https://howtodowithsharepoint.files.wordpress.com/2015/09/71.png?w=300&h=40
With this step we are all done with the coding part and now the next step is to launch the app and see some action.
- Build and deploy the solution
https://howtodowithsharepoint.files.wordpress.com/2015/09/81.png?w=300&h=154
- Enter the Credentials when asked for Authentication
https://howtodowithsharepoint.files.wordpress.com/2015/09/91.png?w=300&h=188
After successfully authenticating the User, we can see the Start Page for the App
https://howtodowithsharepoint.files.wordpress.com/2015/09/101.png?w=300&h=122
Open the SharePoint Manager and browse the App Web -> Lists and see if we got the list “My-Custom-List” created or not, in our case we do not have any such list created
https://howtodowithsharepoint.files.wordpress.com/2015/09/111.png?w=300&h=199
Now click the Button “Batch Exception Handling” and see the Result Panel
First time when we click the button, JSOM code executes within the Exception Handling Scope, and see if the list is already available in the Site or not, if not it will be added by the code present in catch block as shown below–
https://howtodowithsharepoint.files.wordpress.com/2015/09/121.png?w=300&h=131
We can confirm the same in SharePoint Manager as well-
https://howtodowithsharepoint.files.wordpress.com/2015/09/131.png?w=300&h=169
When we click the button afterwards the code in Try Block gets executed and successfully loads the list due to which in the Success Callback function “get_hasException()” function returns False and we get an alternative message in the Result Block as shown below –
https://howtodowithsharepoint.files.wordpress.com/2015/09/142.png?w=300&h=129
I found this technique really effective when we have a scenario for implementing Fall Back mechanisms without involving multiple Server Calls for the same request.
Hope this simple implementation could help you to understand the concept of Batch Exception Handling.