Search code examples
jqueryweb-servicessharepointspservices

SPServices : how to get a survey?


I made a basic survey using SharePoint and I can't find how to get the question using SPServices.

I only know how to get the responses of the survey, using this code :

$().SPServices({
    operation: "GetListItems",
    webURL: "https://mysite.com/",
    listName: "SurveyMobileSP",
    CAMLQuery:"",
    error: function (xhr, message, error) {
        alert('Error : ' + error);
    },
    completefunc: function (xData, status) {
        console.log('Status: '+status+' xdata: ' + 'RESPONSE: ' + xData.responseText);
    });
});

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 qurstions its value is not http://schemas.microsoft.com/sharepoint/v3

    How to retrieve questions from a Survey list using SPServices

    function getSurveyQuestions(complete) 
    {
      $().SPServices({
        operation: "GetList",
        listName: "Survey",
        completefunc: function(xData, Status) {
          var questions = []; 
          $(xData.responseXML).find("Fields > Field[SourceID!='http://schemas.microsoft.com/sharepoint/v3']").each(function() {
            var $fieldNode = $(this).get(0);
            questions.push($fieldNode);
          });
          complete(questions);
        }
      });
    }
    

    Usage

    getSurveyQuestions(
      function(questions)
      {
         for(var i = 0; i < questions.length;i++) {
            console.log( "Question: " + $(questions[i]).attr("DisplayName"));         
         }  
      }
    );