Skip to content

Instantly share code, notes, and snippets.

@emiliano-poggi
Last active May 22, 2020 20:35
Show Gist options
  • Save emiliano-poggi/4f1ea552a58787004525 to your computer and use it in GitHub Desktop.
Save emiliano-poggi/4f1ea552a58787004525 to your computer and use it in GitHub Desktop.
SharePoint 2013 Pass Parameter to New Form to Populate Lookup Field

SharePoint 2013 Pass a Parameter through URL to New Form

You want to pass a parameter value through URL to a new form and populate a field of the item (in this example a lookup field).

  1. Using SPD, create a New Form aspx page (you can also work directly on the NewForm.aspx page).
  2. Using the browser, connect to the aspx page and click Edit.
  3. Insert the Script Editor web part and add the relevant code (see below)
  4. Stop Editing the page (it will be automatically saved), and Cancel the form.

Now, you will be able to call the web page using URL parameter YourField:

newform.aspx?YourField=22

and open the form with the related lookup field populated with the item with ID 22.

<!-- embed this code on the web page of the form -->
<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("ctl00_ctl40_g_eb897eae_4750_4379_9548_cb0297246b1f_ff41_ctl00_Lookup").value = getUrlParameter("YourField");
}
_spBodyOnLoadFunctionNames.push("populateNewForm")
</script>
@akapital
Copy link

I am trying to implement this code above but having issues because 1. I need to decode the url and so added the decode edit and 2. uncertain about parameters used (name and "YourField") and when " or ' are required. I tried this and although I can successfully get the default value of 8 to populate when value is none, I am not able to populate when there is a valid value:

<script type="text/javascript"> // this fetches the parameter from the current url // CASE SENSITIVE function getUrlParameter('ID') { name = name.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 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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment