Getting People/Group And Creating By Value In Rest API Call in SharePoint Online And Multiple Filtering

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.
  1. /_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.
  1. /_api/web/lists/getbytitle('infolist')/items?$select=Author/Title&$expand=Author/Id     
Rest URL to be use SP 2013
  1. var ListName = 'test';  
  2. 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 ";  
  3.   
  4. function GetData() {  
  5.     var ListName = 'Test';  
  6.     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 ";  
  7.     $.ajax({  
  8.         url: url,  
  9.         headers: {  
  10.             Accept: "application/json;odata=verbose"  
  11.         },  
  12.         async: false,  
  13.         success: function(data) {  
  14.             var items = data.d.results; // Data will have Author object    
  15.         },  
  16.         eror: function(data) {  
  17.             alert("An error occurred. Please try again.");  
  18.         }  
  19.     });  
  20. }  
Author object will be appended in the result,

Ebook Download
View all
Learn
View all