Pass URL parameter to SharePoint new form (NewForm.aspx) to auto-populate a lookup field

I recently received a request to create an education listing of courses where a user could “Register” easily for upcoming opportunities. This required creating two lists: one for courses and another for the registrations.

The most important part of this was that when a user clicked “Register Here” on a calendar item, it would take them to this separate registration form on a completely different list and auto-populate the Training lookup drop-down based on which “Register Here” link they clicked.

Courses list (calendar app)

The first list for courses, Courses, is a calendar app with the courses. It has two necessary columns you must add:

  • A calculated column called Training: =IF([End Time]>=NOW(),(TEXT([Start Time],”YYYY-MM-DD HH:MM AM/PM”)&” | “&Title),””)
  • A custom hyperlink-type field called Register Here. The value is set by a simple SharePoint Designer workflow.

The link is structured so that when clicked, it’ll take the user to the NewForm.aspx form for the registrations list (ContinuingEdRegistration). The user will enter their info there to register for the course.

For the workflow, I used a simple two-step 2010 platform workflow for the Courses list, set to trigger on creation OR change.

When setting the variable in the workflow:

  1. Click the ellipses (three dots) to open “String builder”
  2. Paste the normal link to the second list’s NewForm.aspx page (ContinuingEdRegistration in my example)
  3. Add ?Training= to the end of it, replacing Training with whatever URL parameter you’re wanting to use to help in auto-populating a field
  4. Click “Add or change lookup” to add Current Item –> ID
  5. Add a comma and a space (will not work without space)
  6. Enter link text, like Register Here

The second (and last) step is to set the hyperlink field we created (Register Here) to that variable from the first step. Publish the workflow.

You can set the calendar view to whatever works best – an actual calendar, a list, boxed, etc. as long as the registration link is available to users to click.

ContinuingEdRegistration list (Custom list app)

The ContinuingEdRegistration list and new form is simple:

  • A lookup column to our Training on the first list
  • A field for the user being registered

The key to this working is editing this ContinuingEdRegistration’s NewForm.aspx page seen above and adding the following script via a Script Editor web part or Content Editor web part. Thanks to Emiliano Poggi for sharing the script that inspired this solution.

<script type="text/javascript">

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()
{
document.getElementById("Training_67194962-c685-4033-8744-701fd9f26beb_$LookupField").value = getUrlParameter("Training");
}
_spBodyOnLoadFunctionNames.push("populateNewForm")
</script>

Be sure to update both the “ID” boxed below, as well as the URL parameter to look for (used in the SPD workflow between ? and =).

Click to enlarge

If you’re unfamiliar with getting element IDs using F12/developer tools, just go to the NewForm.aspx, hit F12, choose the “selector” probably a mouse as seen here and then left-click the element (dropdown) to get its ID. The following screenshot is using Chrome to find the ID (in blue following the # mark):

Click to enlarge

Once you’ve updated the script for your specific fields, you can either copy and paste it into a script editor web part or save the script as a .js file in a folder like Site Assets then reference it in a Content Editor web part like this:

Click to enlarge

Once you’ve pasted a link to the script in the content editor web part properties panel, click OK. Then you can stop editing/save the page to continue.

Testing

To test your solution, just modify or create a new calendar item. Here’s what should happen:

  1. Your SPD workflow runs, setting the “Register Here” hyperlink field to the correct URL with that item’s unique ID included
  2. Your user clicks that hyperlink on the calendar item and is taken to the Registration list’s NewForm.aspx page. Note that the URL should include ?Training=123 or something similar at the end- this is the URL parameter the script is looking for.
  3. The script finds the URL parameter and dynamically updates the dropdown to the lookup choice that matches the ID.
  4. The user completes the rest of the registration fields and saves/submits.

Taking it further

A further enhancement might be adding &source=CoursesListURL to the URL created in the workflow to redirect users to the calendar again after registering.

Ta-da! Best of luck.

CategoriesUncategorizedTags

2 Replies to “Pass URL parameter to SharePoint new form (NewForm.aspx) to auto-populate a lookup field”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.