Thursday, May 14, 2020

How we can get the lookup column value using rest API?

Ans:- We can get the value of lookup column by using $expand keyword.

var Url=_spPageContextInfo.siteAbsoluteUrl+"/_api/web/lists(guid'<guid>')/items?$select=Id,Title,LookupField1/FieldToBeExpanded1,LookupField2/FieldToBeExpanded2&$expand=LookupField1,LookupField2"

When creating a lookup field, you can select which field from source list you want to show. The number of such fields is limited by field type. For example, you can create a lookup with lookup field pointed only to the following field types:

Single line of text
Date and Time
Number

Thus in your REST query, you can select additional fields only with above-mentioned field types. For example, you have a list Category with fields Title, Id, Active (custom Yes\No field). You created a lookup to Category in your other list (employee).
/_api/web/lists/getbytitle('employee')/items?$select=category/Id,category/Title&$expand=category - works

and

/_api/web/lists/getbytitle('employee')/items?$select=category/Id,category/Title,category/Active&$expand=category - doesn't work because Yes\No is unsupported field type for lookup.

No comments:

Post a Comment