Expand a multi value field in REST API
-
02-10-2020 - |
Question
I have a Sharepoint list that has a multi value field which stores Person or Group
.
In the GET
call to the Sharepoint list, I get the following response. Collaborators
is the name of the field in discussion.
"Id": 1,
"ContentTypeId": "0x0100B86E441274F847A599E46276483F9FBB009692E8772BC86A4F8BB8570C018F79BC",
"Title": "One Microsoft Way",
"StartDate": "2015-04-15T06:55:19Z",
"EndDate": "2015-04-15T06:55:19Z",
"CollaboratorsId": {
"results": [
18
]
},
"ID": 1,
"Modified": "2015-04-15T06:55:26Z",
"Created": "2015-04-15T06:55:26Z",
"AuthorId": 1,
"EditorId": 1,
I get an array of IDs
in the response, but I wish to expand that field to show the Person or Group name. I tried to use $expand=Collaborators&$select=Collaborators/Title
. But this does not work.
Any way to expand the values in a multi-value field?
Solution
Try this sample
var call = $.ajax({
async: false,
url: _spPageContextInfo.webAbsoluteUrl
+ "/_api/Web/Lists/GetByTitle('AppointmentInfo')/items"
+ "?$select=Id,Collaborators/Id, Collaborators/Title,Title"
+ "&$expand=Collaborators/Id",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function(data, textStatus, jqXHR) {
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var CollaboratorsArray = items[i].Collaborators.results;
for (var j = 0; j < CollaboratorsArray.length; j++) {
console.log(Collaborators[j].Id + ' - ' + Collaborators[j].Title);
}
}
});
OTHER TIPS
Its should be
$select=Collaborators/Title&$expand=Collaborators
Example your URL should like
http://yoursite/_api/web/lists/GetByTitle('Employee Details')/items("1")?$select=ManagerName/Title,FirstName,LastName,Position,Phone,Fax,PreviousCompany,Reference,BirthDate,Relocate,EmailAddress&$expand=ManagerName
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange