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 =

      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 +=

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.


 Subscribe to my blog!

3 thoughts on “Solution: SPQuery Does Not Search Folders

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


Leave a Reply

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