Sunday, August 30, 2009

Creating and working with Survey in SharePoint - Part 2

Hi All,

Here I am back with some more interesting stuff in survey list. In previous post we discussed about the advantages of survey list. But it is not true in every case. It has got many other limitations as well. Here in this article, we are going to discuss about one basic difficulty about survey.

I would first suggest you to read Creating and working with Survey in SharePoint - Part 1 of the series.

For further reading read Creating and working with Survey in SharePoint - Part 3

Creating and working with Survey in SharePoint - Part 4

Creating and working with Survey in SharePoint - Part 5

If you want to have some HTML in survey’s question, then we cannot insert that. Sometimes we may require making some font bold, something italic and we want to underline something. We need to increase the font size, we may require to add hyperlink in question, then we cannot do it.

So anyways we have to overcome this problem anyhow. So here is a way how you can insert HTML in the question of survey.

First make a note of one very important point, you have to repeat these steps for each and every page that you have for the survey. Means if you have branching question then you get more than one page to respond.

Before getting actually in to this, you first need to understand that how we will replace the normal text with HTML text.

Ok, let us go back to our first question. Open the question and add the following words before and after Department.



See the effect of this change on questions. We can clearly read startitalic and enditalic in first question.



We have added these words and we will replace this word with startitalic with <i> and enditalic </i> in content editor web part. You can use whatever word that you want, we only need to replace those words in the web apart. Let us see how we will do this now.

Go ahead and add one content editor web part below the questions. Edit the page and add the content editor web part.

Click on modify shared web part, click on source editor and write the code as shown in below.

<script language="JavaScript">
var ClassName;
ClassName = 'ms-formlabel';

var elements = new Array();

var elements = document.getElementsByTagName('td');

for(var e=0;e<elements.length;e++)
{
if(elements[e].className == ClassName)
{

elements[e].innerHTML = elements[e].innerHTML.replace('startitalic','<i>');

elements[e].innerHTML = elements[e].innerHTML.replace('enditalic','</i>');


}

}

</script>



Let us have a look on this above code. Here what we have done is that we have first taken out ms-formlable class as questions belong to this class in SharePoint architecture. We then take all ID because all questions are in TD tag and then we find our forcefully entered two words and replace them with proper HTML tag and there you go. See the effect of the above code.



Let us go ahead and change our third question. Make the following change in it.



Now again go ahead and change the existing editor web part with following addition with above code.

elements[e].innerHTML = elements[e].innerHTML.replace('linebreak','<br/>');

elements[e].innerHTML = elements[e].innerHTML.replace('startunderline','<u>');

elements[e].innerHTML = elements[e].innerHTML.replace('endunderline','</u>');


The effect of the above code is shown below. We have also broken question in two lines.



Now let us go and create hyperlink in the same question. Here is a trick to add hyperlink to the question. Let us say for example we want to have a hyperlink on “like” word and that should take to the Home page link and it should open in new page. In addition to this, it should be of bigger size than other words.

So here is a simple answer of the tuff question. Add just one more line to your content editor web part.

elements[e].innerHTML = elements[e].innerHTML.replace('like','<a href="/sites/Test/default.aspx" target="_blank" style ="font-size:14pt" >like</a>');


and see the effect of above code.



I hope that now you have the clear picture. All it takes is the knowledge of HTML and you can insert anything, almost anything with HTML in the questions.

I will soon come up with part 3. keep reading it.

64 comments:

Rohit agrawal said...

Hi,
I am not clear as to wher do i add this content editor web part. When you mention to add it below questions.. but the "respond to survey" or "NewForm.aspx" page does not have edit page enabled.

Anonymous said...

Hi There

Just wondering how do you add the content editor web part to the questions??

Thanks
K

SharePoint Kings said...

Rohit,

check this link http://www.sharepointkings.com/2008/05/how-to-edit-list-forms-like-newformaspx.html

we think you will able to edit page with this trick.

Rohit Agrawal said...

Thanks. With the above trick i was able to add content editor web part.

Rohit Agrawal said...

Hi,
By default survey choice menu has one answer on separete line like:
Very good
Good
bad

Is it possible to keep all the options in single line like:
Very Good Good Bad.

Thx.

Rohit agrawal said...

Hi
Adding the content editor web part works fin for NewForm.aspx.
But for responses as graphical summary, the keywords are not replaced by html. do i need to do anything else?
I have added content editor web part to graphical summary page with same script what i used in newform.aspx pls advise. thx.

Rohit Agrawal said...

Hi, Any answers to above two questions?

Anonymous said...

Hello chaps, please can someone let me know how long it will take (no.of days) to setup this out of the box Sharepoint Survey? (without branding for a specific requirement)

Thanks
Thomas

SharePoint Kings said...

@Rohit:

sorry, no time to work on horizontal options.

@Anonymous: try to provide link on default page which will redirect to geographical summery page.

Cindy Tan said...

Hi,
Great articles!

I manage to modify font color using html code.

1) Do you have idea how to customise the rating scale. Example it has 5 number range for user to select like "Very Satisfied", "Satisfied", "Neutral", "Dissatisfield" and "Very Dissatisfied". But in the system, it only have 3 text filed to key in. So I key in "Very Satisfied", "Neutral" & "Very Dissatisfied". May I know how to add in for all range?

2) How to put some background color for the Rating Scale type?

Thanks

Regards
Cindy Tan

SharePoint Kings said...

Cindy,

Not getting your question can you elaborate?

Cindy Tan said...

Hi,

Type of answer: Rating Scale
Number Range: 5
Range Text: [It only have 3 text box for us to enter, I put Very Satisfied; Neutral; Very Dissatisfied ]

Output for this rating scale question, it will come out the 1,2,3,4,5 and follow by range text on top.

Let say, I just want the range text come out instead the 1,2,3,4,5. May I know how to remove the number range?

Secondly, I would like to put 5 values of range text instead of 3 only?

Thanks

Regards
Cindy Tan

TEST said...

Is there any way to edit the choices and not the questions themselve? Like for example my question is:

Who is the greatest?
Ammar
You
Jordan

So you have three choices right, so is there any way you can edit Ammar to be in bold, and You to be in Italics and so on. I tried doing it but couldn't get anywhere. Thanks for your help.

SharePoint Kings said...

Cindy,
still not getting your exact question but what we understand is you want more then 5 text box then 3 for explaining?

if that is the case then we think that is not possible, still we are not sure and we are working on it

regarding back ground color you can put content editor webpart and find the DIV and change the back ground color but it will be kind of hard code.

SharePoint Kings said...

Test,

same answer as cidy,

you can use content edition webpart and in javascript find particular div or control change its style but it become kind of hard code.

that first one is Bold then second one is italic etc..

Anna said...

Hi,

I have added the script in the CEWP in order to use line breaks in survey questions, which worked perfectly, but it only working for the first instance of "linebreak" in the question. My hope was to insert many line breaks, which I haven't quite figured out.

Any help would be much appreciated!

SharePoint Kings said...

Hi Anna,

If you want to insert many llinebreaks, then the idea is you have to write the word "linebreak" as many times as you want at appropriate places (whereever you want) and then in script, write down the same line which replaces the "linebreak" word with "\n" same number of times as the word "linebreak" appears in your question.

Let us know if this solves your query.

Anonymous said...

Hey guys!

I have a problem with DispForm.aspx in the survey I modified. If I do not add any script in form then text appears like that “over1 RISIKO FOR ULYKKER over2” (sorry it’s in Danish and I use over1 and over2 instead of startitalic and enditalic). If I do add a modified script then things go wrong: “x0020_RISIKO_x0020_FOR_x00>over1 RISIKO FOR ULYKKER”

Script I used:
script language="JavaScript"
var ClassName;
ClassName = 'ms-standardheader';

var elements = new Array();

var elements = document.getElementsByTagName('h3');

for(var e=0;e<elements.length;e++)
{
if(elements[e].className == ClassName)
{

elements[e].innerHTML = elements[e].innerHTML.replace('over1','i');
elements[e].innerHTML = elements[e].innerHTML.replace('over2','/i');
}

}
/script

I noticed an “a” tag inside the html code. I think it’s guilty of massing the text up.
a name="SPBookmark_over1_x0020_RISIKO_x0020_FOR_x00"/



Is there a solution?

SharePoint Kings said...

by default sharepoint will take _x0020_ for space (" ") in column name.

and in DB that so called column will be saved with some limited characters. so you are having this problem.

change the column or your can use start with also to find column.

filthy gorgeous said...

This is very nice and clear, something I haven't been able to resolve myself for months now! Thanks for putting my mind at ease! You guys are the SharePoint Kings!!!

Anonymous said...

Hi guys, just to check, does this workaround apply to sharepoint 2003? been trying for ages adding the code in the content editor web part but doesn't seem to work :(

SharePoint Kings said...

Anonymous,

not tested on 2003 and it will not work

what we are doing is we are just working with objects from JavaScript
so only work in default condition

Bryan said...

Hello,
I was able to change formatting in survey questions no problem. I also found how to insert a hyperlink and it works great.

My only other question is that the survey contains a lookup list box but it is not wide enough. I wanted to try to use the innerhtml replace method to change 'width:143px' to 'width:300px' but cannot get it to work. Can you please help? Sample code below.

td class="ms-input">

div style='width:143px;height:125px;overflow:scroll'>

select name="ctl00$m$g_6eee9ec7_e15c_4d7a_9e47_ca80b9772ac9$ctl00$ctl01$ctl01$ctl00$ctl00$ctl04$ctl00$ctl00$SelectResult" id="ctl00_m_g_6eee9ec7_e15c_4d7a_9e47_ca80b9772ac9_ctl00_ctl01_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_SelectResult" multiple="multiple" title="Who requires this? selected values" onchange="GipSelectResultItems(ctl00_m_g_6eee9ec7_e15c_4d7a_9e47_ca80b9772ac9_ctl00_ctl01_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_MultiLookupPicker_m);" ondblclick="GipRemoveSelectedItems(ctl00_m_g_6eee9ec7_e15c_4d7a_9e47_ca80b9772ac9_ctl00_ctl01_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_MultiLookupPicker_m); return false" onKeyDown="GipHandleHScroll(event)">

/select>/div>

SharePoint Kings said...

Bryan,
Use "IE developer toolbar" in IE or firebug in Mozilla and check which div style you have to change. because there might be some other style has been applied.

Peter said...

This is a great workaround to add the HTML to a survey. Is there a way to add code to the CEWP that will "Finish" the survey that the hyperlink is in? I'm having problems with my users clicking the link and forgetting to go back and click "Finish" on the survey.
TIA

SharePoint Kings said...

Peter,

in survey we can only add javascript... any solution from javascript can be implement.

Deep said...

It worked beautifully..! I had added content editor web part on top of the page and it was not working earlier but now realized it needs to be at the bottom so html replacements can happen. So it works just fine now.
Thanks.

Jay said...

Hi,
As per continuation to Cindy's questions here..... I have same question , you said you did not understand Cindy's question so here goes..

Survey's Rating Question when we create , it has Three TextBoxes to put values of Rating Header.
eg. Low , Medium , High

I want to have 5 Header instead of Three ,like VeryLow, Low, Medium, High, VeryHigh

How do I do that?

ollyhodgson said...

@Rohit:

To get the script to work on the graphical results page (summary.aspx), you need to change the ClassName to 'ms-vb' in the CEWP, from 'ms-formbodysurvey' (assuming that's the class you have on the newform.aspx page).

Rgds, Olly

SharePoint Kings said...

Thanks Olly

Diggermom said...

I'm trying to get the script to work on the graphical results page (summary.aspx), I changed the ClassName to 'ms-vb', but it doesn't seem to be working. When I view the source code for the summary.aspx page, it appears the ClassName is 'ms-standardheader, ms-vb', but when I try making that change in the CEWB that doesn't seem to be working either. Any suggestions?

SharePoint Kings said...

Diggermom,

First try to change some other style from the page or hide something from page with javascript in CEWB.

so you know whether javascript is firing from it or not?

Psyche said...

Hi - this whole series of articles is brilliant, thank you so much! I do have a question.

Using MOSS2007, and have a survey with branching questions so that each question is on a spearate page.

When I add a content editor web part to the first question (ie first page), then it works fine. I have added 3 content editor web parts - 1 to add some explanatory text to the question, 1 to control the formatting, and 1 remove the save and cancel buttons.

On the next question, to do the same. However, what ever I put into Question 2 (the second page), is replicated on all subsequent pages and questions, which is fine for the scripts, but not for comments, which need to be different for each question.

SharePoint Kings said...

@Psyche,
we are not getting you question?
you want to say, Javascript will be fire on first question but no second question???

can you please elaborate?

Psyche said...

Hi SK,
Sorry that I was not clear.

On the first page, Content Editor Web Part 1 (CEWP1) contains text. On the same page CEWP 2 contains script to remove save and cancel button. This all works on the first page perfectly, but does not affect the other pages (which is good)

On the second page CEWP1 contains text and CEWP 2 contains script to remove save and cancel button. This all works perfectly on page 2, which is good. However, when I go to page 3 to edit the page and add the CEPW with text and the CEWP with script, they are already there, copied from page 2.

I would like to have each page with its own CEWP and its own text etc.

Psyche said...

Hi SK,

Sorry for not being clear. What I mean is that CEWP on the first page affects the first page only, while CEWPs on the second page affect all remaining pages.

I would like to have different CEWPs on page 2 and page 3 - is it possible?

SharePoint Kings said...

Psyche,

we got you.

actually this is the way sharepoint will process.

you can do one thing,
find some thing in URL so that you can separate each question/ page

then in JavaScript check URL and find your case and then keep your all you JavaScript of each page in separate that case.


may be this work.

we try to find some other solution also if we get time.

Psyche said...

It's a good suggestion - I will look at that - thank you. (Keep up the good work :) )

Anonymous said...

Once I finally figured out your directions, this seemed like the perfect solution to my problem. However, I am not getting the proper results. In my survey, I am using questions with rating scales for the answer. The problem I have is that the subquestions do not contain a line break between them so it is very confusing to the reader. Following your directions, I have added the word linebreak at the end of the question and added the code in a CEWP to replace linebreak with
. However, there is no separation between the questions and the word linebreak still appears in the question.

Thanks,
Terry

SharePoint Kings said...

Anonymous (Terry),

check you HTML from view source or by alerting innerHTML of main control

render it on separate html and see does your changes reflects or not and see which changes you need to do.

Erin said...

What are the "magic words" to get HTML to show? For example, you put "beginitalics" and "enditalics" around the word you want to italicize. What do you put around the words you want to convert to an HTML link?

I can get the link to work in the content editor web part, but not in the question itself. Thanks!

SharePoint Kings said...

Erin,

if you check javascript, we are just replacing beginitalics to enditalics to so it will show text as italic you can do bold also

for hyperlink you have to add put some placeholder that you can find it in javascript and replace with your logic

Anonymous said...

Hello,

I was able to format the "question" text but what about the text users see for the selections inside the question?

For example you had a multiple choice question with radio button choices for Hindi and English. How would you format the radio button text.

I was trying to create a survey where you could use choice menus to vote or choose a picture for a photo contest. It would be so nice to be able to display the image inside the survey next to the text instead of using text to describe the photos that they could vote on.

SharePoint Kings said...

Anonymous,

check HTML how it renders and change it the same as question change, you can add div with image hard coded

we had not done it but you can give it a try

Pedro said...

To be able to see the HTML formats in summary.aspx:

script language="JavaScript"
var ClassName;
ClassName = 'ms-standardheader';

var elements = new Array();

var elements = document.getElementsByTagName('h3');

for(var e=0;eover1 RISIKO FOR ULYKKER
What you can do is replace over1 to over1 << with a space:
elements[e].innerHTML = elements[e].innerHTML.replace('over1','i');
elements[e].innerHTML = elements[e].innerHTML.replace('over2','/i');

I think this may work!

Anonymous said...

hi,
I have a requirement wherein I need to add few sub headings between teh survey questions.
How can i do this. There is constraints that I cannot use Javascript or Sharepoint Designer.

Parth Patel said...

@Anonymous,

don't think that is possible without javascript or SPD.

may be custom webpart can be the last option.

Bill Fellner said...

Hello,

I have added Content Editor Web part to the NewForm.aspx page for submitting a response to a survey. I pasted the content in the source box, make sure it was cleaned up, and applied everything.

It is not working. I'm still seeing the words "startitalic" & "enditalic" in the rating scale questions.

I'm on MOSS 2007. Thoughts?

SharePoint Kings said...

Bill Fellner,
its for moss 2007 only.

just check you might be missing something.

also check whether JavaScript is firing or not and if firing check by putting alert that which value is available in innerHTML.

Anonymous said...

I am having the same issue, where the term linebreak still appears in the question. I have checked the java code and ita all appears okay. You say to "check whether JavaScript is firing or not and if firing check by putting alert that which value is available in innerHTML. " How do you do this?

SharePoint Kings said...

Anonymous,

you have to do this by putting alert in JavaScript. so if JavaScript fires then you will get alert.

not check the values you are setting and getting so that you come to know which value will be replaced by which value.

Jackie said...

Hi: For the hyperlink, I was only able to get the first survey question to work. How do I hyperlink different words in different questions? Do I use only 1 cewp or multiple cewps? Thanks
Jackie

SharePoint Kings said...

Jackie,

you can put javascript in same content editor web part or in different doesn't matter.
what you have to take care is that in JS find your specific element and replace it.

Steffy said...

Hi Sharepoint King... I have followed all your instructions on how to add hyperlinks, but it is working on some questions and on others its not, and on some words and not others. For example, my Question one has 3 hyperlinks: 2 are working and one isn't hyperlinked at all... on my question 2, it is not working, on my 3 it is, and so on... is there anything specific I can check?
Thanks!!!
Ceci

SharePoint Kings said...

steffy(ceci),

please check view source how your question is rendering then check you javascript it might be some problem while comparing and replacing.

javascript is case sensitive while comparison so please take care else it will not be any problem

shally said...

Hi,

I have added the script to "Respond to this survey" page, and "show all resposes" page, it's working well. But when I added the same script at "Graphical Summary page", it's not working.
Please if you ccould suggest how to add teh same script to that page too.

Thanks in advance.

SharePoint Kings said...

shally,

we had not done this in Graphical Summary page.

we are tide up with some work now a days, but we will surely find some work around for this.

Ming Hsueh said...

Why doesn't this work?

elements[e].innerHTML = elements[e].innerHTML.replace('','');

elements[e].innerHTML = elements[e].innerHTML.replace('
','
');

SharePoint Kings said...

Ming Hsueh,

just try to put alerts to check elements or some object are getting value or null.

hsueh010 said...

Why do we need to use the words startItalics and endItalics, why can't we simply use the and

SharePoint Kings said...

hsueh010,
by default SharePoint will store you text by encoding HTML so it wont reflect as italic.

you can try it out and check the result it self.

Anonymous said...

Hey! this has been really helpful for sure :) However, I have a question - After adding PAge Separator to your questions - we only see the following 3 buttons: Next, Save, Cancel. Is there a way to add a "Previous" button as well? So respondants can go to the previous question and double check or edit their answers?

Any suggestions would greatly be appreciates!!! Thanks!

SharePoint Kings said...

@Anonymous,

you can find button in JS, add previous button over there,
and on click you can use browser history back functionality.

to get browser history back functionality do some google its a simple one liner JavaScript.

Keyur said...

Hi Kings,

Hope you are doing. I was referring your survey article part 2.
http://www.sharepointkings.com/2009/08/creating-and-working-with-survey-in_7080.html

For me, summary.aspx is not working.

can you please help?

Thanks,
Keyur




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com