Skip to content

Instantly share code, notes, and snippets.

@empo empo/0.md
Last active Sep 30, 2017

Embed
What would you like to do?
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>
@mikeclt

This comment has been minimized.

Copy link

mikeclt commented Dec 27, 2016

Line 19 - how do I find the value for the .getElementByID string?

@hzerfass

This comment has been minimized.

Copy link

hzerfass commented Sep 30, 2017

I am able to get this to populate the field in my form when I'm in the form....but it isn't saving the value when I submit the form. Is there a way to get the value to actually save without clicking in the field and then hitting enter or tab? Has anyone else run into this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.