| Paul's profilePaul Galvin's SharePoint...BlogListsSkyDrive | Help |
|
February 16 Solution: SPQuery Does Not Search FoldersThis 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:
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:
I instead used a constructor that specified a view:
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:
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:
</end> Technorati Tags: SharePoint Development, SharePoint Search Comments (3)
Paul Galvin
has turned off comments on this page.
TrackbacksThe trackback URL for this entry is: http://paulgalvin.spaces.live.com/blog/cns!1CC1EDB3DAA9B8AA!738.trak Weblogs that reference this entry
|
|
|