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.

Tutorials | 105<br />

2.25.6 Add Choices to Existing Poll<br />

First, we will need to retrieve the Poll object from the database. After that, we can create as many Choice objects as we<br />

need and add them to the Choices collection of the Poll object. When all Choice objects have been added, we need to<br />

save the changes in the database – we use the UpdatePoll method of the PollHandler class. The PollHandler adds,<br />

updates or removes any changed choices within the Poll.<br />

// Retrieve Poll from the database<br />

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

// Determine current maximum choice order<br />

int maxOrder = 0;<br />

foreach( Choice ch in poll.Choices )<br />

{<br />

if ( ch.Order > maxOrder )<br />

maxOrder = ch.Order;<br />

}<br />

// Create two additional choices to add – when creating Choice<br />

// set text and order<br />

Choice choice1 = new Choice("Added choice 1", ++maxOrder);<br />

Choice choice2 = new Choice("Added choice 2", ++maxOrder);<br />

// Add choices to the poll<br />

poll.Choices.Add( choice1 );<br />

poll.Choices.Add( choice2 );<br />

// Commit poll object in database<br />

poll = PollHandler.UpdatePoll(poll);<br />

© <strong>EPiServer</strong> AB

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

Saved successfully!

Ooh no, something went wrong!