Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Weblog Ton Stegeman [MVP] > Posts > Querying SharePoint for content - using SPSiteDataQuery and CrossListQueryInfo
Querying SharePoint for content - using SPSiteDataQuery and CrossListQueryInfo

When I was writing the querying part of the new Content By Type webpart that I will introduce shortly, I noticed there are 2 ways you can use to query SharePoint sites for content. The first is well known and described in a lot of articles on the web. In the method we use the SPSiteDataQuery object to get the data we want. Below you find an example.

    string lists = "<Lists BaseType=\"5\" />";
    string viewFields = "<FieldRef Name=\"Title\" />";
    string webs = "<Webs Scope=\"SiteCollection\" />";
    SPSiteDataQuery siteQuery = new SPSiteDataQuery();
    siteQuery.Lists = lists;
    siteQuery.ViewFields = viewFields;
    siteQuery.Webs = webs;
    results = SPContext.Current.Web.GetSiteData(siteQuery);

This query returns the title of all items in Issue lists in all sites in the current site collection. Nothing special here. This method of crawling for content works in both WSS and in MOSS. If you are running Microsoft Office SharePoint Servers, you can also use the CrossListQueryInfo object to query for content. The advantage is that SharePoint has a caching mechanism for the queries that you run. By using CrossListQueryInfo, your webpart will use this caching mechanism. And you can also make use of audience targeting. See this link for more info.

The query of the previous example now looks like this:

    string lists = "<Lists BaseType=\"5\" />";
    string viewFields = "<FieldRef Name=\"Title\" /><FieldRef Name=\"ContentType\" />";
    string webs = "<Webs Scope=\"SiteCollection\" />";
    CrossListQueryInfo query = new CrossListQueryInfo();
    query.RowLimit = 100;
    query.WebUrl = SPContext.Current.Site.ServerRelativeUrl;
 
    query.Lists = lists;
    query.Webs = webs;
    query.Query = "<Where><Neq><FieldRef Name=\"ContentType\" /><Value Type=\"Text\"></Value></Neq></Where>";
    query.ViewFields = viewFields;
 
    CrossListQueryCache cache = new CrossListQueryCache(query);
 
    results = cache.GetSiteData(SPContext.Current.Site);

Please not that for this query I added a where statement to the query. This just checks for items that have a value in the ContentType field. If you don't specify a query, the GetSiteData method will generate an error message.

A warning if you are querying your sites using field names instead of IDs. When testing my Content By Type webpart, I started querying using the field names (SPField.InternalName) that I got from the site columns that are attached to the content type. I found that there are a number of issues with this. The internal name of the site columns are not exactly the same as the internal names of the fields that are used in the lists. This is caused by little differences in the site column features and the schema XML files for the lists. the Task content type for example contains a field with internal name "TaskDueDate" The Task lists in your sites however have a field called "DueDate". If you query using TaskDueDate, you will not get the results you expeect. Below you find these exceptions:

  • Task: TaskDueDate in content type is called DueDate in list
  • Task: TaskStatus in content type is called Status in list
  • Issue: IssueStatus in content type is called Status in list
  • Contact: EMail in content type is called Email in list
  • Picture: Comments in content type is called Description in list

So I ended up querying using the ID guid of the fields. This applies to both methods described above.

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).