Friday, January 10, 2014

Easiest way to get query string parameters value in JavaScript in Sharepoint.

As a developer we need to get QueryString parameter values so many times. For this simple requirement we end up using some JQuery Library only for this purpose or writing lot of code in JavaScript. Here is good news for SharePoint developer. "JSRequest" class is available in SharePoint to get QueryString parameter value without any fuss.

Here is the way to use it :

/*Call EnsureSetup method to initialize JavaScript*/
JSRequest.EnsureSetup();
/*Suppose your site url is <siteurl>?param1=value1&param2=value2*/
var param1Value = JSRequest.QueryString["param1"];
var param2Value = JSRequest.QueryString["param2"];

I am not 100% that it will work in all the version on SharePoint or not. But It will definitely work in SharePoint 2010 / 2013. This approach will save your effort, and time. Also it will reduce the chance of bug injection.