Querying Document Libraries in WSS3#

In WSS2, we've ran into several problems when it comes to querying document libraries with the SPQuery class. The problem with this method is, that it works great on lists, except is cannot deal very well with folders in a list (Document Library, Forms Library, etc). From the SDK:

"SPQuery can only get the items in the current folder; SPQuery will not get the items in subfolders. This is the design feature in SharePoint Portal Server 2003. To get the items in subfolders, it is necessary to change to the subfolder in SPQuery first. Also it is necessary to specify the format for using dates in the query. (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tscSPQuery_SV01013584.asp) "

One of the limitations of document libraries, is that the performance will drop when putting more then 1000 documents in a folder (probably because of the view). Especially when putting a large number of documents in a library, you will have to use folders.

No problem so far, but looking at the SPQuery class, it will only return listitems per folder. So, when using folders, you will have to recursively look in all folders and subfolders, to query all the documents. For 20 folders this will mean you will have to perform 21 queries, a big problem when it comes to performance!

So, this made me think there should be a solution for this problem. Steve triggered me with his article about capacity planning in WSS 3, to look more into the problem. And guess what, the article says 2000 documents per folder, not a big improvement there. And looking at the SPQuery class in WSS3, it still has the same bevaviour when it comes to document libraries.

Then I remembered Patrick telling about a new class, called the SPSiteDataQuery, which can be used to query data in site collections. Great invention, which is also the solution for querying a complete document library at once!

SPSite site = SPControl.GetContextSite(this.Context); SPWeb web = site.OpenWeb(); SPSiteDataQuery qry = new SPSiteDataQuery(); string date = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today); qry.Query = "<orderby><fieldref name="Title"></orderby>" + "<where><geq><fieldref name="Created">" + "<value type="DateTime">" + date + "</value></geq></where>"; qry.Lists = "<lists servertemplate="101">"; qry.ViewFields = "<fieldref name="Title">"; qry.Webs = "<webs scope="SiteCollection">"; DataTable tbl = web.GetSiteData(qry);Great way to do more with queries, expecially when it comes to document libraries (and many other scenario's)!
Monday, October 23, 2006 6:34:53 AM UTC #     | 

 

All content © 2012, Mart Muller
On this page
This site
Calendar
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Archives
Sitemap
Disclaimer

Powered by: newtelligence dasBlog 1.9.7174.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts