I'm relatively new to javascript and i am currently working on a project that has previously had a SharePoint contractor involved, person has now left the organisation. He has used some script on an aspx page that passes in some parameters to a js file which runs a REST call on a SharePoint list. The result is that the SharePoint list item displays in a nicely formatted HTML page as opposed to the standard list display forms. The SharePoint list item ID is appended into the URL of the page as what i believe is a query string/parameter (i may be wrong as i don't fully understand it).

What i'm looking for, if someone would be kind enough, is to give me a dummies guide of what is actually happening and explain the regex used and how it all comes together.

I've pasted the main code into chrome and have seen exact matches come up in the search results, so i assume from this it is a common pattern used and not particularly specific to a SharePoint list.

I'd also like to be able to expand on the script to add in the list item title to the URL if this is possible. Code snippets below.


<!--Get the ID from URL-->
<script type="text/javascript">
function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    vars[key] = value;
    return vars;

<script type="text/javascript">
    var url = window.location.protocol + "//" + window.location.hostname;

The below function is then run from the page...


Here is the code from the JS file that catches the values passed in the function

function getNewsItem(url) {

    var ID = getUrlVars()["ID"];
console.log("the ID is " + ID);

    url: "http://SITE URL HERE/sites/SITE NAME/_api/lists/getbytitle('NewsArea')/items?$filter=ID eq '"+ID+"'&$expand=Author/Id&$select=Author/Title,Title,MainText,PublishedDate,Category,ImageURL",
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
        "Access-Control-Allow-Origin": "*"
    success: function(data){

    ........Rest of function then runs below

No need to understand the regexp, that "developer" added a getUrlVars function that SharePoint provides in at least 3 ways... for free:

What does this code getQueryStringParameter do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.