1,804 articles and 14,986 comments as of Sunday, June 12th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Monday, December 6, 2010

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.

 

Please Join the Discussion

21 Responses to “Poll for SharePoint”
  1. AdrienM says:

    Hi, thanks for this post :)

    Where are store the results? Via Google?

    Thx

  2. Steven says:

    Download link not working

  3. This is great, Alexander!

    M.

  4. Tesfaye says:

    Awesome job! Worked for me well and I am going to demonstrate this to my team.

    Thank you!!

  5. Frank says:

    This cool and I may be able to use on one of our surveys.
    Thanks

  6. Detlef says:

    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.

    • 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.

      • Detlef says:

        Doh! Thanks, it works now. I am feeling very small at the moment for overlooking this detail.

  7. David says:

    Any options for https environment?

  8. Elena says:

    This is great! Thanks and v 1.2 fixes the issue with single response. Just another minor problem when the title includes a .
    For me changing this line
    chartImgBuffer.push(”&chtt=”+encodeURIComponent(pollInfo.question).replace(/ /g,’+').replace(//g,’|')); // Title
    to
    chartImgBuffer.push(”&chtt=”+encodeURIComponent(pollInfo.question.replace(//g,’|')).replace(/ /g,’+')); // Title
    fixes it.

  9. Shri says:

    I was wondering how I could create something like this in SharePoint without a custom web part. This works amazingly well! Thanks :-)

  10. PJaxon says:

    Any options for anonymous access?

  11. PJaxon says:

    Great code, but the way. Thanks for putting this together. I’m going to tinker around with it to customize it a little… and if I come up with anything worthy I’ll post it here in case it helps others. Cheers ~ pj


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!