6

weird but i can't figure this out or find a solution anywhere....

I want to filter on two values on a GET in REST URL from javascript. So since this is from ECMA I am just using filtering in the url...

something like:

/_vti_bin/ListData.svc/Table?$filter=Color eq 'Red'&Active eq false

doesn't work so I have tried:

/_vti_bin/ListData.svc/Table?$filter=((Color eq 'Red') and (Active eq false)
/_vti_bin/ListData.svc/Table?$filter=Color eq 'Red' and Active eq false
etc

i know that you can only use one operator at a time in a URL but I not sure how to set two or three field filters in one URL like this

appreciate any help.

1

2 Answers 2

6

Everywhere in this answer replace sp2010 with your site url, Table with your list name, PortalHomeDataContext with your site name (without spaces) + DataContext and RestTest.ServiceReference1 with your generated namespace.

This is tested against a SharePoint 2010 site:

http://sp2010/_vti_bin/listdata.svc/Table?$filter=(Color eq 'Red') and (Active eq false)

A very efficient way of getting the syntax (provided you know C# and LINQ) is the following:

  1. Create a Console Application using visual studio
  2. Add a service reference to http://sp2010/_vti_bin/listdata.svc
  3. Change to console application to something like this:

    using System;
    using System.Linq;
    using RestTest.ServiceReference1;
    
    namespace RestTest
    {
      class Program
      {
        static void Main(string[] args)
        {
          var context = new PortalHomeDataContext(new Uri("http://sp2010/_vti_bin/listdata.svc"));
          context.Credentials = System.Net.CredentialCache.DefaultCredentials;
          var data = from d in context.Table
                     where d.Color == "Red" && d.Active == false
                     select d;
          foreach (var row in data)
          {
              Console.WriteLine(row.Title);
          }
        }
      }
    }
    
  4. Now start Fiddler (download from http://fiddler2.com/) and run the console app. Fiddler will show the url generated by the console app.

3
  • Just to tack on a tool I find useful with Chrome is Dev HTTP Client. It hav a very simple interface for composing HTTP requests. chrome.google.com/webstore/detail/dev-http-client/… Commented Jun 26, 2013 at 19:09
  • Very cool! C# yes, Linq I am a little less familiar with however it's one of those on the list to learn type things so...thanks!
    – Justin
    Commented Jun 28, 2013 at 10:36
  • Also just the general tested URL you provided worked for me which I appreciate, but even more so the examples for finding URL from console!
    – Justin
    Commented Jun 28, 2013 at 10:38
4

I tried this using the 2013 api in SharePoint online, but I think the filter syntax should work the same...

You need to specify your boolean field as 1 or zero, like this...

https://mysite.sharepoint.com/_api/web/lists/getbytitle('defects')/items?$select=Color,Title,ID&$filter=Color eq 'Red' and Active eq 1&$top=100

Also, make sure you are using internal names, not display names.

2
  • I used the ODATA Query Builder from the SharePoint Online App Store to construct this. An awesome tool, although it does not seem to support boolean fields (And no, I am not affiliated with it). Commented Jun 26, 2013 at 17:54
  • Thanks...I assume that tool is only available in SP2013 since you mentioned app store? The link you provided worked but so did false from previous answer...I suppose at least in sp2010you can use either?
    – Justin
    Commented Jun 28, 2013 at 10:40

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.