SharePoint Survey – Randomise questions

My last post was about my disappointment in finding out that workflows cannot be used with surveys… I was trying to build a quiz that would display random questions from a pool and then automatically grade the quiz using a workflow. At this point, the grading won’t happen but I thought I’d share my code that I used to randomise the questions in the survey.

The code uses javascript to hide (display: none) the TDs that hold the questions and answers on the NewForm.aspx then display a set number of those Q&As randomly.

I haven’t done extensive testing, but it seems okay. I pasted the below after ‘</WebPartPages:WebPartZone>’ on my survey’s NewForm.aspx.

<script type="text/javascript">
//----------------------------------
//NUMBER OF QUESTIONS IN YOUR SURVEY
var numQs = 3;
//----------------------------------

//inArray function - thankyou http://itcave.net/javascript/inarray
Array.prototype.inArray = function ( search_phrase ) {
for( var i = 0; i < this.length; i++ ) {
if( search_phrase == this[i] ) {
return i;
}
}
return false;
}

//get TDs and hide all the questions and answers
var strX = document.getElementsByTagName("td");
var QAsetno = 1; //var for sequential question numbers
for(var i = 0; i < strX.length; i++) {
if(strX[i].className == "ms-formlabel") {
strX[i].id = "question"+QAsetno;
document.getElementById(strX[i].id).style.display = 'none';
i++;
if (strX[i].className == "ms-formbodysurvey") {
strX[i].id = "answerset"+QAsetno;
document.getElementById(strX[i].id).style.display = 'none';
}
QAsetno++;
}
}
QAsetno--;

var proposedQno;
var randQno=new Array();
for(var x = 1; x <= numQs; x++) {
//propose a random number and check if it has been used
do {
proposedQno = Math.ceil(Math.random()*QAsetno);
} while (randQno.inArray(proposedQno)!=false);

//at this point, the proposedQno will be unique so we can add it to the list of used numbers
randQno[x] = proposedQno;
//make the Q&A visible using the unique proposedQno
document.getElementById("question"+proposedQno).style.display = '';
document.getElementById("answerset"+proposedQno).style.display = '';
}

</script>

I was hoping I could do something similar (building a auto-graded quiz) using a custom list, but the layout on a list NewForm.aspx seems a lot more complicated for a one-fits-all solution such as this.

Anyway – I hope this helps somebody – enjoy!

5 thoughts on “SharePoint Survey – Randomise questions

  1. This code is exactly what ive been looking for to solve a problem i have. Unfortunately i am not an expert in javascript so i am unable to get this to work. I have inserted content editor webparts before which have worked. Unfortunately i dont have access to designer either. I just wonder if there are parts in this code that i need to alter before it will work?? So, I have a test survey with 7 questions and i want it to select 4 at random every time someone clicks respond to this survey. Any help would be appreciated

    • Hi Jonathan, this post is pretty old and was designed to work with MOSS 2007 – I haven’t test on later versions of SharePoint. I’m not an expert, but I think this code must be placed on the NewForm.aspx as it’s own code i.e. not in a content editor web part.

  2. Hello Sir,

    Do i have to change anything in the code above. Please let me know what needs to be changed. Your reply will be appreciated. It will be very helpful for me. I am looking out for the exact requirement. Please…………

    • Hi Ramanjulu, this code should work. Like I said above, I wrote this a long time ago for MOSS 2007. Which version are you using?

      • it is SharePoint 2010 and i just copied and pasted after “‘”, nothing i have changed. Please guide me.

Leave a reply to Ramanjulu Cancel reply