2

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);
    });
});
Pull
  • 2,236
  • 2
  • 16
  • 32

1 Answers1

2

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"));         
     }  
  }
);
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193