Home > Work > Useful JavaScript Function – Refreshing a page with parameters for use with Query String Filter Web Parts in #SP2010 #SharePoint #ProjectServer #PS2010 #in

Useful JavaScript Function – Refreshing a page with parameters for use with Query String Filter Web Parts in #SP2010 #SharePoint #ProjectServer #PS2010 #in

In my current project I have the need to determine the current users personal preferences based on favourites stored in a list.

These personal preferences then drive various web parts on the page.

Using this the Enterprise Edition of SharePoint Server 2010 makes this very easy with the use of the Query String Filter Web Part and various other web parts that support connections including:

  • Reporting Services Web Part
  • Excel Services Web Part
  • List Web Parts
  • etc…

Using the combination of parameters and the above web parts it makes it a lot easier to create dashboard pages that are specific to a users:

  • Preferences
  • Job Role
  • Department
  • etc…

Now in most of my projects, I have found that dashboard pages tend to be buried under a 2nd or 3rd level of navigation, which makes it very easy to create links with parameters already in them.

However, in this case, I have to create various dashboard pages at the root of the site, whilst using standard SharePoint Publishing Navigation (Global Navigation).  The parameters being passed are user selectable based on favourites stored in a list.

Given that the SharePoint Site Map Provider doesn’t know about this random favourites list I have created, I need to improvise to ensure that the end user is presented with dashboard pages personalised to them, as soon as they hit the root site URL.

Code Example:

1 <script type="text/javascript"> 2 3 var currentLocation = location.href; //Current Page URL 4 //set it to lowercase for comparison purposes 5 currentLocation = currentLocation.toLowerCase(); 6 7 //run function before document / SharePoint ready to avoid unwanted page flashing 8 refreshPageWithParameters(); 9 10 function refreshPageWithParameter() { 11 12 var urlParameter = ""; 13 14 //In this example I am checking for a query string parameter called portfolioid 15 urlParameter = querySt("portfolioid"); 16 17 //confirm we do not have the parameter so the page refresh doesn't loop 18 if (urlParameter == "") { 19 //Stop the rest of the page render, JavaScript will still run 20 document.execCommand("stop"); 21 22 /* 23 Run functions you need to do to get your parameter value here 24 25 set output of functions to urlParameter below 26 */ 27 28 urlParameter = "test"; //<function output here>; 29 30 var newURL = currentLocation; 31 32 if (currentPathname.indexOf("?") != -1) { 33 //& - Other Query String Parameters exist - add this to the end 34 newURL = "&portfolioid=" + urlParameter; 35 } 36 else { 37 //? - Other Query String Parameters do not exist 38 newURL = "?portfolioid=" + urlParameter; 39 } 40 41 //Refresh the page with the new URL 42 location.href = newURL; 43 44 } 45 46 } 47 48 function querySt(ji) { 49 50 //This Useful JavaScript function was explained in a previous blog post 51 hu = window.location.search.substring(1); 52 hu = hu.toLowerCase(); 53 gy = hu.split("&"); 54 55 for (i=0;i<gy.length;i++) { 56 ft = gy[i].split("="); 57 if (ft[0] == ji) { 58 return ft[1]; 59 } 60 } 61 62 return ""; 63 } 64 65 </script>

Essentially the following happens:

  • The page load to a certain point
  • We determine if we have the query string parameter(s) we are looking for
  • If we don’t, we stop the page from rendering using document.execCommand(“stop”)
  • Go get our require parameter(s)
  • Determine if any previous parameters existed on the page currently (to make the code more generic)
  • Reload the page with the new parameter(s)

If you find any unwanted page flashing before the page reload, use JavaScript to set style=”display: none;” on the main content <div> on the v4.master – Master Page, if the parameter doesn’t exist.

Although this is certainly one method of achieving my goal and seems to currently fit my clients requirements, I would certainly be interested to hear from others on how to achieve my goal without drastically changing standard SharePoint components.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: