Change the number of votes

The following sample code shows how you can increase the number of votes for a given answer using the API. It's useful if you want to create your own voting dialog.

 

[C#]

 

using CMS.Polls;

using CMS.SettingsProvider;

using CMS.DataEngine;

using CMS.GlobalHelper;

 

...

 

       string pollName = "testingpoll"; // Poll code name

 

       // Get poll info for the specified code name

       PollInfo pollObj = PollInfoProvider.GetPollInfo(pollName);

 

       if (pollObj != null)

       {

           // Get all answers of the poll

           DataSet ds = PollAnswerInfoProvider.GetAnswers(pollObj.PollID);

 

           if (!DataHelper.DataSourceIsEmpty(ds))

           {

               // Get first pollanswerinfo object

               PollAnswerInfo pollAnswerObj = new PollAnswerInfo(ds.Tables[0].Rows[0]);

 

               if (pollAnswerObj != null)

               {

                   // Increment votes by 1

                   pollAnswerObj.AnswerCount++;

 

                   // Save the data

                   PollAnswerInfoProvider.SetPollAnswerInfo(pollAnswerObj);

               }

           }

       }