Skip Ribbon Commands
Skip to main content

Tanmay Shahane | zevenseas | SharePoint Blog

:

Tanmay Shahane | zevenseas | SharePoint Blog > Posts > Try catch blocks in JSOM for server error in SharePoint 2010
August 21
Try catch blocks in JSOM for server error in SharePoint 2010

 

Hello kind readers…
 
JSOM… or the JavaScript client object model that is one of the major features of SharePoint 2010 is really a great API for doing loads of functionality without owning a Visual studio :)
 
I really love the JSOM and use it very often and with time I always learn something new about it… the one thing I wanted to share with all was about the error handling/trapping with JSOM, as more and more people are moving towards using the client object model, knowing the right error handling techniques could really help in better coding practices and more robust code.
 
Mostly when we make ASync calls with client object model… we provide a error function to get any errors that might have come in the query and it gives out a message saying "cannot complete operation"  or something… well not always would you have a clear idea on what happened at the server end to troubleshoot the error, for that you need a proper server side error message.
 
Now with JSOM you can still get that error message and this is something that can be REALLY helpful at times… following are the two ways of handling error in JSOM, one is normal usual Client side way, other is when you trap sever side error message,
 
Normal way:
//get clientcontext
//get some web
//get some list
//make some query
Ctx.executeQueryAsync(onQueySucceeded,onQueyFailed)
 
funciton onQueySucceeded(){...}
funciton onQueyFailed() {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
 
This way could give some information of course but not always the server end information about the error which you might need for troubleshooting or understanding the cause of your functionality failure.
 
SP namespace itself provides a way to handle the error scenarios which can give you server side error information as well, and it is nice one… following is the way to use it..,
 
var _siteObjects ={
errorScope: null,
clientCtx:null,
};
_siteObjects.clientCtx = //get the current client context or the site relative client context
_siteObjects.errorScope = new SP.ExceptionHandlingScope(_siteObjects.clientCtx);
//scope for handling starts
var startScope = _siteObjects.errorScope.startScope();
//Start the try block for trapping server error
var tryBlock = _siteObjects.errorScope.startTry();
// you query and other operations here like getting the web and list and items query
//load the objects in context to get from server
Ctx.load('objects here');
 
  //end try block
tryBlock.dispose();
 
//start catch block
var catchBlock = _siteObjects.errorScope.startCatch();
 
//place holder for server processing on error if any
//like if you want to delete the item being created or editing if any errors
//or any other thing you would want if the error was thrown
//NOTE  - server side things will only execute if the error was thrown in corresponding try block
catchBlock.dispose(); //end catch block
startScope.dispose(); //exception scope ends
 
//now you call your execute query with normal way
_siteObjects.clientCtx.executeQueryAsync(onQueySucceeded,onQueyFailed)
 
function onQueySucceeded(sender, args) {
        if (_siteObjects.errorScope.get_hasException()) {//If there was a server side error log it in the div from exception object
            jQuery(_containerId).append("<div message='" + _siteObjects.errorScope.get_errorMessage() + "'>An error has occured.</div>");
        }
 
Now this errorMessage is the error that was thrown at server end by the code and you could get the exact error from this to trouble shoot.
 
 
This is just another way of error handling when working with Client object model in JS and could give you more robust ways of handling different situations based on your needs and requirements.
 
Hope this helps somebody….
 

      - "T"

Comments

Nice one

The main advantage of this according to me is that you can execute multiple scenarious with just one call to the server!
 on 8/21/2012 5:42 AM

Christian Louboutin Boots On Sale


My partner and i take a nap, please take a straw head wear, along with buckles face,desire grass inside Hong Kong. I came to be to depart you, Once you enter in the path involving crimson dirt,   Christian Louboutin Boots On Sale   the actual wind chime because i tend not to invest the particular wedding ring.While this individual came up, This individual shook his / her head in a very vast natrual enviroment road, and smiled. I will be myself, you did not maintain myself in a aspiration.
http://www.zamshoes.com/ Christian Louboutin Boots On Sale
http://www.zamshoes.com/categories-pumps.html black christian louboutin pumps sale
http://www.zamshoes.com/categories-boots.html christian louboutin boots on sale
http://www.zamshoes.com/categories-for-men.html  christian louboutin men
http://www.zamshoes.com/categories-sneakers.html christian louboutin sneakers sale
http://www.zamshoes.com/categories-flats.html christian louboutin flats cheap
http://www.zamshoes.com/miu-miu-shoes.html miu miu shoes outlet
http://www.zamshoes.com/jimmy-choo.html jimmy choo shoes on sale
http://www.zamshoes.com/manolo-blahnik.html manolo blahnik something blue
 on 11/4/2012 4:41 PM

Add Comment

Title


Body *


Attachments

 

 Related Posts

 
 

 Statistics

 
Views: 563
Comments: 2
Tags:Client Object Model
Published:276 Days Ago