I came across an interesting little problem with putting query string parameters for an HTTP request into a SharePoint page.
I was using a Dataview web part, which accepts query string parameters into the filter that it runs over the data that it’s going to display. That’s pretty cool – I had a list of items with a status column, and I wanted to be able to filter the items based on that status, and that the status would come from the query string. However, on the page, I also wanted to show a title with what was being filtered by in it. So, for example, my url might read:
http://server/site/page.aspx?status=Ready
And I wanted the title in the page to read “Filtering by ‘Ready‘”.
What I didn’t appear to be able to do was to just use the request object . Something like
<%= Request.QueryString.ToString() %>
won’t work as code blocks aren’t, apparently, allowed in the file – the error message is “code blocks are not allowed in this file”. Okay, given that SharePoint designer is supposed to open these pages up to ‘Power Users’ I do get why code blocks aren’t allowed. But I really wanted to write code, and there had to be a way to do that. Well, Kirk Allen Evans has figured out how – you have to enable them in the PageParserPaths section of the web.config file. (It’s strange how easy it is to forget the web.config file that controls SharePoint sometimes). Just to repeat his example (in case blue and red on black is difficult to read):
<PageParserPaths>
<PageParserPath VirtualPath="/pages/*" CompilationMode="Always" AllowServerSideScript="true" />
</PageParserPaths>
This example allows code blocks for all files under the /pages directory of my web application – it’s worth noting that this is for the web application in IIS, not a SharePoint site collection or the like.
Okay, so that works – great. However, there are security implications, and this is probably best only used for development environments. I’ve written a bit about under what conditions our inline code will run.
In our instance our customer is kind of reluctant to make changes on their server. Changing the web.config file would be a bit of a big deal. (As a side note, this is a bit restrictive – I think a blog posting on that is due sometime). So what could we do without a server footprint?
Well, without code in the page, we would need a web control to call in that page – that would work also. It’d be trivial to write one. However, that requires deployment, marking as safe in the web.config file, etc., so really that just pushes the problem into another file.
Thus, I ended up getting a bit ‘old skool’ – JavaScript in the page to write the value from the query string on the client. I came up with:
<script type="text/javascript">
var query = window.location;
var regex = /letter=([^&=]+)/i;
var match = regex.exec( query );
if( match != null ) {
document.write( "Filtering by '" + match[1] + "'" );
}
</script>
Made a function, this became:
function getQueryParam(key) {
var regex = new RegExp(key+"=([^&=]+)","i");
var match = regex.exec( window.location
);
if( match != null ) {
return match[1];
} else {
return null;
}
}
What this code does is it gets the page’s url (window.location), runs a regular expression looking for the ‘letter’ parameter, and if it find one, it outputs some text to the document. A bit noddy, but simple and it works – with no server footprint. Of course, you do need JavaScript enabled…
[…] I set up a page with a ListView web part and a filter web part. The filter web part would supply the letter we wanted to filter by to the ListView, which would then filter on the first letter column. As a side note, I started by using the Choice Filter web part, which isn’t just a drop-down list but is rather uglier – so in the end I just used a QueryString Filter web part and built my own navigation for the filter. This was why I ended up looking at how to show query string parameters within a page. […]
Nice solution, jst what I needed!
[…] he seems to have thought that to get the query string parameter he’d have to use Javascript – something like this. However, I found myself thinking of the Data View web part – it allows parameters based on the […]
[…] I’ve detailed my attempts to show a query string parameter in a page. This should be a simple enough to do as inline code, but as my previous attempts recorded, when I […]
Couldn’t you simply use some Javascript; like “location.search.substring(1);” ?
I had a request to pass parameters to the url of an iFrame embedded in my layout page. I had to accept whatever parameters were sent so I needed to grab the entire query string and append it.
This worked fine for me; though I’m a newbie and now you have me second guessing myself? š
No, what you’ve done for what you describe is fine – but it isn’t what I’ve done in this example.
if your query string is “?letter=A&list=123456-1234-1234-1234”
“location.search” would return “?letter=A&list=123456-1234-1234-1234”
“locaton.search.substring(1)” would return “letter=A&list=123456-1234-1234-1234” (i.e. without the leading “?”)
“getQueryParam(‘letter’)” would return “A”
As you’re needing to pass the entire query string, location.search.substring(1) is fine – but all I wanted was a single value from the query string.
You migt also find this interesting:
http://www.novolocus.com/2009/05/15/get-query-string-parameters-with-regular-expressions-in-javascript/
(Same function really, but I keep having to tell people to do it this way if you only want one or two query string parameters)
Hi all,
Here is a simple way of doing it. Just place this javascript code where ever you want to display the querystring variables. (of course change var1 and var2 with your values)
//First we must call the EnsureSetup method
JSRequest.EnsureSetup();
//Get a query string parameter
var var1= JSRequest.QueryString[“var1”];
var var2 = JSRequest.QueryString[“var2″];
document.write( var1+ ”);
document.write(var2);
The JSRequest class is a JavaScript object that lives on all SharePoint Pages, and allows us to simply and quickly get query string parameters using JavaScript.
It has 3 fields: QueryString, FileName, and PathName.
QueryString – is an array of keyvalue pairs representing the current query string parameters.
FileName – is a string containing the current name of the current page.
PathName – is the server relative path of the current page.
Nice! Thanks, good tip!
Being a newbie; can someone show me with an example where to place this code when you want to show the parameter in the Titlebar Webpart? And is it also possible to use this paramater to filter a Listview webpart (Projecttasks)? Can you give an example of the code including the webpart, please? Thanks a lot!
+ I want to use the same parameter from the QueryString in one page for both the titlebar and the listview webpart.
You can add a parameter to the web part using Sharepoint Designer and this can be a QueryString value. This is then available to the Web Part.
AT on June 24, 2009 at 5:32 pm said:
Hi all,
Here is a simple way of doing it. Just place this javascript code where ever you want to display the querystring variables. (of course change var1 and var2 with your values)
//First we must call the EnsureSetup method
JSRequest.EnsureSetup();
//Get a query string parameter
var var1= JSRequest.QueryString[“var1”];
var var2 = JSRequest.QueryString[“var2”];
document.write( var1+ “);
document.write(var2);
The JSRequest class is a JavaScript object that lives on all SharePoint Pages, and allows us to simply and quickly get query string parameters using JavaScript.
It has 3 fields: QueryString, FileName, and PathName.
QueryString – is an array of keyvalue pairs representing the current query string parameters.
FileName – is a string containing the current name of the current page.
PathName – is the server relative path of the current page.
Thanks for this post. Still useful 8 years later š Just put it on one of my pages in a SharePoint 2010 system.