Solution: SPQuery Does Not Search Folders

This past week I was implementing an "evolving" solution for a client that uses BDC and SPQuery and ran into some difficulty using SPQuery against a document library containing folders.  Bottom line: assign "recursive" to the view attribute of the query.

My scenario:

  • On Monday, I upload a document and supply some meta data.
  • The following week, I upload a new document.  Much of this new document’s meta data is based on the document I uploaded on Monday (which we call the "master document").
  • We’ve created a web service façade that provides a BDC-friendly interface to the list so that users can easily locate that Monday document via a title search.
  • A BDC data column provides a friendly user interface.  (This is part of my attempt at using BDC for a more friendly Lookup column).

The final BDC façade service uses a query like this to do the lookup:

 // Used U2U tool to assist in generating this CAML query.
      oQuery.Query =
        "<Where>";

      if (titleFilter.Length > 0)
        oQuery.Query +=
          "  <And>";

      oQuery.Query +=
        "    <And>" +
        "      <Geq>" +
        "        <FieldRef Name=\"DocumentId\" />" +
        "        <Value Type=\"Text\">" + minId + "</Value>" +
        "      </Geq>" +
        "      <Leq>" +
        "        <FieldRef Name=\"DocumentId\" />" +
        "        <Value Type=\"Text\">" + maxId + "</Value>" +
        "      </Leq>" +
        "    </And>";

      if (titleFilter.Length > 0)
        oQuery.Query +=
          "    <Contains>" +
          "      <FieldRef Name=\"Title\" />" +
          "      <Value Type=\"Text\">" + titleFilter + "</Value>" +
          "    </Contains>" +
          "  </And>";
      oQuery.Query +=
        "</Where>";

During the initial stage of development, this worked great.  However, we introduced folders into the directory to solve some problems and suddenly, my BDC picker wouldn’t return any results.  I tracked this down to the fact that the SPQuery would never return any results.  We used folders primarily to allow multiple files with the same name to be uploaded but with different meta data.  When the file is uploaded, we create a folder based on the list item’s ID and then move the file there (I wrote about that here; we’ve had mixed results with this approach but on the whole, it’s working well).  The user don’t care about folders and in fact, don’t really understand that there are any folders.  We have configured all the views on the library to show items without regard to folders.

I hit this problem twice as the technical implementation evolved and solved it differently each time.  The first time, I wasn’t using the CONTAINS operator in the query.  Without a CONTAINS operator, I was able to solve the problem by specifying the view on the SPQuery’s contructor.   Instead of using the default constructor:

SPList oList = web.Lists["Documents"];

SPQuery oQuery = new SPQuery();

I instead used a constructor that specified a view:

SPList oList = web.Lists["Documents"];

SPQuery oQuery = new SPQuery(oList.Views["All Documents"]);

That solved the problem and I started to get my results.

I then added the CONTAINS operator into the mix and it broke again.  It turns out that the CONTAINS operator, so far as I can tell, does not work with the view the same way as the a simpler GEQ / LEQ operators.  I did some searching and learned that the query’s ViewAttributes should be set to "Recursive", as in:

oQuery.ViewAttributes = "Scope=\"Recursive\"";

That solved the problem for CONTAINS.  In fact, this also solved my original search problem and if I had specified the recursive attribute the first time, I would not have run into the issue again.

The fact that a view-based SPQuery works for some operators (GEQ/LEQ) and not others (CONTAINS), coupled with the fact that KPIs don’t seem to work at all with folder-containing document libraries leads me to believe that SPQuery has some orthogonality issues.

Special Thanks:

  • The good folks at U2U and their query tool.
  • Michael Hoffer’s great "learning by doing" blog post, comments and responses.

</end>

 Subscribe to my blog!

3 thoughts on “Solution: SPQuery Does Not Search Folders

  1. No name

    http://www.batteryfast.co.uk/asus/w3v.htm asus w3v battery,
    http://www.batteryfast.co.uk/asus/w3000a.htm asus w3000a battery,
    http://www.batteryfast.co.uk/asus/w3000.htm asus w3000 battery,
    http://www.batteryfast.co.uk/asus/w3000v.htm asus w3000v battery,
    http://www.batteryfast.co.uk/asus/a31-s5.htm asus a31-s5 battery,
    http://www.batteryfast.co.uk/asus/a32-s5.htm asus a32-s5 battery,
    http://www.batteryfast.co.uk/asus/s52n.htm asus s52n battery,
    http://www.batteryfast.co.uk/asus/s5000.htm asus s5000 battery,
    http://www.batteryfast.co.uk/asus/s5200n.htm asus s5200n battery,
    http://www.batteryfast.co.uk/asus/s5n.htm asus s5n battery,
    http://www.batteryfast.co.uk/asus/90-naa1b1000.htm asus 90-naa1b1000 battery,
    http://www.batteryfast.co.uk/asus/s2691061.htm asus s2691061 battery,
    http://www.batteryfast.co.uk/asus/a42-v6.htm asus a42-v6 battery,
    http://www.batteryfast.co.uk/asus/a32-u5.htm asus a32-u5 battery,
    http://www.batteryfast.co.uk/asus/u5f.htm asus u5f battery,
    http://www.batteryfast.co.uk/asus/u5a.htm asus u5a battery,
    http://www.batteryfast.co.uk/asus/u5f.htm asus u5f battery,
    http://www.batteryfast.co.uk/asus/u5.htm asus u5 battery,
    http://www.batteryfast.co.uk/compaq/nw8000.htm compaq nw8000 battery,
    http://www.batteryfast.co.uk/compaq/nc8000.htm compaq nc8000 battery,
    http://www.batteryfast.co.uk/compaq/v1000.htm compaq v1000 battery,
    http://www.batteryfast.co.uk/compaq/nc6000.htm compaq nc6000 battery,
    http://www.batteryfast.co.uk/compaq/nx5000.htm compaq nx5000 battery,
    http://www.batteryfast.co.uk/compaq/n600.htm compaq n600 battery,
    http://www.batteryfast.co.uk/compaq/n600c.htm compaq n600c battery,
    http://www.batteryfast.co.uk/compaq/n610c.htm compaq n610c battery,
    http://www.batteryfast.co.uk/compaq/n610v.htm compaq n610v battery,
    http://www.batteryfast.co.uk/compaq/n620c.htm compaq n620c battery,
    http://www.batteryfast.co.uk/compaq/e500.htm compaq e500 battery,
    http://www.batteryfast.co.uk/compaq/e500s.htm compaq e500s battery,
    http://www.batteryfast.co.uk/compaq/v300.htm compaq v300 battery,
    http://www.batteryfast.co.uk/compaq/v500.htm compaq v500 battery,
    http://www.batteryfast.co.uk/compaq/pp2060.htm compaq pp2060 battery,
    http://www.batteryfast.co.uk/compaq/nx9000.htm compaq nx9000 battery,
    http://www.batteryfast.co.uk/compaq/nx9005.htm compaq nx9005 battery,
    http://www.batteryfast.co.uk/compaq/nx9010.htm compaq nx9010 battery,
    http://www.batteryfast.co.uk/compaq/nx9020.htm compaq nx9020 battery,
    http://www.batteryfast.co.uk/compaq/n110.htm compaq n110 battery,
    http://www.batteryfast.co.uk/compaq/n110s.htm compaq n110s battery,
    http://www.batteryfast.co.uk/compaq/pp2101x.htm compaq pp2101x battery,
    http://www.batteryfast.co.uk/compaq/presario-2100.htm compaq presario 2100 battery,
    http://www.batteryfast.co.uk/compaq/ze4000.htm compaq ze4000 battery,
    http://www.batteryfast.co.uk/compaq/f4809a.htm compaq f4809a battery,
    http://www.batteryfast.co.uk/compaq/f4812a.htm compaq f4812a battery,
    http://www.batteryfast.co.uk/dell/inspiron-700m.htm dell inspiron 700m battery,
    http://www.batteryfast.co.uk/dell/inspiron-710m.htm dell inspiron 710m battery,
    http://www.batteryfast.co.uk/dell/latitude-x200.htm dell latitude x200 battery,
    http://www.batteryfast.co.uk/dell/8u443.htm dell 8u443 battery,
    http://www.batteryfast.co.uk/dell/312-0058.htm dell 312-0058 battery,

    Reply

Leave a Reply to Paul Galvin Cancel reply

Your email address will not be published. Required fields are marked *