A customer wanted to distribute a survey that would anonymously collect information from mobile device users. The survey needed to pre-fill certain known values such as UserName and Client OS. The UserName is not an AD account but a custom ID from the application they are using.
I created a new NewForm.aspx in SPD and called it csNewForm.aspx. This turns the web part into a data view that allows you to edit the individual controls. This is desirable because I wanted to hide some of the fields later. We didn’t want the users to modify the values in the pre-populated fields.
Figure 1: the customized csNewForm.aspx for my survey
Some searching on the internet yielded a cool Regex that would parse out query string parameters. I implemented this into a PopulateNewForm.js file. The file calls the getUrlParameter function and passes the returned value to one of several functions that insert the value into the edit box or whatever control is appropriate.
Snippet 1: PopulateNewForm.js
In the case of this particular survey I needed to select an appropriate radio button depending on the parameter value so I simply set it’s “checked” property to true.
The last line of the script uses the _spBodyOnLoadFunctionNames object to push the populateNewForm() function into the web page at runtime. That’s the main function that calls all other functions
The implementation benefits from having a url that specifies a "Source" parameter as shown above. It will redirect the user to a custom "Thank You" page after they fill in the survey.
Final step is to hide the fields that are to be pre-filled so the user can't modify at creation time. To do this you can use the built-in "ms-hidden" class in the definition of the cells that house the controls that are bound to those fields. I.e., the label and actual control cells need to have a class="ms-hidden" attribute.
The link that is distributed to the mobile users looks like this
The parameters get populated into the form, the mobile users fill in the rest of the values and they get a nice “Thank You” at the end of it, all of it maintainable by another team that doesn’t use Visual Studio or knows anything about SharePoint. Success!