2

I have 2 lists: Questions and Answers. Questions has a single lookup to Answers. Answers has a single choice column called Category. I would like to query Questions, expand its Answer and select the Answers Category.

I thought this would be simple like it was in 2010 and tried these:

/_api/web/lists/getByTitle('Questions')/items?$filter=Id eq 2&$expand=Answer&$select=Id,Answer/Category

/_api/web/lists/getByTitle('Questions')/items?$filter=Id eq 2&$expand=Answer&$select=Id,Answer/CategoryValue

but I get the error:

The query to field 'Answer/Category' is not valid.`

How can this be done in the new REST APIs?

2 Answers 2

4

In SharePoint, the lookup column cannot "lookup" choice type , so the error would occur when using REST API to get the choice column with lookup column. However, the lookup column can lookup values in calculated column, so you can get calculated column value with same API.

So if you want to get the choice column value, you need to get the ID of the item first with the REST API and then use item ID to get the choice column or use a calculated column instead.

FYI - You can only retrieve text, number or date values through lookup field similar to when you create a lookup column via UI and choose what additional properties you want to include.

3

There is no option to retrieve choice field data directly from the “lookup” list. To get the “Category” data follow below steps:

  1. Create a “Calculated” field in the “Answers” list. In this example I created a new field called “CategoryCalculated”.
  2. Now double click on “Category” from the options within “Insert Column” options box.
  3. Now click the “OK” button in the bottom of the page.
  4. “CategoryCalculated” value will be same as “Category”. Now you can retrieve the new field “CategoryCalculated” using your query. Your query will be like this: /_api/web/lists/getByTitle('Questions')/items?$select=Id,Answers/CategoryCalculated&$filter=Id eq 2&$expand=Answers

Check the below screenshot: enter image description here


If the answer is helpful, please upvote and mark as accepted.

My Fiverr and Upwork profiles: https://www.fiverr.com/nizam5567 https://www.upwork.com/freelancers/nizamuddin

1
  • 1
    @NIzam_Uddin it's good answer Oct 16, 2019 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.