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:
- Click the ellipses (three dots) to open “String builder”
- Paste the normal link to the second list’s NewForm.aspx page (ContinuingEdRegistration in my example)
- 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
- Click “Add or change lookup” to add Current Item –> ID
- Add a comma and a space (will not work without space)
- 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.
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 =).
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):
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:
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.
To test your solution, just modify or create a new calendar item. Here’s what should happen:
- Your SPD workflow runs, setting the “Register Here” hyperlink field to the correct URL with that item’s unique ID included
- 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.
- The script finds the URL parameter and dynamically updates the dropdown to the lookup choice that matches the ID.
- 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.
2 Replies to “Pass URL parameter to SharePoint new form (NewForm.aspx) to auto-populate a lookup field”
It would be amazing to see a “Modern” version of it.
How would you do it not using workflows and the NewForm.aspx Nate?
Id really love to do this, but for some reason my NewForm.aspx pages are not editable pages. Theres no way to add anything to them, unless Im using powerapps.I guess Ill try embedding the code into powerapps and see if that works.