0

In SharePoint Online, is it possible to filter a survey list by Created By? I've found several posts with similar question but none of the suggested solutions work (i.e. expand=Author or expand=Author/ID. I'm trying to find out if the current user already voted.

<script>

 $(document).ready(function(){
    myVote();
 });

function myVote() {
var webAbsUrl = _spPageContextInfo.webAbsoluteUrl;
var currUser = _spPageContextInfo.userEmail;
var tCounter = 0;
var tCount;
var tEMail;

var voteUri = webAbsUrl + "/_api/web/Lists/getByTitle('Survey')/Items?$\
  filter=Author/EMail eq '" + currUser + "'&$\
  select=Id, Author/EMail&$\
  expand=Author/Id";

var voteAjax = $.ajax({
  url: voteUri,
  async: false,
  method: "GET",
  headers: { "Accept": "application/json; odata=verbose" },
  success: function (data) {
    tCount = data.d.results.length;
    if (tCount == 1) {
        console.log ('Current user already voted.');
    } else if (tCount == 0) {
        console.log ('Has not voted.');
    } else {
        console.log ('Something went wrong');
    }
    //Just for test
    if(data.d != undefined) {
      $.each(data.d.results, function (index, item) {
        tId = data.d.results[tCounter].Id;
        tEMail = data.d.results[tCounter].Author.EMail;
        console.log (tEMail);
        tCounter++;
      });
    }
  },
  errors: function(data) { console.log("Failed to load votes"); }
});
}

First error is the tCount - it should only return 1 but its returning 2 (total # of people responded, mine included).

Second error is Uncaught TypeError: Cannot read property 'EMail' of undefined.

I also tried filtering by AuthorID, tCount also returned 2.

If I don't filter, then the second error goes away and logs each Author.EMail in console.

1

We can use current user Id to pass the $filter in REST API for Author. The following code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    myVote();
});
function myVote() {
    var webAbsUrl = _spPageContextInfo.webAbsoluteUrl;
    var currUser = _spPageContextInfo.userId;
    var tCounter = 0;
    var tCount;
    var tEMail;

    var voteUri = webAbsUrl + "/_api/web/Lists/getByTitle('Survey')/Items?$filter=Author eq '" + currUser + "'&$select=Id,Author/EMail&$expand=Author";

    var voteAjax = $.ajax({
        url: voteUri,
        async: false,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            tCount = data.d.results.length;
            if (tCount == 1) {
                console.log ('Current user already voted.');
            } else if (tCount == 0) {
                console.log ('Has not voted.');
            } else {
                console.log ('Something went wrong');
            }
            //Just for test
            if(data.d != undefined) {
                $.each(data.d.results, function (index, item) {
                    var tId = item.Id;
                    tEMail = item.Author.EMail;
                    console.log (tEMail);
                    tCounter++;
                });
            }
        },
        errors: function(data) { console.log("Failed to load votes"); }
    });
}
</script>
0

I believe for that to work you need to either expand on the Email property of the Author, or just expand on the entire Author like

$expand=Author/Email

or

$expand=Author

but not

$expand=Author/Id

because that then is only returning the ID of the Author user, when what you are trying to get to is the email.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.