Eric’s adventures in Sharepoint, technology, and life.
Email icon Home icon
  • Using Query String Parameters and Surveys pt2

    Posted on May 14th, 2009 Eric No comments

    In part 1, I showed how to pass a query string parameter to a text or number field in a survey. In part 2, I’ll show how to pass it to a look up field. This was utilized in our Employee Training modifications. The look up list for courses can get quite long and have the potential to be named the same, so we wanted to take the guess work out for the registrants.

    To set a query string on a look up list, perform the all the same steps as in part 1, but do not paste that code into PlaceHolderMain. There is a different script to use. Paste the below code into the PlaceHoldermain area, replacing the Field name and parameter names with yours on line 20.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    
    // 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
     
    _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];
      }  
      setLookupFromFieldName("<<FIELD DISPLAY NAME>>", vals["<<QUERYSTRING VARIABLE NAME>>"]);
    }
     
    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);
        ShowDropdown(theInput.id); //this function is provided by SharePoint 
        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].value == 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;
    }

    This helpful tip taken from the Sharepoint Designer Team Blog.

    Post to Twitter Post to Plurk Plurk This Post Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Stumble This Post