Add a rating scale question to a survey programmatically

After many attempts googling for this I found this blog, but I didn't describe how to set the additional settings.

So I ended up by trying the "AddFieldAsXml(string strXml)" and I got to this:
using (SPWeb web = site.OpenWeb())
{
try
{
// create survey list and get guid
Guid guidList = web.Lists.Add(SURVEY_NAME, String.Empty, SPListTemplateType.Survey);

// Build XML rating question
string xmlField = "<Field Type=\"GridChoice\" DisplayName=\"How cool is Djavan ROA?\" Required=\"TRUE\" GridStartNum=\"1\" GridEndNum=\"5\" GridTxtRng1=\"Low\" GridTxtRng2=\"Average\" GridTxtRng3=\"High\" StaticName=\"How_x0020_cool_x0020_is_x0020_Dj\" Name=\"How_x0020_cool_x0020_is_x0020_Dj\"><CHOICES><CHOICE>Rating</CHOICE></CHOICES></Field>";

// Add the question
web.Lists[guidList].Fields.AddFieldAsXml(xmlField);
}
catch (Exception ex)
{
// Exception handling
}
}


Warning: The syntax coloration had trouble displaying the [\"] so just replace the ["] in the string with [\"].

Comments

Pradip Shrestha said…
Can we determine if survey list contains any question or not? How?
Djavan ROA said…
Hi, sry for the delay.
A question is a field (column), you should look in SPList.Fields http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.aspx
Create Survey Lists in SharePoint : http://sharepointsolutions-sudheer.blogspot.in/2014/11/creating-survey-list-programmatically.htmla