in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

echef

From the cluttered (and clustered) brain of Josef Nielsen... A great place for Food, Friends, and... uh... SharePoint of course!

Pre-populating Form Fields for a New Item

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.

Published May 16 2008, 08:29 AM by josef.nielsen
Filed under: ,

Comments

 

spykew said:

Hi,

How do you get the id for that specific field?

Thanks, it is a very usefull script

August 4, 2008 11:52 AM
 

josef.nielsen said:

The only way to get it is to view source on the form page you want to add the script to, ie newform.aspx... You will not find it in SharePoint Designer, as it is internal to the webpart.  Look for the INPUT tag with the correct "Title", and grab the "id" value from that tag.

August 4, 2008 12:14 PM
 

spykew said:

Thanks, it worked :)

In the mean time I found on this post a script to get the element that we want to populate: blogs.msdn.com/.../using-javascript-to-manipulate-a-list-form-field.aspx

Cheers,

Roger

August 4, 2008 12:43 PM

About josef.nielsen

I'm a long time computer nerd, living and loving technology wherever I can find it. I've recently shifted from a focus in MS SQL server, MSCS Clustering, and High Availability Engineering to focus on collaboration technologies, including SharePoint 2007. I recently moved from my long-time home in Seattle Washington to Salt Lake City.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts