Skip to main content

How to Filter a SharePoint REST Request with $filter

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A SharePoint REST request for a collection, such as a site's lists, returns all of it unless you filter it. $filter adds a condition to the address, and the answer holds only what matches. This guide lists only a site's visible custom lists in the browser.

Syntax​

General form
<collection address>?$filter=<field> <operator> <value>
  • <collection address>: a SharePoint REST address that returns several things, such as https://<site address>/_api/web/lists or a list's …/items.
  • <field>: the field to test, by its internal name.
  • <operator>: how to compare, such as eq (equal) or ne (not equal).
  • <value>: what to compare with. Text goes in single quotes, 'Suggestions'; numbers and true or false don't.

Join two conditions with and, and other query options with &.

Microsoft's Use OData query operations in SharePoint REST requests says: Use the $filter query option to select which items to return. Its table of operators lists what SharePoint supports:

SupportedNot supported
Number comparisons: lt, le, gt, ge, eq, neArithmetic: add, sub, mul, div, mod
Text comparisons: startswith(), substringof(), eq, neendswith, replace, substring, tolower, toupper, trim, concat
Date and time functions, such as day() and year()

The Example​

The root site contoso.sharepoint.com has its own hidden lists as well as the lists people made. The address keeps only the lists that are visible and made from the custom list template, and returns their titles:

  • BaseTemplate eq 100: made from the custom list template. Microsoft's ListTemplateType enum gives GenericList the number 100.
  • Hidden eq false: not hidden.
  • $select=Title: only each list's title.

Prerequisites​

  • Access to the site and a signed-in SharePoint session in the same browser. The browser sends your sign-in with the request.

Step 1: Open the Address​

In the browser where you use SharePoint, type the address in the address bar and press Enter:

Browser address bar
https://contoso.sharepoint.com/_api/web/lists?$filter=BaseTemplate eq 100 and Hidden eq false&$select=Title

Use your own site's address in place of contoso.sharepoint.com. The browser turns the spaces into %20 when it sends the address.

Step 2: Read the Answer​

The browser sends no Accept header asking for JSON, so SharePoint answers in XML. In my browser it appeared as plain text:

The browser showing SharePoint's XML feed with three entries, each with m holding one d:Title: Announcements, Expense Claims and Suggestions; the lists' IDs are masked

Each entry is one list that matched both conditions:

Entryd:Title
1Announcements
2Expense Claims
3Suggestions

The site's hidden lists and its libraries aren't there: they failed one of the two conditions.

Common Mistakes​

MistakeWhat happensDo this instead
Using = instead of eqIt isn't an OData operatorWrite eq
Text without quotes, Title eq SuggestionsThe value isn't read as textQuote it: Title eq 'Suggestions'
A second ? before $selectOnly one ? starts the query optionsJoin it with &
Using a function from the unsupported column, such as endswithSharePoint doesn't support it in $filterUse startswith() or substringof()

Troubleshooting​

SymptomLikely causeFix
Nothing comes backNo entry matches every conditionTest the conditions one at a time
Hidden lists come backHidden eq false is missingAdd it with and
A sign-in page or an access errorThe browser isn't signed in, or you can't open the siteSign in to SharePoint in the same browser

FAQ​

Can I use $filter on list items?​

Yes. …/lists/getbytitle('<list name>')/items?$filter=<column> eq '<value>' returns the items whose column has that value. Use the column's internal name.

Can I filter on a person?​

Microsoft's article says users are matched by their display name, and that membership-based queries aren't supported.

Is this safe to try on a real site?​

Yes. Opening an address in the browser sends a GET request, which only reads.

Conclusion​

Add ?$filter=<field> <operator> <value> to a SharePoint REST address, joining conditions with and and other options with &. …/lists?$filter=BaseTemplate eq 100 and Hidden eq false&$select=Title returned the site's three visible custom lists.

Additional Resources​