Sitefinity CMS

Managing Questions Send comments on this topic.
See Also
Developing with Sitefinity > Modules > Modules API > Polls > Question Items > Managing Questions

Glossary Item Box

This topic provides examples for using PollManager methods to manage question items.

 

Creating Questions:

Finding Questions:

Modifying Questions:

Deleting Questions:

 

Creating Questions

Create a question for specified poll item:

A poll item could contain one or more questions of type IQuestionItem. The following example demonstrates how to create a new question for a newly-created poll:

CreateQuestion(IPollItem poll) Copy Code
// create new instance of PollManager
Telerik.Polls.PollManager pollManager = new Telerik.Polls.PollManager();
// create new PollItem to which we are going to add a question
Telerik.Polls.IPollItem pollItem = pollManager.CreatePoll();
// set title of new poll
pollItem.Title = "My Poll with Question";
// update the poll item
pollManager.UpdatePoll(pollItem);
// create new QuestionItem
Telerik.Polls.IQuestionItem question1 = pollManager.CreateQuestion(pollItem);
// assign values to some properties of the question item
// assign text to question
question1.Text = "Question 1 - text";
// the question will be displayed with RadioButton control
question1.Layout =  Telerik.Polls.QuestionLayout.RadioButton;
// this question will be the first one for the poll named pollItem
question1.SortOrder = 0;
// update the question item
pollManager.UpdateQuestion(question1);

 

Finding Questions

Get a specific question by its ID:

GetQuestionById(Guid id) Copy Code
// create new instance of PollManager
Telerik.Polls.PollManager pollManager = new Telerik.Polls.PollManager();
// get all polls
IList<Telerik.Polls.IPollItem> listOfAllPolls = pollManager.GetAllPolls();
if (listOfAllPolls.Count > 0)
{
   
// get reference to the first poll
   
Telerik.Polls.IPollItem firstPoll = (Telerik.Polls.IPollItem)listOfAllPolls[0];
   
//find ID of second question
   
if (firstPoll.Questions.Count > 2)
   {
       
// get the ID of the second question - this process is actually redundant, but we
       
// are using it here for the purpose of demonstration
       
Guid secondQuestionId = ((Telerik.Polls.IQuestionItem)firstPoll.Questions[1]).ID;
       
// retrieve the QuestionItem by passing the ID to the GetQuestionById method
       
Telerik.Polls.IQuestionItem secondQuestion = pollManager.GetQuestionById(secondQuestionId);
       
// write the headline of the secondItem to see the result
       
Response.Write(secondQuestion.Text);
   }
}

 

Get all questions for a given poll item:

GetQuestions(Guid pollId) Copy Code
// create new instance of PollManager
Telerik.Polls.PollManager pollManager = new Telerik.Polls.PollManager();
// get all polls
IList<Telerik.Polls.IPollItem> listOfAllPolls = pollManager.GetAllPolls();
if (listOfAllPolls.Count > 0)
{
   
// get reference to the first poll
   
Telerik.Polls.IPollItem firstPoll = (Telerik.Polls.IPollItem)listOfAllPolls[0];
   
//find ID of the first poll
   
Guid firstPollId = firstPoll.ID;
   
// get all the questions belonging to the first poll by passing the ID of the
   
// first poll to the GetQuestions function
   
IList<Telerik.Polls.IQuestionItem> questionsOfFirstPoll = pollManager.GetQuestions(firstPollId);
         
   
// write all the texts so that we can see the result
   
foreach (Telerik.Polls.IQuestionItem questionItem in questionsOfFirstPoll)
       Response.Write(questionItem.Text +
"<br />");
}

 

Modifying Questions

Updating a QuestionItem is a very simple process:

  1. Get a QuestionItem associated with a transaction
  2. Change its properties
  3. Save the QuestionItem with PollManager
UpdateQuestion(IQuestionItem updatedQuestion) Copy Code
// create a new instance of PollManager
Telerik.Polls.PollManager pollManager = new Telerik.Polls.PollManager();
//get all polls
IList<Telerik.Polls.IPollItem> listOfAllPolls = pollManager.GetAllPolls();
if (listOfAllPolls.Count > 0)
{
   
// get reference to the first poll
   
Telerik.Polls.IPollItem firstPoll = (Telerik.Polls.IPollItem)listOfAllPolls[0];
   
//find ID of the first poll
   
Guid firstPollId = firstPoll.ID;
   
if (firstPoll.Questions.Count > 0)
   {
        
// get the ID of the second question - this process is actually redundant, but we
        
// are using it here for the purpose of demonstration
        
Guid secondQuestionId = ((Telerik.Polls.IQuestionItem)firstPoll.Questions[0]).ID;
        
// retrieve the QuestionItem by passing the ID to the GetQuestionById method
        
Telerik.Polls.IQuestionItem secondQuestion = pollManager.GetQuestionById(secondQuestionId);
        
//modify the properties of the QuestionItem
        
secondQuestion.Text = "The modified text";
        
//update the question
        
pollManager.UpdateQuestion(secondQuestion);
   }
}

 

Deleting Questions

Delete a question specified by ID:

DeleteQuestion(Guid id) Copy Code
// create a new instance of PollManager
Telerik.Polls.PollManager pollManager = new Telerik.Polls.PollManager();
//get all polls
IList<Telerik.Polls.IPollItem> listOfAllPolls = pollManager.GetAllPolls();
if (listOfAllPolls.Count > 0)
{
   
// get reference to the first poll
   
Telerik.Polls.IPollItem firstPoll = (Telerik.Polls.IPollItem)listOfAllPolls[0];
   
//find ID of the first poll
   
Guid firstPollId = firstPoll.ID;
   
if (firstPoll.Questions.Count > 2)
   {
       
// get the ID of the second question - this process is actually redundant, but we
       
// are using it here for the purpose of demonstration
       
Guid secondQuestionId = ((Telerik.Polls.IQuestionItem)firstPoll.Questions[1]).ID;
       
// retrieve the QuestionItem by passing the ID to the GetQuestionById method
       
Telerik.Polls.IQuestionItem secondQuestion = pollManager.GetQuestionById(secondQuestionId);
       
// finally, delete the second question of the first poll
       
pollManager.DeleteQuestion(secondQuestionId);
   }
}


 

See Also