0

Successfully grabbing values for person columns with multiple selections allowed. Looking at the call in the browser I see the info I need:

function dChange() {
$.ajax({
  url: xurl + "/_api/web/lists/getbytitle('somelist')/items/getById(someid)?$select=Approvers/Name,Reviewers/Name,Informed/Name&$expand=Approvers,Reviewers,Informed",
  type: "GET", headers: { "Accept": "application/json;odata=verbose", },
  success: function (data) {
    if (data.d.Approvers.results) {
      data.d.Approvers.results.forEach(function (Approver) {
        console.log(Approver.Name);
        });
    }
  },
  error: function (error) { alert(JSON.stringify(error)); }
});
}

The following works fine if only a single person in the Approvers column:

$.each(data.d.Approvers.results, function (index, item) {
console.log("hello");
});

or

var x = data.d.Approvers.results.length;
alert(x);

etc...

However, if more than one person was in the Approvers column these methods produce errors: Uncaught TypeError: Cannot read... property of undefined at Object.success...

Suggestions?

enter image description here

  • Can you post more example code from your success function? In answering one of your other questions I did a quick test and found that if there are no users in the field then data.d.Approvers would not have a results property at all, but one or more users will add a results array. So it doesn't make sense that just trying to access data.d.Approvers.results would cause that error if there is more than one person is in the column, it must be something else in the code. – Dylan Cristy Apr 5 at 20:28
  • Updated to show full function. Not much in there until I correct my syntax to retrieve multiple persons from the Approvers column. These samples and others work great when there is only a single person in the Approvers column. – BDC604 Apr 5 at 20:41
  • Any chance you can console.log(data.d) when you are getting an item with more than one user in the Approvers column and post a picture of the JSON structure? All my testing is giving me consistent results - no users means no results array, one or more users means a results array is present (just of varying length). Without being able to see the actual result and data that's returned from your query it's hard to speculate as to why you're getting that error. – Dylan Cristy Apr 5 at 20:58
  • Picture added. First run is when multiple persons in Approvers column. Second is when there is only one. – BDC604 Apr 5 at 21:05
  • So some other error is happening, maybe on the server, because clearly in the first run data.d is undefined, which means you got nothing back from the server. There is no other special syntax to read something that just isn't there. Are you getting different list items (different IDs) with each of those? Have you tried adding a second person to the Approvers column for the list item you can successfully retrieve? – Dylan Cristy Apr 5 at 21:10
0

The methods listed above work, the specific list item with multiple ppl I was using for testing was creating problems for some reason. When I used a different list item with multiple ppl this worked. Weird. Thanks Dylan for your advice.

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.