Pre-Populating SharePoint List Item Values Using Query String Variables

A client of ours was having trouble getting this idea to work based on a post he found on the Microsoft SharePoint Designer Team Blog.  I took a look and got it to work nicely in a test situation.  This is a nice trick, as it can help to pre-populate metadata that your users might otherwise need to provide.

Here’s how I got it to work:

  • I created a Custom List on a MOSS site with no customizations
  • I added the script below into the page, just below the line:       <asp:Content ContentPlaceHolderId=”PlaceHolderMain” runat=”server”>
  • Now, when I hit the New button on the list, I get alerts that the JavaScript is parsing out the Query String variables, as expected
  • I altered the default URL to look something like: http://<<servername>>/Lists/<<List Name>>/NewForm.aspx?Title=XXX
  • The Title is pre-populated with the Query String value that I provide, like ‘XXX’ above

The original post above shows how to make this work for a lookup column, and the code can be extended to work with any column type.  (It would be a nice exercise to make the code work regardless of the column type, but I’ll leave that as an exercise for the reader.)

I’ve left in the alerts that I used to test things so that you can see how it flows.

<script type="text/javascript">


function fillDefaultValues() {
  var qs =,;
  var args = qs.split("&");
  var vals = new Object();

  for (var i=0; i < args.length; i++) {
    var nameVal = args&#91;i&#93;.split("=");
    var temp = unescape(nameVal&#91;1&#93;).split('+');

    nameVal&#91;1&#93; = temp.join(' ');
    vals&#91;nameVal&#91;0&#93;&#93; = nameVal&#91;1&#93;;
    alert('fillDefaultValues: vals&#91;nameVal&#91;0&#93;&#93;=' + vals&#91;nameVal&#91;0&#93;&#93; + ' nameVal&#91;0&#93;=' + nameVal&#91;0&#93; );
  setValueFromQueryString(nameVal&#91;0&#93;, vals&#91;nameVal&#91;0&#93;&#93;);

function setValueFromQueryString(fieldName, value) {
  alert('setValueFromQueryString: fieldName=' + fieldName + ' value=' + value);
  if (value == undefined) return;
  var theField = getTagFromIdentifierAndTitle("input","TextField",fieldName);
  if (theField != null) theField.value = value;

function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  alert('getTagFromIdentifierAndTitle: tagName=' + tagName + ' identifier=' + identifier + ' title=' + title);
  var len = identifier.length;
  var tags = document.getElementsByTagName(tagName);

  for (var i=0; i < tags.length; i++) {
    var tempString = tags&#91;i&#93;.id;
    if (tags&#91;i&#93;.title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
                alert('getTagFromIdentifierAndTitle: tags&#91;i&#93;.id=' + tags&#91;i&#93;.id + ' tags&#91;i&#93;.title=' + tags&#91;i&#93;.title + ' tags&#91;i&#93;=' + tags&#91;i&#93;);
      return tags&#91;i&#93;;
  return null;

NOTE: This approach will NOT work in SharePoint 2003 because the column input fields on the page are generated differently.  Rather than just using the basic <INPUT> tag to construct the input fields (as MOSS does), each column is displayed by using JavaScript, like the example below (do a View Source on your page to see this in your environment):

  <TH nowrap valign=top class="ms-formlabel"><nobr>Year</nobr></TH>
  <TD class="ms-formbody" style="padding-left:5px">
      fld = new TextField(frm,"Year","Year","2008");
      fld.cchMaxLength = "255";
      fld.cchDisplaySize = "";
<SPAN class="ms-formdescription"></SPAN>

This is done internally in the ListFormWebPart, so short of writing a custom Web Part with a new BuildUI function, I just don’t see a way to make this approach work.  Too bad, as, while it isn’t very elegant, it works well!

Yet another good argument to move to MOSS.


  1. By ‘altering the URL’, I meant that I just hacked the actual URL in the address bar.  However, you’d probably want to have the URL dynamically generated as a link in some preceding page, so yes, that would be a Designer task.
  2. As you can see above, this same approach won’t work with 2003, but if you look at the code snippet from 2003 that I give, it might give you clues on how to make it work.  I’ve never had reason to figure this out for 2003, so I just never spent the time on it.
  3. hey!
    but wat can i do if i want to prepopulate fields in newform.aspx in Sharepoint portal2003//
    please in despair
  4. It works fine for one field but when I tried to use it for multiple fields only the last one is getting pre-populated. Is there something that I should do?

  5. Marc,
    Excellent script. I am trying this script on our SharePoint 2010 list form, it does alert but none of the form fields are getting prepopulated. What did I miss?


    • Snehal:

      It’s very hard to say what you’ve missed. As with all examples, you’ll need to debug it in your environment. Note that this is a *very* old post and things may have changed in 2010 and with updates.



Have a thought or opinion?