Programmatically Create SharePoint Survey questions

Scenario

I'm seeing this question "Is there way to create SharePoint Survey questions programmatically ?" many times in SharePoint Forums and News Groups

Solution

The answer is that it's definitely achievable programmatically through SharePoint API's. The following snippet would help.

SPWeb web = SPControl.GetContextWeb(HttpContext.Current);

Guid surveyId = web.Lists.Add("Name of the Title", "Description of the survey", SPListTemplateType.Survey);

SPList survey = web.Lists["Survey List Name or Id];

string Question = "Question#1 for survey";

StringCollection choices = new  StringCollection();

choices.Add("first choice");

choices.Add("second choice");

choices.Add("third choice");

survey.Fields.Add(Question, SPFieldType.Choice, true, false, choices);

 

 Subscribe to my post

Published Thu, Aug 14 2008 5:56 by lavssun

Comments

# re: Programmatically Create SharePoint Survey questions

I have a webpart that already build list survey type, now ,i like to know how to answer those questions , but in the same way, i mean programmatically. The problem is that i build the webpart via render, and the controls for the answer too.

So, how answer the questions dinamically in run time.

Friday, August 15, 2008 12:05 PM by damian

# re: Programmatically Create SharePoint Survey questions

Hi,

Your article was very useful to me but I finally opted for the AddFieldAsXml as my case needed further settings.

sharepointologic.blogspot.com/.../add-rating-scale-question-to-survey.html

Friday, December 19, 2008 5:14 AM by Djavan ROA

# re: Programmatically Create SharePoint Survey questions

Can we determine if survey list contains any question or not?

Sunday, August 30, 2009 11:31 PM by Pradip Shrestha

# re: Programmatically Create SharePoint Survey questions

yes you can programmatically find the whether the survey has questions or not. Each survey question is internally stored as content type.

I think by default a survey list would have 42 fields in the field-collection(without any question). If the field collection has more than 42 items, then obviously it has questions attached to it

Wednesday, September 02, 2009 6:06 PM by lavssun

# re: Programmatically Create SharePoint Survey questions

It is a very good article. Now I'm trying to put a page separator to separate my questions in the survey but i'm having some problems with that. Can someone give some help on that?

Thx

Friday, December 04, 2009 10:29 AM by mario_j

# re: Programmatically Create SharePoint Survey questions

Is there any way creating survey question using SharePoint Client OM?

Monday, April 18, 2011 2:22 AM by Steve Im

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: