Paul Galvin's (old) SharePoint space [SharePoint

Just another site

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 responses to “Solution: SPQuery Does Not Search Folders

  1. Charles February 28, 2008 at 7:25 pm

    You do know that StringBuilder is best practice for creating long strings, right?

  2. Paul February 28, 2008 at 7:46 pm

    Thanks, noname, for the remedial tip.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: