Showing posts with label PreSaveAction. Show all posts
Showing posts with label PreSaveAction. Show all posts

Friday, March 12, 2010

SharePoint Survey and PreSaveAction

So my company wanted to have employees take a quiz after viewing a PowerPoint slide about safety. So I created a new survey in SharePoint to accomplish that task. After completing the quiz they tested it and decided that they wanted the ability to have it graded. I thought no problem, I will just use a workflow. Then they added a new wrinkle, they wanted a message to be displayed when they clicked finish to show if they had passed and then only save the results if they pass. So I decided to go the route of JavaScript. After looking at several sites I came up with the following code(most of it was copy and paste from other sites):

---------------------------------------Begin Code----------------------------------------------
[script language="javascript" type="text/javascript">]
function PreSaveAction()
{
var questiononeselected = getTagFromIdentifierAndTitle("input","RadioButtons","Energy Control protects employees from injuries caused by:","value");
var questiononevalue = questiononeselected.value;

var questiontwoselected = getTagFromIdentifierAndTitle("input","RadioButtons","LOTO stands for:","value");
var questiontwovalue = questiontwoselected.value;

var questionthreeselected = getTagFromIdentifierAndTitle("input","RadioButtons","An “affected” employee is defined as:","value");
var questionthreevalue = questionthreeselected.value;

var questionfourselected = getTagFromIdentifierAndTitle("input","RadioButtons","An “authorized” employee is defined as:","value");
var questionfourvalue = questionfourselected.value;

var questionfiveselected = getTagFromIdentifierAndTitle("input","RadioButtons","Only “authorized” employees are allowed to remove Lockout tags.","value");
var questionfivevalue = questionfiveselected.value;

var questionsixselected = getTagFromIdentifierAndTitle("input","RadioButtons","Customers, vendors, and outside service personnel are not required to follow the Company’s Energy Control policy.","value");
var questionsixvalue = questionsixselected.value;

if (questiononevalue != "All of the above")
{
alert('Thank you for taking the quiz.\n Unfortunately you did not pass.\n Please try again after reviewing the presentation.')
return false;
}

if (questiontwovalue != "Lock out-tag out")
{
alert('Thank you for taking the quiz.\n Unfortunately you did not pass.\n Please try again after reviewing the presentation.')
return false;
}

if (questionthreevalue != "A person who operates, uses, or works around equipment that is receiving maintenance")
{
alert('Thank you for taking the quiz.\n Unfortunately you did not pass.\n Please try again after reviewing the presentation.')
return false;
}

if (questionfourvalue != "A person who performs maintenance on equipment")
{
alert('Thank you for taking the quiz.\n Unfortunately you did not pass.\n Please try again after reviewing the presentation.')
return false;
}

if (questionfivevalue != "True")
{
alert('Thank you for taking the quiz.\n Unfortunately you did not pass.\n Please try again after reviewing the presentation.')
return false;
}

if (questionsixvalue != "False")
{
alert('Thank you for taking the quiz.\n Unfortunately you did not pass.\n Please try again after reviewing the presentation.')
return false;
}

//debug alert
//alert('1. ' + questiononevalue + '\n2. ' + questiontwovalue + '\n3. ' + questionthreevalue + '\n4. ' + questionfourvalue + '\n5. ' + questionfivevalue + '\n6. ' + questionsixvalue);

alert('Thank you for taking the quiz.\n You answered all of the questions correctly.')
return true;
}
/*******
SharePoint Field Type identifier tagName Option
--------------------- -------------- ---------- ---------
Single Line of Text TextField input
Multiple Lines of Text TextArea input
Number TextField input
Currency TextField input
Choice (radio buttons) RadioButtons input
Choice (radio buttons) RadioButtons input value
Choice (dropdown) DropDownChoice select
Lookup (single)* Lookup select
Lookup (multiple) SelectCandidate; select
SelectResult
Yes/No BooleanField input
******/
function getTagFromIdentifierAndTitle(tagName, identifier, title, option) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i <>
var idString = tags[i].id;
var nameString = tags[i].name;
// get selected radio button value only
if (option == "value" && tags[i].type == "radio" && (identifier == "RadioButtons" && nameString.indexOf(identifier) == nameString.length - len)) {
var tagParentHTML = tags[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML;
if (tagParentHTML.indexOf('FieldName="'+title+'"') > -1) {
var radioButtons = document.getElementsByName(nameString);
var radioValue = "";
for (var x=0; x <>
if (radioButtons[x].checked) {
radioValue = radioButtons[x].parentElement.title;
break;
}
}
var o = document.createElement("INPUT");
o.type = "hidden";
o.value = radioValue;
return o;
}
}
// get radio buttons group
if (tags[i].type == "radio" && (identifier == "RadioButtons" && nameString.indexOf(identifier) == nameString.length - len)) {
var tagParentHTML = tags[i].parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.innerHTML;
if (tagParentHTML.indexOf('FieldName="'+title+'"') > -1) {
return document.getElementsByName(nameString);
}
}

// all other input or select type
else if (tags[i].title == title && (identifier == "" || idString.indexOf(identifier) == idString.length - len)) {
return tags[i];
}
}
return null;
}

[/script]
-----------------------------------End Code---------------------------------------------------

Some of the key things to notice are:

1. At the beginning you have the PreSaveAction function. This function is called every time someone clicks on the finish button. It is executed before anything is saved. If it returns true it saves the data, if it returns false it returns you to the survey(leaving all of your input) and nothing is saved.

2. The getTagFromIdentifierAndTitle function was found on the SharePoint Designer blog and is better described there: http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx. Basically it goes and gets the value of what is stored in the input fields. I found a modified version as the original didn't include radio buttons which were what I primarily used in my survey. It can be found here: http://sharepointmalaya.blogspot.com/2009/04/trigger-javascript-events-when-choice.html.

3. It also was important to pass the correct title to the getTagFromIdentifierAndTitle function. I found the titles by viewing the source of the page. It was found in a cooment that looks like this: Fieldname="Name of Field". It appeared right before the definition of each radio button. You then pass that name in as the title.

4. The rest of the code is mostly specific to the survey I created. It goes through and finds each of the input fields and then checks to see if they are correct. If any one of the answers is incorrect it sends them a message and returns them to the quiz. If they are all correct then it saves the quiz.

Well it was a fun adventure in JavaScript and I learned a few things. I am sure the code cloud probably be a little more efficient but it got the job done.