0

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


  

enter image description here

1
  • Hi @user1063287, does my answer below help you in any way? If yes, Please Upvote(^) and accept as an Answer as it helped you & it will help others with similar question in future to find the correct answer easily. It also removes this question from "Unanswered questions" list. Oct 13, 2022 at 11:13

1 Answer 1

0

What you are looking for is this default behavior of SharePoint Survey lists when Survey settings are like this:

enter image description here

When user tries to respond to the survey 2nd time, they will get error screen like:

enter image description here


But, when you user anonymous survey, "Created By (Author)" field will be empty like (All Responses view):

enter image description here

So, you will not be able to check the author of survey from SharePoint UI while using anonymous survey.


Below is working JSOM code which will work fine when survey is not anonymous. But, it will return 0 for Author ID value when survey is anonymous. So, you cannot really compare it with currently logged in user.

<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(spFileLoaded, "sp.js"); });

    //Read survey for current user to find out if already responded   
    function spFileLoaded() {
        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 context = new SP.ClientContext.get_current();
        var list = context.get_web().get_lists().getByTitle("MySurvey");
        var viewXml = '<View><Query><Where><Eq><FieldRef Name="Author" LookupId="TRUE"/><Value Type="Integer"><UserID/></Value></Eq></Where></Query></View>';
        var query = new SP.CamlQuery();
        query.set_viewXml(viewXml);
        var items = list.getItems(query);
        context.load(items, 'Include(Title, Id, Author)');
        context.executeQueryAsync(function () {
            var enumerator = items.getEnumerator();
            while (enumerator.moveNext()) {
                var authorId = enumerator.get_current().get_item("Author").get_lookupId();
                if (authorId == _spPageContextInfo.userId) {
                    console.log("Already responded.");
                    cbSurveyResult(true);
                    break;
                }
            }
        }, function () {
            cbSurveyResult(null);
        });
    }
</script>

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.