31.10.2014 Views

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

104 | Developer’s <strong>Guide</strong> <strong>EPiServer</strong> Community 3.1<br />

Get the Current State<br />

In the example below, we will create a table of integer values that represent percentage distribution of votes among<br />

poll choices. In a real life scenario, these numbers can be used e.g. to display a percentage bar chart that will show how<br />

people voted in this poll.<br />

// Get the poll get state<br />

Poll poll = PollHandler.GetPoll(11);<br />

// Create table to keep the percentages<br />

int[] percentage = new int[poll.Choices.Count];<br />

int choiceVoteCount = 0;<br />

int totalVotes = poll.VotesCount;<br />

// Fill the table with calculated percentages<br />

for( int ix = 0; ix < poll.Choices.Count; ix++ )<br />

{<br />

choiceVoteCount = poll.Choices[ix].VoteCount;<br />

percentage[ix] = (int)((((float)choiceVoteCount)/totalVotes)*100);<br />

}<br />

After a poll ha been retrieved from the database, we have access to all the properties of the poll itself, as well as its<br />

choices. There is no need to use other PollHandler methods to retrieve information about the poll – the properties<br />

provide all necessary data.<br />

2.25.5 Adding Choices after Creation<br />

The poll choices do not have to be added before the poll is committed in the database. It is possible to create a poll<br />

with its text (a question), and after that to add choices (e.g. after a research on what choices are available). This<br />

example will present how to update a poll with new choices.<br />

Import Necessary Namespaces<br />

First, import the necessary namespaces that will be used to add choices to a poll. The namespace<br />

StarCommunity.Modules.Poll is described by clicking on its name. Make sure you add the assembly as a reference,<br />

mentioned in section 1.1.1.<br />

using StarCommunity.Modules.Poll;<br />

© <strong>EPiServer</strong> AB

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!