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