So for surveys and such I thought it would be nice to pre-populate a SharePoint List "new" form with some data from the link... This makes it very nice for surveys and such from users. In this case it was to automate and remove error from some user feedback for our helpdesk. So what I did was make the URL contain an extra parameter for a field value, then I added some JavaScript to the NewItem.aspx page in that list. This script is specific to one field one (as you can tell by the long auto-ID). Here is the script I used:
function getURLParam()
{
var strReturn = "null";
var strHref = window.location.href;
//find the "Ticket" parameter
if ( strHref.indexOf("?") > -1 )
{
var strQueryString = strHref.substr(strHref.indexOf("?")+1);
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
{
//find the Task Parameter
if (aQueryString[iParam].indexOf("Ticket=") > -1 )
{
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
//insert parameter to Task Field
//This should be one line, not two...
//the field name is too big to fit in the blog window ;)
document.aspnetForm.ctl00_m_g_2a4ef5db_f38c_4101_b1e9_bd5bce3e5a04_
ctl00_ctl04_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_TextField.value = strReturn;
}
}
_spBodyOnLoadFunctionNames.push("getURLParam");
Notice the execute line, using _spBodyOnLoadFunctionNames. This is required to get the script to execute after all the content has been populated. The URL to feed this function would look something like this:
http://sharepointserver.company.pri/sites/feedback/lists/tickets/NewItem.aspx?Source=http%2A%2F%2Fsharepointserver%2Ecompany%2Epri%2Fsites%2Ffeedback%2FPages%2FThannkYou%2Easpx&Ticket=123456
Two things to note... first, the "Source" parameter needs to be Escaped (using "%" codes for all symbols), and second, that it doesn't have to be where you came from... It is actually where you go to when you click OK or Cancel. In this case, I'm sending the user to a Thank You page since they have filled out the feedback. This can be anything, including a custom page with code to close the browser window if you want.