Using the CrossListQueryInfo and CrossListQueryCache

Lately, I am relatively a lot working with the CrossListQueryInfo. This is a way to query multiple lists at once, crossing multiple SPWebs, if you want to. And, the main reason why I used it, it's possible to use the cached lists of the sitecollection, to improve performance! The first steps with this CrossListQueryInfo can be quite frustrating, as there isnt too much documentation to find on.

I will start with a code sample, to show how to use this to query multuple webs to retrieve Items with the contentType News Item OR Location News Item from the publishing pages. When you are going to the CrossListQueryInfo, it´s smart to make use of U2U´s Caml Query Builder, to quickly build up thecaml that you want use.
Things to keep in mind while using the CrossListQueryInfo:

  • The results that are returned are ONLY PUBLISHED items. I was breaking my head around this one, because I was sure that my query was good, the CAML Query builder also returned a result, but the CrossListQueryCache didn't. After publishing, all the results showed up properly
  • ViewFields: Insert the fields that you want to see. If there is a field inside that doesnt exist in the list that you query, your result will be nill, nada, nothing. Make sure that you put in the INTERNAL field names!
  • RowLimit: When filling in value '0', you won't see any results
  • When using the CrossListQueryCache , make sure not to use the GetSiteData that uses the SPWeb param: GetSiteData(SPWeb web) and GetSiteData(SPWeb web, SPSiteDataQuery query) DO NOT use caching!!
  • and make sure to use Microsoft.SharePoint.WebPartPages.WebPart! This WebPart inherits from System.Web.UI.WebControls.WebParts.WebPart and enhances it with some extra functionality for connected webparts, client-side programming and data caching!! Thanks to Waldek Mastykarz for sharing this info.

Below is an example of a query that queries all the Publishing pages libraries that resides in the current SPWeb and all childWebs.

private DataTable ExampleQuery()

        {

            clqi = new CrossListQueryInfo();

 

            // Insert the list types that you want to use. In this case, its the publishing page library (850, see code below)

            clqi.Lists = "<Lists ServerTemplate=\"" + (int)ListServerTemplateCodes.PageLibrary + "\" />";

 

            // Insert the fields that you want to see. If there is a field inside that doesnt exist in the list that you query, your result will be nill, nada, nothing.

            // Make sure that you put in the INTERNAL field names!

            clqi.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"FileLeafRef\" /><FieldRef Name=\"Nieuws_x0020_Type\" /><FieldRef Name=\"Nieuws_x0020_Leverancier\" /><FieldRef Name=\"Uitgelicht\" /><FieldRef Name=\"Created\" /><FieldRef Name=\"Comments\" />";

 

            // scop to use. Another possibility is SiteCollection

            clqi.Webs = "<Webs Scope=\"Recursive\" />";

 

            // turn the cache on

            clqi.UseCache = true;

 

            // if row limit == 0, you will get 0 results

            clqi.RowLimit = 100;

 

            // I know a stringbuilder would be better, but i wanted to show the markup of the query

            clqi.Query = "<OrderBy>" +

                            "<FieldRef Name='Title' />" +

                        "</OrderBy>" +

                        "<Where>" +

                            "<Or>" +

                                "<Eq>" +

                                    "<FieldRef Name='ContentType' />" +

                                    "<Value Type='Text'>News Item</Value>" +

                                "</Eq>" +

                                "<Eq>" +

                                    "<FieldRef Name='ContentType' />" +

                                    "<Value Type='Text'>LocationNews Item</Value>" +

                                "</Eq>" +

                            "</Or>" +

                        "</Where>";

 

            // put the CrossListQueryInfo object into the CrossListQueryCache

            CrossListQueryCache clqc = new CrossListQueryCache(clqi);

 

            // and query the data!

            // make sure: the GetSiteData(SPWeb web) and GetSiteData(SPWeb web, SPSiteDataQuery query) DO NOT use caching!!!

            DataTable tbl = clqc.GetSiteData((SPContext.Current.Site, CrossListQueryCache.ContextUrl());

 

            // return the datatable

            return tbl;

        }

 The enum below can be used to make life a little bit easier. I got it from: http://www.aspenhorizons.com/devblog/?p=29

public enum ListServerTemplateCodes

    {

        GenericList = 100,

        DocumentLibrary = 101,

        Survey = 102,

        LinksList = 103,

        AnnouncementsList = 104,

        ContactsList = 105,

        EventsList = 106,

        TasksList = 107,

        DiscussionBoard = 108,

        PictureLibrary = 109,

        DataSources = 110,

        SiteTemplateGallery = 111,

        UserInformationList = 112,

        WebPartGallery = 113,

        ListTemplateGallery = 114,

        XMLFormLibrary = 115,

        MasterPagesGallery = 116,

        NoCodeWorkflows = 117,

        CustomWorkflowProcess = 118,

        WikiPageLibrary = 119,

        CustomGridForAList = 120,

        DataConnectionLibrary = 130,

        WorkflowHistory = 140,

        GanttTasksList = 150,

        MeetingSeriesList = 200,

        MeetingAgendaList = 201,

        MeetingAttendeesList = 202,

        MeetingDecisionsList = 204,

        MeetingObjectivesList = 207,

        MeetingTextBox = 210,

        MeetingThingsToBringList = 211,

        MeetingWorkspacePagesList = 212,

        PortalSitesList = 300,

        BlogPostsList = 301,

        BlogCommentsList = 302,

        BlogCategoriesList = 303,

        PageLibrary = 850,

        IssueTracking = 1100,

        AdministratorTasksList = 1200,

        PersonalDocumentLibrary = 2002,

        PrivateDocumentLibrary = 2003

    }

 

more information about this subject can be found at:

http://sharepoint.nailhead.net/2008/04/musing-on-crosslistquerycache-class.html

 

Published Fri, Mar 27 2009 10:56 AM by Bas

Comments

# re: Using the CrossListQueryInfo and CrossListQueryCache

Sunday, April 26, 2009 6:39 AM by Anderson

I tried th code:

CrossListQueryInfo crossListInfo = new CrossListQueryInfo();

                   crossListInfo.Lists = "<Lists ServerTemplate=\"" + (int)ListServerTemplateCodes.Survey + "\" />";

                   crossListInfo.ViewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"Created\" />";

                   crossListInfo.Webs = "<Webs Scope=\"Recursive\" />";

                   crossListInfo.WebUrl = SPContext.Current.Site.ServerRelativeUrl;

                   crossListInfo.UseCache = true;

                   crossListInfo.RowLimit = 100;

                   crossListInfo.Query = "<OrderBy><FieldRef Name=\"Title\" Ascending=\"FALSE\" /></OrderBy>";

                   CrossListQueryCache xlqCache = new CrossListQueryCache(crossListInfo);

                   dtResult = xlqCache.GetSiteData(SPContext.Current.Site, CrossListQueryCache.ContextUrl());

always no result... i sure i created suvery lists.

# re: Using the CrossListQueryInfo and CrossListQueryCache

Thursday, May 28, 2009 8:32 AM by Bas

Did you put any items into the list?

# CrossListQueryInfo not able to return MultiLookup Fields

Friday, August 14, 2009 4:16 PM by BBB - Bas Blogging 'Bout.Net

Some time ago I wrote a blogpost about how to use the CrossListQueryInfo and CrossListQueryCache to be

# re: Using the CrossListQueryInfo and CrossListQueryCache

Tuesday, October 13, 2009 12:32 PM by Julien

HI,

I would like a query for customs lists. Is it possible ?

# re: Using the CrossListQueryInfo and CrossListQueryCache

Wednesday, October 14, 2009 10:23 AM by Bas

Yes, that is possible, if you have a servertemplate ID. If you couple a ContentType to that list, you can just query for the CT too.

# re: Using the CrossListQueryInfo and CrossListQueryCache

Thursday, June 02, 2011 7:37 PM by Ronak

Thanks for Sharing Information.I was trying to follow your example in Console application but getting error Object reference is null

can u please advise me

Thanks

Ronak

# re: Using the CrossListQueryInfo and CrossListQueryCache

Friday, June 03, 2011 1:49 PM by Bas

@Ronak, where in the code did you get that error? Can't help you this way

# re: Using the CrossListQueryInfo and CrossListQueryCache

Monday, June 06, 2011 1:29 PM by Ronak

@Bas Thanks for Quick reply.Please find the code

using (SPSite siteColl = new SPSite("spdev-art-sci/news-events"))

               {

                   using (SPWeb web = siteColl.OpenWeb())

                   {

                       DateTime startdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                       string offset = startdate.Subtract(DateTime.Now).Days.ToString();

                       CrossListQueryInfo Query = new CrossListQueryInfo();

                       Query.Lists = "<Lists ServerTemplate=\"106\"/>";

                       Query.Webs = "<Webs Scope=\"SiteCollection\" />";

                       Query.WebUrl = web.ServerRelativeUrl;

                       Query.UseCache = true;

                       Query.Query = "<OrderBy>" +

                                         "<FieldRef Name='EventDate' Ascending='True' />" +

                                     "</OrderBy>" +

                                     "<Where>" +

                                           "<And>" +

                           //  "<Geq><FieldRef Name='EndDate'/><Value Type='DateTime'><Today OffsetDays='" + offset + "' /></Value></Geq>" +

                                                  "<Eq><FieldRef Name='Public'/><Value Type='Text'>1</Value></Eq>" +

                                                  "<Eq><FieldRef Name='ContentType'/><Value Type='Text'>Event Room Management</Value></Eq>" +

                                           "</And>" +

                                     "</Where>";

                       Query.ViewFields += "<FieldRef Name='ID' />";

                       Query.ViewFields = "<FieldRef Name='Title' />";

                       Query.ViewFields += "<FieldRef Name='EventDate' />";

                       Query.ViewFields += "<FieldRef Name='EndDate' />";

                       Query.ViewFields += "<FieldRef Name='Location' />";

                       Query.ViewFields += "<FieldRef Name='Event_x0020_Description' />";

                       Query.ViewFields += "<FieldRef Name='Event_x0020_Category' />";

                       Query.ViewFields += "<FieldRef Name='fRecurrence' />";

                       Query.ViewFields += "<FieldRef Name='fAllDayEvent' />";

                       Query.ViewFields += "<FieldRef Name='EventType' />";

                       /*Query.ViewFields += "<ProjectProperty Name=\"Title\" />";

                       Query.ViewFields += "<FieldRef Name='RecurrenceData' />";

                       Query.ViewFields += "<FieldRef Name='XMLTZone' />";

                       Query.ViewFields += "<FieldRef Name='EventType' />";

                       Query.ViewFields += "<FieldRef Name='MasterSeriesItemID' />";

                       Query.ViewFields += "<FieldRef Name='RecurrenceID' />"; */

                      // Query.RowLimit = 100;

                       CrossListQueryCache clqc = new CrossListQueryCache(Query);

                       dt = clqc.GetSiteData(siteColl); // Get error here

Thanks

ronak

# re: Using the CrossListQueryInfo and CrossListQueryCache

Tuesday, June 07, 2011 4:51 PM by Ronak

@Bas Naver mind it works in webpart.

Thanks

Ronak

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 4 and 2 and type the answer here: