Step by Step use of CrossListQueryInfo for cross site and cross list in SharePoint 2010 and SharePoint 2013

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>
<FieldRef Name=’AssignedTo’/>
<Value Type=’User’>” +
+ @”</Value>
<FieldRef Name=’Status’/>
<Value Type=’Choice’>Completed</Value>
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.


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s