0

Lots of great posts on using the getURLParameter(ID) to populate a lookup column in a NewForm.aspx when coming from a list view web part on another page. Here is my problem, the source url that I am querying does not have an explicit "ID=6" but instead since I am coming from a document set home page looks like this:"...%2FSharePoint%2FProject%2520Document%2520Set%2FForms%2FProject%2Fdocsethomepage%2Easpx%3FID%3D6..." So the part that I am trying to parse or get the url parameter is "FID%D6" instead of simply ID=6.

I tried using this code which I got from a similar post (apologies for being unable to give credit to original poster) but did not successfully populate the ID. Was only able to populate the Null result:

<script type="text/javascript">
// this fetches the parameter from the current url
// CASE SENSITIVE
function getUrlParameter( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return "";
else return results[1];
}

function populateNewForm()
{
//Populate your field
//to know the element id of your field you use the IE dev tools
document.getElementById("Project_x0020_Name_c9c83d51-e59d-4fc8-b811-d8eee5fd6818_$LookupField").value = getUrlParameter("ID");
}

_spBodyOnLoadFunctionNames.push("populateNewForm")
</script>

Any ideas on edits to use this code to parse this type of URL and/or any other existing solutions that anyone is aware of? Here is my attempt to edit the code and decode the encoded url but it is not working:

// this fetches the parameter from the current url // CASE SENSITIVE var UrlParams = document.URL.split("?")[1].split("&"); var GetUrlParam = function ('ID') { for (var i = 0; i
1
  • Karthik, thank you for your edits to improve my question, much appreciated
    – akapital
    Commented May 22, 2020 at 13:37

2 Answers 2

0

the URL is just encoded, you could try decoding the URL before passing through the function, or try another function to get the URL. Its a standard javascript function not specific to SharePoint so there are many reference out there

https://html-online.com/articles/get-url-parameters-javascript/

Perform some debugging, as in instead of getting window.location.href set it to a string test = "https://testurl.com?ID=1" and see if it returns the expected results, and go from there

This is the function I use on the daily

function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
13
  • Warwick, thank you for your response, so I will try to decode the URL before passing through the function and update with the result.
    – akapital
    Commented May 22, 2020 at 13:39
  • I was able to find many related posts which decode the url: stackoverflow.com/questions/7731778/… stackoverflow.com/questions/19491336/… stackoverflow.com/questions/25161246/… but don't know js well enough to marry with the populate new form script that was my starting point. @Warwick W
    – akapital
    Commented May 22, 2020 at 15:47
  • Tried this but did not work, any ideas: <script type="text/javascript"> function getUrlParameter(ID) {ID = ID.replace(/[[]/,"\\[").replace(/[]]/,"\\]"); var regexS = "[\\?&]"+ID+"([^&#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return "8"; else return decodeURI(results[1]); }function populateNewForm(){ //Populate SPListForm document.getElementById("Project_x0020_Name_c9c83d51-e59d-4fc8-b811-d8eee5fd6818_$LookupField").value = getUrlParameter('ID'); }_spBodyOnLoadFunctionNames.push("populateNewForm") </script>
    – akapital
    Commented May 22, 2020 at 20:17
  • what error are you getting/seeing?
    – Warwick W
    Commented May 24, 2020 at 23:39
  • comment out the field update for now and just write the output of the function to the console.log(getUrlParameter(ID)); - get that working and then do the second part to update list
    – Warwick W
    Commented May 24, 2020 at 23:42
0

Ok, this solved my problem and many thanks to Warwick and Alex B:

setTimeout(function(){ var theValue = GetUrlKeyValue("ID",false,unescape(GetUrlKeyValue("Source"))); document.getElementById("Project_x0020_Name_c9c83d51-e59d-4fc8-b811-d8eee5fd6818_$LookupField").value = theValue; },1000);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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