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

  • Using Query String Parameters and Surveys

    Posted on May 13th, 2009 Eric 4 comments

    Today, in part 1, I’m going to describe how to use a query string parameter within a Sharepoint survey. This can be important for several reasons with the most notable being providing users a hyperlink to submitting a new survey from a work flow email. The hyperlink can have parameters attached to it to pre-populate data that you may not want the user to have to change. In part 2, I’ll describe how to use query string parameters to set Lookup field values.

    The below steps are the same for using a text or number field or a lookup field. What will differ is the code used to populate the fields.

    1. Open the site in Sharepoint Designer.
    2. Navigate to the Lists menu and expand out the survey in question.
    3. Make a copy of NewForm.aspx and rename it to something else, Survey.aspx or Feedback.aspx seem more fitting than NewForm.
    4. Open the newly renamed page.
    5. Delete the stock form from the lower pane.  If you get an error trying to delete it, try expanding the chevron and selecting Default to Master’s Content. Alternatively, set the IsVisable control from true to false. Deleting it could cause problems down the road as noted here.
    6. Click the chevron again and select the Create Custom Content link to create a PlaceHolderMain Custom area.
    7. Click the PlaceHolderMain Custom content area.  Insert a Custom List Form into the page from the Insert -> Sharepoint Controls menu.
    8. Set the top drop down to your survey list and ensure the New item form is selected and click OK.
    9. Click the chevron of this form and click the parameters link.
    10. Create a new parameter with the source being query string and give the parameter a variable name.
    11. Paste the below script into the the PlaceHolderMain area, substituting the Field Name and the parameter name with your values on line number 30.
    12. 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
      
       
      _spBodyOnLoadFunctionNames.push("fillDefaultValues");
      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;
      }
      function setTextFromFieldName(fieldName, value) {
      if (value == undefined) return;
        var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
      theInput.value=value
      }
      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];
       }  
       setTextFromFieldName("theFormFieldtoPopulate", vals["myQueryStringName"]);
      }
    13. Save and Close the file. Test the form to see if it is in fact accepting your parameter. Navigate to the URL and append ?YourParameter=SomeValue to the end. When you hit enter, the new survey form should appear with the parameter value in the specified field.
    14. Go back to Sharepoint Designer and right click on the survey. Select Properties and click on the Supporting Files tab. Change the New item form to the newly created page by clicking the Browse button and navigating to it. Click OK to apply the change.

    Now this parameter can be leveraged in work flows that email users by creating a URL and grabbing a look up value from Sharepoint list data. This provides 1 click access to users to get to and fill out a survey. In part 2, I’ll describe a scenario where a look up column needs to be populated. For further information, see the Sharepoint Designer Team Blog for other use cases.

    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