Poll for SharePoint


15.12.2011 v1.5.1: Bugfix in the file “PollForSharePoint.js” – line 238. The property “listBaseUrl” were missing – thanks to Jaap Dijkma for finding this bug.


04.12.2011 I have updated the solution to support putting the list in a central location. See “listBaseUrl” below.


22.10.2011 Updated the code to support free input rather than predefined answers – as requested by Claudia:

Hi, thank you for this wonderful piece of code, I think it’s great and we use it on our intranet :) .
I wondered if it is possible to use a single line of text for free input instead of radiobuttons with preset answers?
We like to use the poll for ‘brandtagging’ so that people can type keywords.
Just wondering…..
thanks! Claudia

I thought this was a clever idea and have implemented this in v1.4. See updated CEWP code and description below.


17.08.2011 Updated the code with these changes:

  • Lets you view the results and come back to answer.
  • Tidied up the code.
  • The charts now supports https (Thanks to Google).
  • Removed the poll question from the chart title to preserve the formatting.

01.03.2011 Updated the code to fix a bug when “singleResponse” is true. Thanks to “Al”. I have also included two more settings in the “argObj”: qStyle and aStyle. These sets the CSS style of question and answers. See CEWP code below.


28.11.2010 Updated the code to support special characters in the poll question and answers. Thanks to Christophe for the tip.


This code lets you generate polls using JavaScript/jQuery only. No need for server side installed WebParts.

Poll
IMG

Result with column chart
IMG

Result with bar chart
IMG

Result with pie chart
IMG

Result with “table chart”
IMG

Already answered
IMG

Not active
IMG

Finished
IMG

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.6.2/jquery.min.js"></script>
      <script type="text/javascript" src="/test/English/Javascript/Poll/PollForSharePoint.js"></script>
      <script type="text/javascript">
      
      buildQueryWP({pollAnswerListName:'PollA',
      				listBaseUrl:L_Menu_BaseUrl,				
      				id:'myPoll_01-03-2011', // Allowed characters id a-z, 0-9 - and _
      				start:'10/22/2011', // format: mm/dd/yyyy
      				end:'11/10/2011', // format: mm/dd/yyyy
      				singleResponse:false,
      				q:"What is your<br />favorite junk food?",
      				qStyle:'font-size:small;font-weight:bold;color:gray',
      				aStyle:'font-size:xx-small',
      				a:['Pizza','Hot dog','Hamburger','Neither of them'], // Leave empty for free input				
      				color:['32CD32','FFCC11','FF3300','C0C0C0'],
      				forceLowerCaseAnswer:false, // Group result by lowercase				
      				chart:'col', // table, bar, col or pie
      				height:100,
      				width:450});
      </script>
      
      

      You can control these values in the top of the file “PollForSharePoint.js”:

      var submitLinkText = 'Submit';
      var backLinkText = 'Back';
      var showResultBtnText = 'Show result';
      var pollNotActiveText = 'The poll is not active prior to {0}';
      var pollEndedText = 'The poll ended on {0}';
      var alreadyRespondedText = 'You answered: ';
      var totalResponsesLabel = "Total responses: ";
      

      Object attributes explained

      • pollAnswerListName: DisplayName or GUID of the list that stores the answers
      • listBaseUrl: The baseUrl of the site. This is like “/sites/hr” when the list is located in the site “hr” under “/sites”. Use L_Menu_BaseUrl (or omit the property) for current site. New in v1.5
      • 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>
      • qStyle: CSS syntax style
      • aStyle: CSS syntax style
      • a: Answers in an array format. To use free input and not predefined answers, leave the array empty.
      • color: Colors for the chart in an array format. This must have the same length as the previous parameter – one color for each answer
      • forceLowerCaseAnswer: Primarily for use with free input to avoid getting two “series” when the only difference are uppercase characters. New in v1.4
      • chart: “bar” for bar chart, “col” for column chart, “pie” for pie chart or “table” for a plain table.
      • height: Height in pixels
      • width: Width in pixels

      Regarding free input
      If you leave the attribute “a” as an empty array, the user can supply free text as “answer”. When using free input, the result are automatically presented as a table.

      Download code

      The code for the file “PollForSharePoint.js”:
      Download code from this location

      Ask if anything is unclear.

      Alexander

78 Responses to “Poll for SharePoint”

  1. Luis DP Says:

    Hi Alexander,
    Your solution is great and I am currently using it in a client’s intranet and they love it.

    I do have a question:
    How can I just have a numbers as a result set. The client want a ‘thumbs up’ x amount of people like it (like facebook) instead of the graph.

    Thanks,
    Luis

  2. DF Says:

    Alexander,

    I’m testing the SharePoint Poll for the first time.

    I have it working, however I have a question…

    After the results are displayed, short of refreshing the page, is there any way to get back to the poll?

    For example, if a visitor looks at the results first (without answering the poll), is it possible to place a back button on the poll so they can go back, re-read the question and possible answers, then place their vote?

    Thanks!

    • DF Says:

      Alex,

      One other question… The Poll works great in my SP2007 environment, but in my SP2010 environment I encountered a problem.

      Please see the link below.

      http://nivo.dev7studios.com/

      On my SP2010 site, I replaced my SP2007 sites static banner with a “Nivo Slider” web part. The slider is working fine and looks great.

      The problem is, as soon as I drop my “Poll for SharePoint” web part on the page, the slider stops loading/displaying images… And if I delete the Poll for SharePoint web part, the slider starts cycling thru images again.

      Can you image any reason why the Poll for SharePoint would interact with the Nivo Slider? Can you recommend a fix that would allow me to use both web parts on the same page?

      Thanks!

    • Alexander Bautz Says:

      Hi, I haven’t looked at it yet, but ensure you call jQuery only once.

      Alexander

  3. DF Says:

    Alexander,

    I have another “Poll for Sharepoint” question…

    Would it be possible for you to modify the code so the poll pulled it’s questions and possible responses from a SP list?

    For example, given a SP list that contained a column for questions, and several additional columns for each of the possible responses, could the Poll for SP web part be modified so it displayed the 1st question and 4 possible responses, then when the visitor submitted their response, the poll would graphically display the results, along with a “Next” button. If the visitor clicked the “Next” button, that would cause the web part to display the next question in the list… Thereby allowing the visitor to cycle thru and respond to multiple poll questions?

    Thanks!
    DF

    • Alexander Bautz Says:

      Hi,
      Everything is possible, but with the limited time i have, i feel this one is as good as it gets :-)

      Alexander

  4. zagreusmdMark Says:

    Hey. Nice bit of code. I’ve been playing with it, and I notice two things about the poll question:

    1) The CSS styles get applied to the question if the user has not entered a response yet, but not to the question when the results are displayed.
    2) Using as a line break in the question works on the initial display, but again it does not work when the results are displayed (the question appears thus: “This isthe question” — no line break.

    Right now my solution is to modify the JS to remove the question entirely, and simply display that in the HTML above the answers. Any tips on how I might apply the same styling to the title on the results screen as is applied on the question screen?

  5. Jon Says:

    Hi,

    I keep getting an error message saying:

    “One or more field types are not installed properly. Go to the list settings page to delete these fields.”

    What am I doing wrong?

    Cheers,
    Jon

    • Alexander Bautz Says:

      Hi,
      You have a wrong/missing field name. In the list you must create two columns: “Answer” and “Question”

      These has to be new columns – you can not rename the title field.

      Alexander

  6. Jon Says:

    Brilliant – works great. Thansk Alexander.

    Jon

  7. John Says:

    I tried using this technique under WSS 3.0. Under the single response mode how would the javascript code know what the current user id is. For me _spUserId is always null. If I set it to a valid user in the javascript code it works.

    • Alexander Bautz Says:

      Hi,
      The variable “_spUserId” is provided by SharePoint itself – trough the master page. Does your page inherit from the master page?

      Alexander

  8. asp.net, c#,javascript Says:

    asp.net, c#,javascript…

    [...]Poll for SharePoint « SharePoint JavaScripts[...]…

  9. Tom Says:

    Hi Alexander,

    This works great, but i’m getting a strange grey panel appearing at the top of my page and an error:

    Webpage error details

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; WOW64; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; InfoPath.2; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)
    Timestamp: Wed, 12 Oct 2011 13:18:14 UTC

    Message: ‘this.el.style.visibility’ is null or not an object
    Line: 109
    Char: 8
    Code: 0
    URI: http://intranet/WebResource.axd?d=_ok40Df4DyKI4FNoqkgVvLu94RIdKCcFQGEz6Q09yFrv-fIKWbnCk_CLe_bG7IUGInLq_A4apRMT8JeC-3GdvCXDUHknE5AgzgPO-iT7TEHpIasNutkn_DrH1UN92g4g4au3dGTN1avjrU2fbmsSPxqh5rDohZuHDLXaMuJzdHm2uZDp-dxCcz3d0M1EB7HQFTJii-1BiPewwlaow9lONYaHT0xTQE817uBLCM1eA9pB0xyU0&t=634177258816171641

  10. Claudia Says:

    Hi, thank you for this wonderful piece of code, I think it’s great and we use it on our intranet :) .
    I wondered if it is possible to use a single line of text for free input instead of radiobuttons with preset answers?
    We like to use the poll for ‘brandtagging’ so that people can type keywords.
    Just wondering…..
    thanks! Claudia

  11. Masood Says:

    How to enable anonymous access to this poll?

  12. Masood Says:

    I am getting this exception when anonymous user is trying to submit the volt?

    status:
    500

    responseText:
    soap:ServerServer was unable to process request. —> Access is denied. (Exception from HRESULT: 0×80070005 (E_ACCESSDENIED))

    webserviceUrl:
    /_vti_bin/lists.asmx

    soapBody:
    PollA0<![CDATA[What is yourfavorite junk food?]]>

    • Alexander Bautz Says:

      Hi,
      This solution uses webservices and cannot be used without authentication.

      Alexander

  13. AN Says:

    Hi, thanks for this really useful and easy to implement code. I’ve implemented it in the source code of a content editor web part, the poll appears (with the question and radio buttons) but when I click submit I get the error ” An error occurred! Null”
    Any thoughts? Thanks in advance
    AN

  14. Aarnout Says:

    Hello Alexander, great code, thanks!

    I have a question: is it possible to use a poll-list on another (sub)site?
    In my situation, I would like to have the poll on the home page and the list (with several views) on the HR-subsite of my intranet.
    In your code I saw something like listBaseUrl, but I don’t see how I can use it.
    Regards,
    Aarnout

  15. Jaap Dijkma Says:

    It won’t work with jQuery 1.7

    But with jQuery 1.6.4 it’s a very nice way to get a poll on a SharePoint site. Thanks!

    • Jaap Dijkma Says:

      little bugfix
      v1.5/PollForSharePoint.js line 238:
      res = spjs_QueryItems({listName:argObj.pollAnswerListName,query:query,viewFields:['ID','Answer']});

      should be:

      res = spjs_QueryItems({listName:argObj.pollAnswerListName,listBaseUrl:argObj.listBaseUrl,query:query,viewFields:['ID','Answer']});

    • Alexander Bautz Says:

      The bug is fixed in v1.5.1 – thank you for finding it.

      Alexander

  16. Kris Says:

    Alexander this is fantastic, what a useful tool for sharepoint :) thanks

  17. Andrew Says:

    Greatly usefull tool! Thank you. Quick question – is it possible to enter more than 4 options. I’ve tried, but each time it breaks the poll. Any help would be greatly appreciated! Thanks

    • Jaap Dijkma Says:

      I have used it with 16 options. Did you also put more than 4 colors in the color array?

  18. Carlos Says:

    where I can find the “PollForSharePoint.js” code ? Thank you so much

    • Alexander Bautz Says:

      Follow the link at the bottom of the article. I have updated the link in case the redirect did not work.

      Alexander

  19. Marcel Says:

    Hey Alexander, Great code you have here!

    I’ve just implemented it and can see results, looking good so far. Is there an option to always show the chart? So without having to click ‘Show Result’ first…

    Thanks in advance.

  20. Levi Says:

    Hi Alexander,
    The code works great but I’m wondering if I am doing something wrong b/c when I edit the test page housing the poll then save and close, I get duplicate polls in the CEWP and in the HTML there is a huge chunk of code present after the last

    I have both the PollforSharePoint.js file and the jQuery file stored in the same folder in SharePoint…is this the source of my issue? Thanks

    • Alexander Bautz Says:

      Hi,
      Use the content link option in the cewp “sidebar”, or use a HTML form web part.
      Alexander

  21. Susi Says:

    Hi Alexander,

    nice it works fine.
    If I have no internet connection, then the source code is not working to display the charts.So I copied the source code “http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js” in a *.js file that I uploaded to SharePoint.
    But the charts are not displayed…

    Can you tell me why this does not work?

    Thank you.

    • Alexander Bautz Says:

      Hi,
      The charts are created using Google Image charts – and this requires Internet access. There is no way you can display them without. Your only option is the use “table”.

      Alexander

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 435 other followers