After one day’s play around, I wrote this javascript which fill in the survey by parse the query string. There is a jquery function ($(‘#aspnetForm’).fadeTo(“fast”,0.33)) to fade the whole page so you can comment it out.

I only used textbox field and checkbox but you can extend the function easily.

<script>

_spBodyOnLoadFunctionNames.push("fillValues");


function fillValues() {
  //jquery to fade opacity
  $('#aspnetForm').fadeTo("fast",0.33)

  var qs = location.search.substring(1, location.search.length);
  // alert(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(nameVal&#91;0&#93; + " " + nameVal&#91;1&#93;);    
  } 

  if(setTextFromFieldName("Question1", vals&#91;"question1"&#93;))
  {
    simulateButtonClick("diidIOSaveItem");
  }
}

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

function setTextFromFieldName(fieldName, value) {
 if (value == undefined) return false;
   
 var theInput = getTagFromIdentifierAndTitle("input","TextField",fieldName);
 theInput.value=value
 return true;
}

//To use this function: setCheckboxFromFieldName("Checkbox", 0);
function setCheckboxFromFieldName(fieldName, value) {
 if (value == undefined) return;
   var theInput = getTagFromIdentifierAndTitle("input","BooleanField",fieldName);
theInput.checked=value;
}


function simulateButtonClick(name){
  var tags = document.getElementsByTagName("input");

  for (var i=0; i < tags.length; i++) {
   var tempString = tags&#91;i&#93;.id;
   if(tempString.indexOf(name)>0)
   {
     tags[i].click();
    }
  }
}
</script>
Advertisements