Skip to main content

Mike Geyer's Blog

A fun place to read and collaborate about SharePoint and related technologies.
Go Search
  

Keep in touch: RSS Feed  


I'm a


My company is a


And we deliver

  (we're certified)
Mike Geyer's Blog > Posts > How to Use JavaScript to access SharePoint Query String
How to Use JavaScript to access SharePoint Query String

I was working on a project for a great client the other day, using the Data View Web Part (DVWP) and I was having problems with web part connections between the Query String (URL) Filter and two of the DVWPs.  Really, the connections were working, but I needed to add a “custom” hyperlink for editing the displayed item.  There were real, actual, business requirements that wouldn’t allow me to use the out-of-the-box functionality within the DVWP.

Because we were on a tight schedule, I decided to go looking for a different way to gather the query string values using JavaScript.

I wish I could provide a link to the original source for this JavaScript code, but I’m just not having luck in finding the original source.  If this is your content, please comment on this blog entry and I’ll update immediately!

Here’s the JavaScript:

<script language="javascript" type="text/javascript">
<!--
function PageQuery(q)
{
    if (q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if(q)
    {
        for(var i=0; i < this.q.split("&").length; i++)
        {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s)
    {
        for(var j=0; j < this.keyValuePairs.length; j++)
        {
            if(this.keyValuePairs[j].split("=")[0] == s)
            return this.keyValuePairs[j].split("=")[1];
        }
    return false;
    }

    this.getParameters = function()
    {
        var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++)
        {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }

    this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key)
{
    var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
}

// -->
</script>

 

Next, by using a Content Editor Web Part, I included the following HTML and JavaScript:

<hr size="1" style="height: 1px" class="style2" noshade="" />
<table border="0" width="100%">
<tr>
  <td class="ms-vb" align="right">
    <script language="javascript" type="text/javascript">
      <!--
      document.write("<strong><a href=\"EditForm.aspx?ID=");
      document.write(queryString('ID'));
      document.write("&ProjectID=");
      document.write(queryString('ProjectID'));
      document.write("&Source=");
      document.write(escape(window.location));
      document.write("\">Edit</a></strong>&nbsp;&nbsp");
      // -->
    </script>
  </td>
</tr>
</table>

 

Put it all together (or change for your particular business needs) and enjoy.  I hope this helps.

Mike

Comments

Re: How to Use JavaScript to access SharePoint Query String

This post just saved me a ton of time.  Thank you!  Great blog, by the way.
at 8/9/2009 4:00 PM

Re: How to Use JavaScript to access SharePoint Query String

That's quite a big function for processing a string like that. I'd consider just using a regular expression. See
http://www.novolocus.com/2008/03/05/showing-query-string-parameters-in-a-page-in-sharepoint/ 
for my example!

It's surprising the number of folks who have just this problem, though.
at 8/9/2009 4:00 PM

 All Posts

Creating a SP List via Excel - Import from Spreadsheet
I've Moved to Hitachi Consulting
SharePoint 2010 Sneak Peak!
Business Intelligence During an Uncertain Time
Keeping In Touch with Family Around the US
Using Javascript to Manipulate a List Form Field
How to Use JavaScript to access SharePoint Query String
First Blog with Windows Live Writer
Microsoft to offer free classes and certification testing
Job Description - SharePoint Solution Architect
SharePoint Blogs as Status Reporting... and community building
Installing MOSS on WinServer 2008 with Hyper-V and SQL08
Keeping up with SP Blogs
Use Search as Content Roll-up
Web browser compatibility in InfoPath Forms Services
Picture Library: Getting to the Thumbnails
All about dates and SPD
Data View Web Part, learn it... love it!
MSFT's SharePoint 2007 Buzz Kit
Content Query Web Part (CQWP) for All List Types
Content Query Web Part (CQWP) with Anonymous Access
My Blog is Live
Examples of Common Formulas
Creating a Form to Add Items to a SharePoint List
Building a Blog Host
Scaling to Extremely Large Lists
Configuring Email Alert Templates
Populating Form Data from SharePoint List Views
SharePoint Designer Workflows and InfoPath Form Libraries
Restoring the Quick Launch Bar in Web Part Pages
Creating a Hyperlink to an InfoPath Form
Changes to SPD Created Workflows
How to find the CSS Class Applied to any SharePoint Element
Publishing an InfoPath 2007 Form Template to a Server Running InfoPath Forms Services
Why you Shouldn't Choose "Enable Rendering on a Mobile Device" when Publishing an InfoPath Form Template
Find Form Template for an Existing Library
Adding a Portal Breadcrumb to your My Site
Define Custom Permission Levels
Exchange 2003 Hotfix (Outlook 2007 and SP Alerts)
Copyright© 1997-2009 Mike Geyer
The posts on this weblog are provided “AS IS” with no warranties, and confer no rights. The opinions expressed herein are personal.