Question

The question is quite simple:

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

Was it helpful?

Solution

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);
         }          
    }
 }

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top