Friday, June 10, 2011

How to create a SharePoint 2010 Polling Web Part easily

Today I am going write about how to create a simple and very user friendly SharePoint 2010 Polling web part. Since I am hoping to develop it as a fully customizable Polling web part, in here I am going to explain the concept behind it.

I had a requirement, which will allow SharePoint user's to vote for polls which are published by the SharePoint Administrator. You may think that we can use SharePoint Surveys which is already available in SharePoint 2010 as well as SharePoint 2007. But my thought of Surveys is, there are some limitations in Surveys and not to mention it has nice capabilities too.

If I talk about limitations, for an example I will mention one. Let's say that we want to create a Survey, which a user can vote once. And Survey has a nice optional inbuilt feature to block user from voting more than once. But again to one survey, we can add questions later. So let's take this scenario. Administrator adds a survey which has one question. He blocks user from voting twice. Someone votes and when he try to vote for the second time, SharePoint will not allow it. Then Administrator wants to add a another question to the created Survey. Then the problem arises. User can't answer to that question, because he is blocked from accessing the same Survey twice. So what will happen is, Administrator will have to take all the trouble of creating and configuring another Survey.

Since I did not want to get all the trouble, what I did was created a simple web part that will give the following functionalities.
  • Easy to add a new Poll with one question.
  • Can set a expiry date to the Poll and after it expires, users can't no longer see it or vote it.
  • Each user can't vote more than once.
  • Only the Administrator can view the results.
  • Results can be viewed as Bar Charts or Pie Charts.

Now I am going to explain how I created it. Concept is fairly simple.
  • First create two lists. One is to add Polls and the other is to store user's vote information.
  • Let's call the first list as "Poll List" and the other as "User Poll List".
  • Set "User Poll List" as a hidden list.
  • In "Poll List", create seven additional columns. Title is already there when you have created it. List columns are,
    • Title - string
    • Answers - Check Box, Set Check Box values as Yes,No,Pass. So Poll publisher can select which answers should be there in each question
    • Poll Created By - People Picker
    • Date Started - Date and Time
    • Date Expired - Date and Time
    • Total Yes - Number and this column is disabled
    • Total No - Number and this column is disabled
    • Total Pass - Number and this column is disabled
  • In "User Poll List", create one additional column. Again Title is already there. List columns are,
    • Title - string
    • Voter's Name - string
  • Now Open Visual Studio 2010 and create a Visual Web Part.
  • Put a some labels and set their text to empty and a Button. Let's name it as "Vote".
  • Put a Radio Button List, so later we can fill the answer to it.
  • Write method to get the last item in the "Poll List" which is not expired and call it in the Web Part Load event.
  • Fill labels and fill the radio button list from the received data of the last item.
  • Now in "Vote" Button click event we are going to insert user selected data to both lists.
    • First query the "User Poll List" and check whether there is a item which the 'Title' is the current question and the 'Voter's Name' is the current logged on user's name. If there is, that means current logged on user has already voted for the selected question.
    • If there is no item, that means he/she has not voted for this question yet. So first add a item to the "User Poll List" and then update (increase the value by 1) the 'Total Yes'/'Total No'/'Total Pass' field in the "Poll List" to the relevant question.
  • And that's all. Now you might think that there will be a heavy load in the "User Poll List" as user's votes.
  • For that when a question has expires, we will delete all relevant items in the "User Poll List". By doing that we can avoid getting "User Poll List" by overloading.
I have created a nice Polling Web Part using above steps. If you see any drawbacks of the above method, please feel free to post a comment and appreciating your feedback.

Happy Coding.

Regards,
Jaliya


Update - 06th December 2011

Hello guys, since a lot of people are asking for the source code of my Polling Web Part, I have uploaded the source. You can download it from here. Please note that, I have not uploaded the full source code. But I am pretty sure, you will find the provided source code interesting.

Happy Coding.

Regards,
Jaliya

50 comments:

  1. Can you give me a code for multiple choise poll webpart..

    ReplyDelete
  2. yes, that can be done. give me your details.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  3. Hi there,

    If possible can I get as well, thanks

    ReplyDelete
  4. can you pls share the code for multiple choce poll webpart..

    pls send to
    skarthikmca05@gmail.com

    ReplyDelete
  5. hi could you share the code for multiple choice poll webpart.

    wafonrc@gmail.com

    Greetings...

    ReplyDelete
  6. Hi there, You have a nice implementation of a polling web part there. I managed to come up with a polling web part based on the steps you described, and its working great! I really appreciate it! However, I have a question, I am facing this problem of all my mutiple choices showing up in the web part when I only checked Yes and No during the creation of the poll. May I know how you retrieve the choices dynamically?

    -Chuwei limchuwei@gmail.com

    ReplyDelete
  7. Hi Jaliya! Thanks for your help on enlightening me earlier on. I managed to get the answers that were checked by the poll publisher. I retrieved the value from the list and did the following:
    string delimStr = ";#";
    char[] delimiter = delimStr.ToCharArray();
    String[] answerslist = answers.Split(delimiter,StringSplitOptions.RemoveEmptyEntries);

    and everything is working perfectly fine now! Thanks for all your help! :)

    -Chuwei

    ReplyDelete
  8. Hi Chuwei,

    I am glad you made it.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
  9. Hi Jaliya,

    can I get the source code, phebsix@gmail.com.

    Thanks

    ReplyDelete
  10. Hi Jaliya,

    Can you please send me the code. i would really appreciate it!
    email- fuzzy528@gmail.com

    Thanks,
    faraz

    ReplyDelete
  11. It looks as though there is a high demand for the source code. Could you please send me the Code as well?
    Thank you so much,

    adamwatson1.0@gmail.com

    ReplyDelete
  12. Hi Jaliya,

    Can you please send me the code. i would really appreciate it!
    email- darpanthedj@gmail.com

    Thanks,
    Darpan

    ReplyDelete
  13. Hello guys,

    I have updated the post with the source code download link. If you think that the source code is interesting, Please put a comment. So others will find it helpful.

    Thank You.

    Happy Coding.

    Regards,
    Jaliya

    ReplyDelete
    Replies
    1. Jaliya, Can you send help on with need to implement for Anonymous users...Can you please suggest and Share with coding
      and the question and choices will be display on homepage for Anonymous..Can You please share with complete code ASAP...would u appriciate u...pls help me....dvmkrishna.2009@gmail.com

      Delete
  14. Man thats super cooool

    What about the complete code

    can you send it to system_by@yahoo.com

    ReplyDelete
  15. I have a similar requirememnt for creating polls or voting mechanism on sharepoint, however, one important requirement is that as ad administrator i must be able to set groups of people who can vote on a certain poll and the groups could be a combination of various things like office location, departments, etc. Any ideas?

    ReplyDelete
  16. email me please : houji_zhou@yahoo.com

    ReplyDelete
  17. Hi Jaliya,
    Its very nice post,i have similar kind of requirement.

    Can you please send complete code to venkey.choudam@gmail.com

    ReplyDelete
    Replies
    1. Please download the source code from the provided link.

      Happy Coding.

      Regards,
      Jaliya

      Delete
  18. Hi there,

    Would you mind sending the source code to cfrankland@gmail.com?

    Thanks very much!

    ReplyDelete
    Replies
    1. Please download the source code from the provided link.

      Happy Coding.

      Regards,
      Jaliya

      Delete
  19. Hi Jaliya, can you please send your source code to guttat19@comcast.net, will appreciate it very much

    ReplyDelete
    Replies
    1. Please download the source code from the provided link.

      Happy Coding.

      Regards,
      Jaliya

      Delete
    2. hi Jaliya,

      Thanks a lot to this source code....please send to me .ascx design code also


      haribabu708@gmail.com

      Delete
  20. it is very nice.
    can you pls send the code to hemalth485@gmail.com

    ReplyDelete
    Replies
    1. Please download the source code from the provided link.

      Happy Coding.

      Regards,
      Jaliya

      Delete
  21. Hi, Jaliya. Can you please send me the code to myprostyle@gmail.com

    ReplyDelete
    Replies
    1. Please download the source code from the provided link.

      Happy Coding.

      Regards,
      Jaliya

      Delete
    2. Nothing in my mailbox. Plz, try again. And copy to mcproxa@yandex.ru

      Delete
  22. Hi Jaliya,

    Very Very useful post !!!!!!!!!
    I have the same requiremwnt for MOSS 2007
    Is it possible to deploy the same webpart in MOSS 2007?
    Plz help me....thanks in advance :)

    ReplyDelete
    Replies
    1. And one more thing,You haven't mentioned about view results and view results as bar cahrt or pie chart in the Source Code....If you don't mind could you please mail me those code part? (Email ID:gvenkateshsun@gmail.com)

      Delete
  23. Hi Jaliya Udagedara,

    Can you give me the Polling code ???

    If yes just send it to jaytjhin@yahoo.co.id

    Thx b4


    Best Regards,

    JAY

    ReplyDelete
  24. Hi Jaliya Udagedara,

    Could you please email me coude to anvar58@hotmail.com

    I tried the link - it did not work. ASking me to log in. If you send it to me directly to my email I would really appreciate it.

    Thank you,
    Anvar

    ReplyDelete
  25. Hi Jaliya,

    Could you also send the full sourcecode to dreweitin@yahoo.com

    Thanks!
    drew

    ReplyDelete
  26. Thats interesting.... but doing so we are coding for something that we already have OOTB called as Surveys.. Its like reinventing the wheel... so is there any way we can use Survey list and use it for polling ?? so that we can use most of sharepoint stuff and need not to worry about minute stuff a lot..

    ReplyDelete
  27. Hi Jaliya,

    Can you please send me the complete source code. we have same requirement with mutiple answers from multiline text.

    email id: veeru.bikkina@gmail.com

    Really appreciated all your help.

    Thanks,
    veeru

    ReplyDelete
  28. Hello Jaliya,

    Could you send code for multiple answers to dezmond928@yahoo.com

    Thanks very much!

    ReplyDelete
  29. Jaliya,

    We have a situation where we would like to have a weekly poll for a "Picture of the Week". How easy is it to reuse the same poll over and over again with this method?

    My email is: scott.walton@hines.com

    Thanks!

    -Scott

    ReplyDelete
  30. Hello Jaliya,

    Could you send code for multiple answers.
    My Email is:balajimore1985@gmail.com

    Thanks very much!

    ReplyDelete
  31. Hi,

    Would you please send me a copy of the source. This web part is really what I'm looking for.

    Please. Thanks.

    Email: chesterben28@gmail.com

    ReplyDelete
  32. Hi,

    can you please send me the full source code.
    Thanks.

    Email: gupta09chandan@gmail.com

    ReplyDelete
  33. I am working with my code as explained in this blog. What are the reporting options we can see? bar chart/pie chart/ export to excel???

    ReplyDelete
  34. Hi,

    Would you be able to share the source code of the Poll Webpart?
    Email: itzpankaj@hotmail.com

    ReplyDelete
  35. Jaliya, Can you send help on with need to implement for Anonymous users...Can you please suggest and Share with coding
    and the question and choices will be display on homepage for Anonymous..Can You please share with complete code ASAP...would u appriciate u...pls help and Mail me....dvmkrishna.2009@gmail.com

    ReplyDelete
  36. I needed to thank you for this great read!! I absolutely loved every little bit of it. I’ve got you book-marked to look at new things you post…
    website design

    ReplyDelete
  37. Heya! If this is working in Sharepoint Online 2013, I would love to get your Quick Poll web part! I've been trying to find/create one for a long time now and as I have no coding skills I have not been able to create anything. :-) So if it's working on SPO 2013 and you are willing to share it, please send it to xear09@gmail.com

    Thank you! :-)

    ReplyDelete
  38. Hello, can you email me the code if it works in SharePoint 2013. My email address is savio.dias@live.com
    Thanks a lot
    Savio

    ReplyDelete
  39. Hai can you please send me the code to ankarao59@gmail.com

    ReplyDelete
  40. Hi,

    Thanks for your post,

    actually I want use online poll for internet site. there is no user specific. so could you please give some suggestion or source code then it will be very helpful.

    my mail id is: madeswvimal@gmail.com

    Thanks and Regards
    S.Madeswaran

    ReplyDelete