This is alternate option to SPSiteDataQuery.
The CrossListQueryInfo class is another mechanism you can use to fetch the List Items from multiple sites. This class internally uses the SPSiteDataQuery class to actually fetch the items from the database and when the items are returned, it stores them in the object cache of the Publishing Infrastructure. When any more calls to the same data are made subsequently, then the data is returned from the cache itself without making any more trips to the database.
The working of the CrossListQueryInfo class largely depends on the object cache of the Publishing Features of SharePoint server. So you cannot use this class in SharePoint 2010 Foundation or in sandbox solutions. Also, the default expiry time of the object cache is set to 60 seconds. So you might want to change that time depending upon your environment requirements.
Here is the same code for using the CrossListQueryInfo class:
|CrossListQueryInfo query = new CrossListQueryInfo();|
|query.ViewFields = “<FieldRef Name=\”Title\” /><FieldRef Name=\”DueDate\” />”;|
|query.Query = @”<Where>|
|<Value Type=’User’>” +|
|query.Lists = “<Lists ServerTemplate=\”107\” MaxListLimit=\”0\”/>”; //Tasks Lists|
|query.Webs = “<Webs Scope=\”Recursive\” />”;|
|//query.RowLimit = 100;|
|//Make sure to set this property as true.|
|query.UseCache = true;|
|CrossListQueryCache cache = new CrossListQueryCache(query);|
|//Make sure to use one of the overloads of the GetSiteData method which takes in the SPSite parameter|
|//and not the SPWeb parametre.|
|DataTable results = cache.GetSiteData(SPContext.Current.Site);|
Make sure to set the CrossListQueryInfo.UseCache as true if you want to use the caching features. Another very important thing to mention is that there are 4 overloads of the CrossListQueryCache.GetSiteData method and only 2 of them support caching.
So only use the methods which accepts the SPSite object as one of the parameters if you want to use caching in your code.