SOLVED

Rest api search query --- Lookup field issue

%3CLINGO-SUB%20id%3D%22lingo-sub-55103%22%20slang%3D%22en-US%22%3ERest%20api%20search%20query%20---%20Lookup%20field%20issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55103%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20document%20library%20which%20has%20a%20custom%20field%20%22Label%22%20this%20Label%20field%20is%20a%20lookup%20field%20(to%20a%20list)%20multiple%20values%20allowed.%20For%20example%20my%20documentlibrary%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETitle%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Label%3C%2FP%3E%3CP%3Edoc1%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20labelY%2C%20labelX%3C%2FP%3E%3CP%3Edoc2%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20labelX%3C%2FP%3E%3CP%3Edoc3%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20labelY%2C%20labelZ%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20I%20want%20to%20create%20a%20rest%20query%20to%20filter%20documents%20by%20Label%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20far%20I%20have%20this%3A%3C%2FP%3E%3CPRE%3Ehttps%3A%2F%2Fblabla.sharepoint.com%2Fsites%2Fmike%2Flabel%2F_api%2Fweb%2Flists%2FgetByTitle('Documents')%2Fitems%3F%26amp%3Bselect%3D'Label%252cTitle%252cType'%26amp%3Brefinementfilters%3D'Label%3Acontains('labelX')'%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%2C%20this%20gives%20me%20all%20documents%20in%20the%20library.%20So%20no%20filtering.%3C%2FP%3E%3CP%3EAnd%20when%20I%20check%20the%20xml%20result%20%22Label%22%20is%20referred%20as%20an%20ID%20to%20the%20list%20and%20not%20as%20%22labelX%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20is%20wrong%20in%20my%20approach%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%20Mike%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-55103%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExtensibility%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-82074%22%20slang%3D%22en-US%22%3ERe%3A%20Rest%20api%20search%20query%20---%20Lookup%20field%20issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-82074%22%20slang%3D%22en-US%22%3E%3CP%3EJust%20to%20give%20a%20great%20article%20related%20to%20this%20question%3A%3C%2FP%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fwww.andrewconnell.com%2Fblog%2FApplying-Filters-to-Lookup-Fields-with-the-SP2013-REST-API%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.andrewconnell.com%2Fblog%2FApplying-Filters-to-Lookup-Fields-with-the-SP2013-REST-API%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55377%22%20slang%3D%22en-US%22%3ERe%3A%20Rest%20api%20search%20query%20---%20Lookup%20field%20issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55377%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1815%22%20target%3D%22_blank%22%3E%40Luis%20Ma%C3%B1ez%3C%2FA%3E%2C%20and%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F637%22%20target%3D%22_blank%22%3E%40Brent%20Ellis%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20both!!%20Found%20the%20solution.%3C%2FP%3E%3CP%3ENow%20make%20it%20some%20more%20dynamic%20but%20I%20can%20manage%20that.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20your%20information%2C%20this%20is%20my%20query%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Ehttps%3A%2F%2Fblabla.sharepoint.com%2Fsites%2Fmike%2Flabel%2F_api%2Fweb%2Flists%2FgetByTitle('Documents')%2Fitems%3F%24select%3D*%2CLabel%2FTitle%26amp%3B%24expand%3DLabel%26amp%3B%24filter%3DLabel%2FTitle%20eq%20'labelX'%26amp%3Browlimit%3D500%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55362%22%20slang%3D%22en-US%22%3ERe%3A%20Rest%20api%20search%20query%20---%20Lookup%20field%20issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55362%22%20slang%3D%22en-US%22%3EHaven't%20tested%20it%2C%20but%20it%20should%20work%20with%20the%20%24filter%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%2F_api%2Fweb%2Flists%2FGetByTitle('Documents')%2Fitems%3F%24select%3D*%2CLabel%2FTitle%26amp%3B%24expand%3DLabel%26amp%3B%24filter%3DLabel%2FTitle%20eq%20'labelX'%3CBR%20%2F%3E%3CBR%20%2F%3ELet%20us%20know%20how%20it%20goes.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55115%22%20slang%3D%22en-US%22%3ERe%3A%20Rest%20api%20search%20query%20---%20Lookup%20field%20issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55115%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F637%22%20target%3D%22_blank%22%3E%40Brent%20Ellis%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGreat!!%20This%20works%3A%3C%2FP%3E%3CPRE%3Ehttps%3A%2F%2Fblabla.sharepoint.com%2Fsites%2Fmike%2Flabel%2F_api%2Fweb%2Flists%2FgetByTitle('Documents')%2Fitems%3F%24expand%3DLabel%26amp%3B%24select%3D*%2CLabel%2FTitle%3C%2FPRE%3E%3CP%3EBut%20now%20I%20need%20to%20get%20it%20to%20the%20next%20level.%20I%20want%20to%20filter%20on%20%22LabelX%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESomething%20like%20this%3A%3C%2FP%3E%3CPRE%3Ehttps%3A%2F%2Fblabla.sharepoint.com%2Fsites%2Fmike%2Flabel%2F_api%2Fweb%2Flists%2FgetByTitle('Documents')%2Fitems%3F%24expand%3DLabel%26amp%3B%24select%3D*%2CLabel%2FTitle%26amp%3Brefinementfilters%3D'Label%2FTitle%20eq%20('labelX')%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20suggestions%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55110%22%20slang%3D%22en-US%22%3ERe%3A%20Rest%20api%20search%20query%20---%20Lookup%20field%20issue%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55110%22%20slang%3D%22en-US%22%3EMike%2C%20don't%20have%20a%20ready%20of%20example%20of%20this%2C%20but%20you%20may%20have%20to%20%22expand%22%20it%20to%20get%20the%20Label%20text.%20By%20default%2C%20all%20you%20see%20is%20the%20ID.%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20example%2C%20getting%20%22more%22%20fields%20for%20a%20user%20field%20is%20done%20like%20this%3A%3CBR%20%2F%3E%3CSITE%3E%2F_api%2Fweb%2FLists(guid'%3CLIST%20id%3D%22%22%3E')%2FItems%3F%24expand%3DPERSONFIELD%26amp%3B%24select%3D*%2CPERSONFIELD%2FEMail%3C%2FLIST%3E%3C%2FSITE%3E%3C%2FLINGO-BODY%3E
Highlighted
Regular Contributor

I have a document library which has a custom field "Label" this Label field is a lookup field (to a list) multiple values allowed. For example my documentlibrary:

 

Title          Label

doc1         labelY, labelX

doc2         labelX

doc3         labelY, labelZ

 

Now I want to create a rest query to filter documents by Label

 

So far I have this:

https://blabla.sharepoint.com/sites/mike/label/_api/web/lists/getByTitle('Documents')/items?&select='Label%2cTitle%2cType'&refinementfilters='Label:contains('labelX')'

 

 

However, this gives me all documents in the library. So no filtering.

And when I check the xml result "Label" is referred as an ID to the list and not as "labelX"

 

What is wrong in my approach?

 

Thanks, Mike

 

 

5 Replies
Highlighted
Mike, don't have a ready of example of this, but you may have to "expand" it to get the Label text. By default, all you see is the ID.

For example, getting "more" fields for a user field is done like this:
<site>/_api/web/Lists(guid'<LIST ID>')/Items?$expand=PERSONFIELD&$select=*,PERSONFIELD/EMail
Highlighted

Hi @Brent Ellis

 

Great!! This works:

https://blabla.sharepoint.com/sites/mike/label/_api/web/lists/getByTitle('Documents')/items?$expand=Label&$select=*,Label/Title

But now I need to get it to the next level. I want to filter on "LabelX"

 

 

Something like this:

https://blabla.sharepoint.com/sites/mike/label/_api/web/lists/getByTitle('Documents')/items?$expand=Label&$select=*,Label/Title&refinementfilters='Label/Title eq ('labelX')

 

Any suggestions?

Highlighted
Solution
Haven't tested it, but it should work with the $filter:

/_api/web/lists/GetByTitle('Documents')/items?$select=*,Label/Title&$expand=Label&$filter=Label/Title eq 'labelX'

Let us know how it goes.
Highlighted

Hi @Luis Mañez, and @Brent Ellis,

 

Thank you both!! Found the solution.

Now make it some more dynamic but I can manage that.

 

For your information, this is my query:

 

 

https://blabla.sharepoint.com/sites/mike/label/_api/web/lists/getByTitle('Documents')/items?$select=*,Label/Title&$expand=Label&$filter=Label/Title eq 'labelX'&rowlimit=500

 

Highlighted