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.

Wednesday, January 27, 2010

Good stsadm reference

I just wanted to post a link to this stsadm reference document:
http://www.helloitsliam.com/Downloads/Articles/Mindsharp%20STSADM%20White%20Paper.pdf

The stsadm is a powerful tool if used correctly.

Tuesday, June 2, 2009

Outlook 2007 Service Pack 2 issues

So we just had a very interesting outlook issue that it took a while to find the answer to. We noticed that we were unable to expand distribution groups to there individual email names. Then we noticed that we couldn't add accounts to our outllok clients. After more troubleshooting we found that it was only effecting computers with outlook 2007 service pack 2. Once we narrowed down the issue I started to do some reasearch. And I came across the following KB article from Microsoft: http://support.microsoft.com/kb/969519/. the article explained all of the errors that we were having. We used method 2 to modify the exchange server registry and everything went back to working correctly. Apparently the root of the problem was the exchange server set to use DSProxt instead of the Referal service, and after Outlook 2007 service pack 2 it can no longer use the DSProxy. Well I am ust glad we found the error. It took a while so I thought I would add another link to the solution.

Friday, April 24, 2009

Another Test Passed

A few months ago I was given the opportunity to take a free version of a new type of Microsoft test. In return I received three free test vouchers. So I decided to start working on getting my Server 2008 certifications. And here are the results of my first test:


So I have added another certification. I am hoping with our current migration to Server 2008 and Hyper-V that I will be able to get a lot of experience working in Server 2008.

Friday, January 9, 2009

A Few Solutions to Recent Problems

So I have had a couple strange problems recently that I thought I would post the solution to. I had several computers that wouldn't install automatic updates after XP service pack 3 was applied. I found that the way to solve this was to re register a dll file. If you just paste this: 'regsvr32 wups2.dll' in the run coomand box (start->run), everything then works great with automatic updates.

The other problem I had was with SharePoint v3. I set up a new SharePoint server for the company I am now working for. I setup a few simple things and was ready to start granting access to the main site. When I tried to add users or groups from active directory, I got the following error code: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT)). I search for quite some time and tried many things but none of the solutions seemed to work. Then I came across this post: http://prequest01.wordpress.com/2008/11/27/unable-to-add-active-directory-users-to-the-sharepoint-groups/ . It solved my problem right away. I am not sure why this option was checked on the SQL server and I hope it does not crash any of our other database apps. It is unfortunate the Microsoft caused SharePoint to produce errors when inserting into the database.

Friday, November 7, 2008

So it has been a while since I posted but my life has been pretty busy. I found a new job working for a company called Western Electronics. I work as computer support/report writer. I also had a new baby girl at the end of September. You can see the details of that over at http://cbpeterson.blogspot.com

Anyways the fun continues and hopefully I will be able to start posting some technical things. I did recently get a new laptop.  I purchased a Dell Studio 17 laptop. So far I like it. I am able to play blu-ray disks on it which has been a lot of fun. I am hoping to try my hand at creating a new website for myself. The main reason I got the laptop was for my month of teaching I did at Brown Mackie college. I taught the intro to computers class for October. They didn't need me this month but they may contact me in the future. 

Saturday, June 21, 2008

A Unwelcome Surprise

So this week I recieved a shock. On Tuesday we recieved an email about a mandatory company meeting. When I showed up there were people from all different departments in the meeting. We then proceeded to find out that we were all being laid off. They reduced the IT staff to a skeleton crew. They claimed that the TIC buisness was not generating enough revenue. I have a feeling that it was the home building part of our buisness though. It has been losing money left and right. Anyways I am back out on the jobn search now. Hopefully I can find something soon. Until then mabey I can try and learn a few new skills.