Edit:
Just realized the Survey is set to anonymous. So I'm assuming you can't check the author?
I'm trying to check if a user has filled out a SharePoint survey. I've found a few questions on it and some examples. I currently combined some code to get where I am now.
The Task:
When the page loads it checks to see if the user has already submitted the survey, it will show the survey questions if they have not. After they submit, I refresh the page.
When the page loads after they submitted, it should re-check if they submitted and if they have it should hide the survey questions and then show the survey results.
The first part works. User opens the page, fills out the survey and submits. The page reloads and I get the error:
Uncaught Error: The property or field 'Id' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Error seems to be on this line:
if(item.get_lookupId() == web.get_currentUser().get_id())
There is not an error when the user has not submitted the survey. This fails after the user submits the survey and tries to reload the page.
Error happens when the page reloads.
Thank you for any help!
$(document).ready(function () {
//Read survey for current user to find out if he have already voted
readSurveyVotes(function (isCurrentUserRespond) {
if (isCurrentUserRespond) {
$("#newFormSurvey").css({
"display": "none"
});
$("#surveyDisplay").css({
"display": "inline"
});
return;
} else {
$("#newFormSurvey").css({
"display": "inline"
});
$("#surveyDisplay").css({
"display": "none"
});
}
});
function readSurveyVotes(cbSurveyResult) {
var isCurrentUserRespond = undefined;
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle("Survey");
var viewXml = '<View><Where><Eq><FieldRef Name="Author"/><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Where></View>';
var query = new SP.CamlQuery();
query.set_viewXml(viewXml);
var items = list.getItems(query);
context.load(items);
context.add_requestSucceeded(onLoaded);
context.add_requestFailed(onFailure);
context.executeQueryAsync(function () {
var enumerator = items.getEnumerator();
while (enumerator.moveNext()) {
var item = enumerator.get_current().get_fieldValues().Author;
if (item.get_lookupId() == web.get_currentUser().get_id()) {
isCurrentUserRespond = true;
cbSurveyResult(isCurrentUserRespond);
break;
}
}
});
function onLoaded() {
if (!isCurrentUserRespond) {
cbSurveyResult(null);
}
}
function onFailure() {
cbSurveyResult(null);
}
}});