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).
- Using SPD, create a New Form aspx page (you can also work directly on the NewForm.aspx page).
- Using the browser, connect to the aspx page and click Edit.
- Insert the Script Editor web part and add the relevant code (see below)
- 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.
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>