Working with People/Group column and Lookup column is always a small hurdle in development.
$expand
This is very useful when dealing with a person or lookup field where only the Id is returned. Using this, we can get corresponding values based on Id.
See the below example:
Lookup Field: Say, there is City column in Country list which is a lookup to Title column in Info List.
- /_api/web/lists/getbytitle('infolist')/items?$select=ID,Title,Employee,company,city/Id&$expand= city/Id
People Field: Let’s say list has a custom field: Author, it will return ‘AuthorId’ in response.
What is the proper way to deal with people field? You need to use ‘$expand’ parameter to expand the field.
Following REST URL gives your idea how to use $expand.
- /_api/web/lists/getbytitle('infolist')/items?$select=Author/Title&$expand=Author/Id
Rest URL to be use SP 2013
- var ListName = 'test';
- var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle(" + ListName + ")/items?$Select=Title,Author/ID,Author/FirstName,Author/LastName,Author/Title,Author/Department,Author/SipAddress&$expand=Author/ID ";
-
- function GetData() {
- var ListName = 'Test';
- var url = "https://XXXXX.sharepoint.com/sites/testsite" + "/_api/web/lists/getbytitle('Test')/items?$Select=Title,Author/ID,Author/FirstName,Author/LastName,Author/Title,Author/Department,Author/SipAddress&$expand=Author/ID ";
- $.ajax({
- url: url,
- headers: {
- Accept: "application/json;odata=verbose"
- },
- async: false,
- success: function(data) {
- var items = data.d.results;
- },
- eror: function(data) {
- alert("An error occurred. Please try again.");
- }
- });
- }
Author object will be appended in the result,