3

The question is quite simple:

how can I get the list of questions from a Survey?

2 Answers 2

3

In Survey list the question is a field. In order to determine whether field is a question or regular field you could utilize SourceID attribute, in case of questions its value is not http://schemas.microsoft.com/sharepoint/v3:

/// <summary>
/// Get questions in Survey List 
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static IEnumerable<SPField> GetSurveyQuestions(SPList list)
{
     return (from SPField field in list.Fields 
                let fieldSchema = XElement.Parse(field.SchemaXml) 
                let sourceId = fieldSchema.Attribute("SourceID").Value 
                where sourceId != "http://schemas.microsoft.com/sharepoint/v3" 
                select field).ToList();
}

Usage

using (var site = new SPSite(siteUrl))
{
    using (var web = site.OpenWeb())
    {
         var list = web.Lists[surveyName];
         var questions = GetSurveyQuestions(list);
         //print questions
         foreach (var question in questions)
         {
            Console.WriteLine(question.Title);
         }          
    }
 }
1
  • Worked awesome! How can I modify so I get the created date for each question?
    – Si8
    Commented May 12, 2016 at 17:37
0

The only answer I found right now is this: checking the ShowInVersionHistory property of each field!

using (SPSite sito = new SPSite("URL"))
        {
            using (SPWeb web = sito.OpenWeb())
            {
               var list = web.Lists["SURVEY LIST NAME"];

                foreach(var Field in list.Fields.Cast<SPField>().Where(x => x.ShowInVersionHistory))
                {
                    Console.WriteLine(Field.ToString());
                }
          }
   }

In my case, this will print all the Questions

Is there a better way to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.