I have a Web Part Page with multiple DVWP/New Item Forms for different lists. Each form contains a lookup field to one central list. I can get one form to auto-populate the lookup field with a query string using the fillDefaultValues script, but the other forms don't respond even when there are multiple scripts on the page.

Are there any tweaks to make to the script to populate multiple forms?

    <script type="text/javascript">

// This javascript sets the default value of a lookup field identified 
// by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable
// identified by <<QUERYSTRING VARIABLE NAME>>

// Customize this javascript by replacing <<FIELD DISPLAY NAME>> and 
// <<QUERYSTRING VARIABLE NAME>> with appropriate values.
// Then just paste it into NewForm.aspx inside PlaceHolderMain


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];

function setLookupFromFieldName(fieldName, value) {
  if (value == undefined) return;
  var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);

// if theSelect is null, it means that the target list has more than
// 20 items, and the Lookup is being rendered with an input element

  if (theSelect == null) { 
    var theInput = getTagFromIdentifierAndTitle("input","",fieldName);
    var opt=document.getElementById(theInput.opt);
    setSelectedOption(opt, value);
    OptLoseFocus(opt); //this function is provided by SharePoint 
  } else {
    setSelectedOption(theSelect, value);

function setSelectedOption(select, value) {
  var opts = select.options;
  var l = opts.length;
  if (select == null) return;
  for (var i=0; i < l; i++) {
    if (opts[i].innerText == value) {
      select.selectedIndex = i;
      return true;
  return false;

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[i].id;
    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
  return null;


1 Answer 1


The getTagFromIdentifierAndTitle function returns after it finds the first matched element, so yeah, it would only set the first lookup. I would try jQuery to do this as jQuery will return an array of matched elements. Mark Rackley has a setLookup function which you can find here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.