Poll for SharePoint
Guest Author: Alexander Bautz
SharePoint JavaScripts
This code lets you generate polls using JavaScript/jQuery only. No need for server side installed WebParts.
Poll

Result with bar chart

Result with column chart

Result with pie chart

The charts are generated using Google Chart Tools / Image Charts (aka Chart API).
Create a custom list with the following fields
- Answer: Single line of text
- Question: Single line of text
Name it anything you like, but keep the display name fairly simple (no special characters) as you will use the display name in the CEWP code.
CEWP code
The CEWP code below refers jQuery from Google. If you have a local copy of jQuery you can change the script src. You find the code for the file “PollForSharePoint.js” at the bottom of the page. In this example the file “PollForSharePoint.js” is placed in a document library named “Javascript”.
NOTE: You must change the script src for the file “PollForSharePoint.js” to point your instance of the file – the CEWP code will not work unless you do this.
Place this code where you want the poll to appear:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/PollForSharePoint.js"></script> <script type="text/javascript"> buildQueryWP({pollAnswerListName:'PollA', id:'myPoll_23-11-2010', // Allowed characters id a-z, 0-9 - and _ start:'11/22/2010', // format: mm/dd/yyyy end:'11/25/2010', // format: mm/dd/yyyy singleResponse:false, q:"What is your favorite junk food?", a:['Pizza','Hot dog','Hamburger','Neither of them'], color:['32CD32','FFCC11','FF3300','C0C0C0'], chart:'bar', // bar, col or pie height:150, width:450}); </script>
Object attributes explained
- pollAnswerListName: DisplayName or GUID of the list that stores the answers
- id: The unique id of the poll. All poll answers are stored in a list and this id is used to separate each poll
- start: Start date in the format mm/dd/yyyy
- end: End date in the format mm/dd/yyyy
- singleResponse: true for one reply per user, false for unlimited number of replies
- q: Poll question. To have a linefeed in the question, use <br>
- a: Answers in an array format
- color: Colors for the chart in an array format. This must have the same length as the previous parameter – one color for each answer
- chart: “bar” for bar chart, “col” for column chart and “pie” for pie chart
- height: Height in pixles
- width: Width in pixles
Download code
The code for the file “PollForSharePoint.js”:
Download code from this location
Limitations:
Charts will only work in http:// environment and NOT https:// due to the charts being generated using Google Chart Tools / Image Charts (aka Chart API). In an https:// environment the result will be shown, but not as a chart.
Ask if anything is unclear.
Alexander
Guest Author: Alexander Bautz
SharePoint JavaScripts
Alexander Bautz is a SharePoint consultant/developer (mainly JavaScript/jQuery solution) living in Norway. Alexander spends a lot of his spare time blogging on the same topics. His focus area is "end user customizations" with no (or as little as possible) server side code.
Hi, thanks for this post :)
Where are store the results? Via Google?
Thx
Hi,
The results are stored in a SharePoint list – the instructions are in the article.
Alexander
Oh yes, in red… I never read title ;) Sry
Thx
Download link not working
Just checked. Seems to be available now. Please try again. — Mark
I’ll try from home. Just tried from work with no luck.
Cheers
Steven
This is great, Alexander!
M.
Thank you!
Alexander
Awesome job! Worked for me well and I am going to demonstrate this to my team.
Thank you!!
This cool and I may be able to use on one of our surveys.
Thanks
I installed v1.1 and get the following error on SP2007. Any pointers on how to debug it?
status: 500 responseText: soap:ServerException of type ‘Microsoft.SharePoint.SoapServer.SoapServerException’ was thrown.Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). webserviceUrl: /teams/detlef/_vti_bin/lists.asmx soapBody: PollA0
That probably means that the value that you’re providing for pollAnswerListName isn’t correct. The error messages for the Web Services aren’t the best.
M.
I am using his example as is, so the id is the same
pollAnswerListName:’PollA’,
id:’myPoll_23-11-2010′ ,
So I am not sure what to do?
Thank you Marc,
I’ve been busy today and haven’t had the time to answer.
Alexander
Did you create a list called PollA? Also, looking at the script, your list needs to have a column named “Question” and another called “Answer”. These two columns probably should be Single line of text.
M.
Doh! Thanks, it works now. I am feeling very small at the moment for overlooking this detail.