I had a small project in SharePoint 2007 where I needed to pull in Querystring variables from a URL into form fields in a Survey. In the environment here, Users send in Help Desk tickets through Remedy (Action Request System). When the request is complete/resolved, the ticket auto-closes 2 days later; sending the user solution details and a Link to the SharePoint Survey. The Survey is basically a Customer Satisfaction Survey, but we also needed to pull in and track things like Ticket Number, IT Group, Individual working on the Ticket, Category, Type, Item and Summary for reporting later...generally if the Survey response numbers were low or unsatisfied, it was at the top of certain reports for followup.
I worked with the Remedy Admin to build the Survey in SharePoint 2007 & once it was done, I gave him the Querystring variable names that i needed to be in the URL for him to populate programatically from Remedy. Then each User with a completed ticket got an email telling them their ticket would close in 2 days & prompted them to follow the link and fill out the Customer Satisfaction Survey. The link from Remedy looked something like this: http://moss/SiteDirectory/TheSite/Lists/IT%20User%20Survey/NewForm.aspx?num=HD00001&grp=ITISS-Backline&ind=John+Smith&cat=Hardware&typ=Laptop&itm=Video&sum=Need+assistance+setting+up+Video+conference+in+the+boardroom+for+Dave+Helfgott
I used Rob Howard's Blog article to get the Querystring manipulation with Form fields going: http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx. Great script! His article specs a Custom List, but it works just as well for a Survey (I will Blog later on how Surveys do not function like traditional List in so many ways and my workarounds for all the limited functionality and other bugs).
Ok, here is what my Survey looks like (with no Querystring data) before modifying it in SharePoint Designer 2007:

I added this JavaScript block pasted below in red to the NewForm.aspx page using SharePoint Designer. I placed the JS in between a ASP:CONTENT tag near the bottom of the page like so: <asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">INSERT-SCRIPT-HERE</asp:Content>.
There is probably a more elegant way to populate field values from Quesrystring vars, but i just copied/renamed the "setTextFromField" function like so "function setTextFromFieldName, function setTextFromFieldName2, etc." and then set up the Querystring vars like so at the bottom of the script: "setTextFromFieldName("Ticket Number", vals["num"]), setTextFromFieldName2("Ticket Summary", vals["sum"]), etc.". NOTE in setTextFromFieldName how vals["num"] corresponds with the Querystring variable NewForm.aspx?num=HD00001 in the URL above and "Ticket Number" corresponds to the name you gave the form field column you created (technically when you created a new Question as a single line of text).
So, this covers the 7 form fields I used with my 7 Querystring variables. In my situation, all 7 were "Single Line of Text" form field types, although (per Rob's article) you can populate all the typical SharePoint column types and their corresponding āidentifiersā and ātagNamesā listed here: http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx
<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("fillDefaultValues");
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags
.id;
if (tags
.title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags
;
}
}
return null;
}
function setTextFromFieldName(fieldName, value) {
if (value == undefined) return;
var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
theInput.value=value
}
function setTextFromFieldName2(fieldName, value) {
if (value == undefined) return;
var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
theInput.value=value
}
function setTextFromFieldName3(fieldName, value) {
if (value == undefined) return;
var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
theInput.value=value
}
function setTextFromFieldName4(fieldName, value) {
if (value == undefined) return;
var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
theInput.value=value
}
function setTextFromFieldName5(fieldName, value) {
if (value == undefined) return;
var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
theInput.value=value
}
function setTextFromFieldName6(fieldName, value) {
if (value == undefined) return;
var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
theInput.value=value
}
function setTextFromFieldName7(fieldName, value) {
if (value == undefined) return;
var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
theInput.value=value
}
function fillDefaultValues() {
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
var vals = new Object();
for (var i=0; i < args.length; i++) {
var nameVal = args
.split("=");
var temp = unescape(nameVal[1]).split('+');
nameVal[1] = temp.join(' ');
vals[nameVal[0]] = nameVal[1];
}
setTextFromFieldName("Ticket Number", vals["num"]);
setTextFromFieldName2("Ticket Summary", vals["sum"]);
setTextFromFieldName3("Group", vals["grp"]);
setTextFromFieldName4("Individual", vals["ind"]);
setTextFromFieldName5("Category", vals["cat"]);
setTextFromFieldName6("Types", vals["typ"]);
setTextFromFieldName7("Item", vals["itm"]);
}
</script>
OK, that is basically it once you save the NewForm.aspx page. You can test out the script out with dummy Querystring variables. The example URL i showed at the top of this would display like the graphic below. Some of the form fields you are not seeing are Group, Individual, Category, Types and Item at the bottom of the Survey. Just the way everyone wanted this laid out. Then when Users hit Finish on the Survey, it writes these populated Querystring variables to the Database as it should. Part 2 of Pulling in Querystrings into a SharePoint 2007 Survey deals with Views, Exporting the data & other Reporting options. Not to mention issues with Content Types, Workflow and Alerts that are present with Surveys, ugh.
