Quantcast
Viewing all articles
Browse latest Browse all 62

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">

_spBodyOnLoadFunctionNames.push("fillDefaultValues");

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[i].split("=");
    var temp = unescape(nameVal[1]).split('+');

    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];
    alert('fillDefaultValues: vals[nameVal[0]]=' + vals[nameVal[0]] + ' nameVal[0]=' + nameVal[0] );
  } 
  setValueFromQueryString(nameVal[0], vals[nameVal[0]]);
}

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[i].id;
    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
                alert('getTagFromIdentifierAndTitle: tags[i].id=' + tags[i].id + ' tags[i].title=' + tags[i].title + ' tags[i]=' + tags[i]);
      return tags[i];
    }
  }
  return null;
}
</script>

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):

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

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.


Viewing all articles
Browse latest Browse all 62

Trending Articles