I had a requirement to get a Poll webpart on the portal Main page like you usually see on websites.
- A question, 3 options as a radio button list and a submit button.
- A user should be allowed to vote only once
- If user has voted show him the response
Out of your mind, U think of Survey on Sharepoint but it wont work like this, the way polls work on websites. SO I searched online and found some poll web part for $199. Big money. If the client buys it shame on me. Y not make my own Poll webpart. So folded my sleeves up started a web part project using C# made my Poll webpart soon. Tested and delivered upto satisfaction. Well now lets get to the technicality, what magic did my code do. Its pretty simple. U just need to take a pen n paper , think of all permutations, combinations possible and than make a good design and than start coding. usually people just fold their sleeves and start coding. Avoid it.
Solution:
- Create a Custom list to save the Questions, with following fields Question, Answer1, Answer2, Answer3, Expiry Date
- Create a Custom list to save the votes, with fields Question, Answer.
It will by default have the created by Field indicating the Author - Create your webpart project.
Khallas means Thats It. u r done.
What does the webpart code to do:
- Override the Createchildcontrols method.
- Query the Questions List to check if any questions exist with expiry date greater than or equal to today. U will need CAML query
- If No, create a label control stating "No Polls currently available"
- If Yes, query the Votes list with the current User name and current question, if an record exists means User has voted else Not Voted.
- If Voted, Query the Votes List get all the responses for the current Question and Response1, Question and response2, Question and Response3 . So the 3 queries count value gives u the number of votes for each option. make a Table to represent this information.
- If Not Voted, Create a Label control for question. create 3 radio buttons for three responses, with radiobutton groupname same. So that they act as a Radio button list
- Create a submit button and also a Onclick event for the button.
- In the Onclick event you simply get the question and selected Radio button value and insert a new record in the Votes list and than repeat step 5 and hide the controls for submitting vote and also add a label "You voted successfully"
- Refactoring comes here, create a separate function for step 5 accept a boolean value indicating whether it is step 5 or step 8 and accordingly to show or not to show the "Voted Successfully message"
Thats it so zimble, I meant simple. Leave a comment if u lost anywhere in between
3 comments:
>You will need a CAML query for this.
What is CAML ?.... ;)
Bus kya yaar, how did u write the CAML query w/o going through CAML first ? ;)
Anyway, in case you are still (really?) wondering, here goes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tscamlovIntroduction_SV01029856.asp
Is is possible for you to post the code? Thanks!
Post a Comment