Important notice

Please note that this blog is discontinued here. All the posts are also coppied to my new blog at http://boris.gomiunik.net.
Important: Due to large amount of comment spam further commenting has been disabled here. If you wish to send me a comment, plase look up the same post on my new blog and leave comment there. Thanks!

7/23/2007

QueryString - get parameters from URL with JavaScript

SharePoint passes a lot of information between pages with the aid of URL parameters. If you pay attention to the URL while watching the EditForm of some list, you'll probably find the folloring url:

http://server/site/lists/listname/EditForm.aspx?ID=1&Source=http://server/site/lists/listname/Allitems.aspx

The part after the question mark is important, since it's passing 2 parameters: ID with value of 1 and Source with value of http://server/site/lists/listname/Allitems.aspx.

Using XSLT with SharePoint Designer you can also get these parameters with XSLT's QueryString. But outside of a SharePoint Data View you can get the value of URL's parameters with the following script:

function queryString(parameter) { 
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false;
//Here determine return if no parameter is found
  }
}

So if I had my page http://www.mysite.com/default.aspx?test=123

The queryString('test') would return value 123.

This function is useful also outside of SharePoint. One word of caution though: Internet Explorer tends to translate a bit the urls, so spaces become %20 and similar. This can be solved with a simple function to translate these values. More about that - next time :)

6 comments:

Petter said...

It works great! Thanks a million :)

PsychedEric said...

"Using XSLT with SharePoint Designer you can also get these parameters with XSLT's QueryString."

I am a student working for a company, and I am developing under sharepoint designer. I need to pass ( get ) the parameter from the url into an xsl variable.

I would like to know where I could find these parameters in sharepoint designer.

In fact I need to replace in an xsl:if the test with ddwrt:Today(), by a date I choose in an input text ( or something else ).

I hope you have a solution.

Eric

Boris Gomiunik said...

Hi, PsychedEric. Here's a reply in an extra post I've made:

Using Parameters from URL as variables in XSLT Data View

If you need dates, you can even make the web part connection. If you'll use dates in URL, you have to make it in appropriate format - you can see the format if you enter <xsl:value-of select="@Created" /> in the XSLT Data View.

naresh said...

Thanks a lot .it worked.

pushkar said...

hi,
i have a calendar list which has recurring and non-recurring tasks.
i am trying to modify the editform.aspx of the calendar list by making the sharepoint default ListFormWebPart invisible and inserting my custom edit webpart from "insert"-"sharepoint controls"-"custom list form" because i want certain fields to be non-editable.
the problem is that the tasks can be edited but the daily tasks cant be.If i open to edit the daily task from the calendar it just shows the toolbar. i wanted to know if there's a way out.
thanks

chris said...

Hi,
For some reason I can not see the values in the url.
I added the Parameters by right clicking on custom button > form Actions. I attached the link to be directed to and added a few paramters.
Any idea?
Thanks
Regards

Chris