All about the Strategy, Design, Customisation, Deployment and Development of SharePoint and its related Technologies

  Administration   All Me!! Baby!!   BDC   Book Review   Business   CKS   Conferences   CQWP   Development   Duffer Moments   Email   Errors   Family   Fixes   General   Groove   How To   How To Code   InfoPath   iPhone   IRM   Longhorn   Lotus Notes   Migration   Mobility   Office System 2007   Personal Projects   Powershell   Records Management   REST Services   Search Server   Security   SEO   SharePoint   SharePoint 2010   Silverlight   SQL   Tech Ed 2008   Testing   Vista   VSTO   WCF   WSS   XSL

[09/09/2007] MOSS2007 – Content Query Web Part (Multiple Content Types)
 
Categories: Development, Office System 2007, SharePoint
 

One of the questions I have been asked a lot recently is how to create a "Latest Updates" web part. Most clients I work with like the Content Query Web Part (CQWP) but don't like the fact that by default it is limited to a single content type. The example I am going to show below will be a content query web part that will list simply two different content types and then style them. There is currently documentation for the CQWP properties on MSDN that can be found below:

http://msdn2.microsoft.com/en-us/library/aa981241.aspx

So to start we are simply going to add a CQWP onto the main portal page. To do this "Edit" the page and click to add a web part. Select the Content query web part as shown below:

When the web part is added to the page by default it renders items that are based on the "Pages" content types. The web part should render as shown below:

Now in order for this to work we need to make a few changes to the actual web part itself. To do this select the "Export" menu item from the web part itself.

Save the web part to your desktop and then open up within an editor such as notepad or visual studio.

Once the file is opened within your editor we will make a few changes to the following sections:

<property name="ListsOverride" type="string" />

<property name="CommonViewFields" type="string" />

<property name="QueryOverride" type="string" />

 

The "ListsOverride" property is used to tell the CQWP what lists to use when rendering content. In the MSDN article you can see that you can use either the "BaseType", "List ID" or use the "ServerTemplate" as the option. In this example we will use the "BaseType" and set it as below:

Changed to:

By setting it to "<List BaseType='1'></Lists>" this will tell the CQWP to only render items from content types based on the base type of document library. This will not show list items. So for this example we only want to see "News" items and "Documents" that have been updated. The next change we need to make is to the "CommonViewFields" section. This is well documented also and is used to tell the CQWP what fields it should be displaying. We want to add the "Created" field so we can sort the data we retrieve. The following changes need to be made:

Changed to:

The next section we need to change is the "QueryOverride". This allows you to "hand-craft" your own CAML query to get data from the system. I am a great fan of using the CAML Builder from U2U; this can be downloaded from the link below:

http://www.u2u.info/SharePoint/U2U%20Community%20Tools/U2U%20CAML%20Query%20Builder%202007%20RTM.zip

I also found that writing your own tool to run CAML queries for testing was a great time saver. I created a simple application that used the "SPSiteDataQuery" method. It is very basic and looks as below:

The application above takes all the parameters that the CQWP takes but lets me test before hand. Anyway I am not here to push my own little two minute applications, so let's continue. Anyway the "QueryOverride" option needs to be changed as below:

Changed to:

The syntax above simply tells the CQWP to query the data based on the "Document" and "Article Page" content types. It also sorts the data in descending order by the "Created" date.

Now we have made the changes we can add this web part back the system. To do this first remove the existing CQWP we added by pressing the close button. Now we could manually import this web part or we could add it to the web part gallery. For this example we will simply import it. Press the add web part button and then press the following link:

Once the right hand web part list has appeared select the arrow shown below:

You will then need to follow the steps shown, browse to the web part file,

And then select upload. Then select the web part zone you wish add it and press import.

As you can see above now we see items stored in document libraries that are set as the default content type of document, hence the images and xsl files (they are in a custom document library) and then the news item which is based on the article page content type. Now we have the correct information showing we need to now style the list so it can show the "Created" date next to the title. To do this I simply created a custom style in the "ItemStyle.xsl" called "Latest Updates". The style is shown below:

As this is a test I have not worries about creating a table-less style but in the real world you would want to create it using "<div>" tags instead. So once you have applied the style now it should render as shown below:

As you can see the CQWP is a great tool for aggregating content from across the SharePoint system. By modifying the properties you can create powerful web parts that once uploaded into the web part gallery will be available for everyone using the system. J

 
12 Comments
 

Comments

Thursday, 13 Sep 2007 01:21 by Bartski
Hi, Great article!!

What if I want to display items from a document library, page library and a custom list.

I tried this by using the listoverride property and the GUID's but have been unsuccesfull.
It only shows the items from the page library and the document library..

Thursday, 13 Sep 2007 10:18 by Rodney Erb
This would be great if I could get it to work.  When I import the CQWP back in I get the message in my CQWP that "There is a problem with the query that this Web Part is issuing. Check the configuration of this Web Part and try again."  When I export the code out, I see the following and not what I created, for example I added
"<property name="QueryOverride" type="string">
<![CDATA[Where><Or>
<Eq><FieldRef Name="ContentType"/><Value Type="Text">Document</Value></Eq>
<Eq><FieldRef Name="ContentType"/><Value Type="Text">Article Page</Value></Eq></Or>
</Where><OrderBy>
<FieldRef Name="Created" Nullable="True" Type="DateTime" Ascending="FALSE"/></OrderBy>
]]>
</property>"

and when I import it back it looks like...

"<property name="QueryOverride" type="string">Where&gt;&lt;Or&gt;
&lt;Eq&gt;&lt;FieldRef Name="ContentType"/&gt;&lt;Value Type="Text"&gt;Document&lt;/Value&gt;&lt;/Eq&gt;
&lt;Eq&gt;&lt;FieldRef Name="ContentType"/&gt;&lt;Value Type="Text"&gt;Article Page&lt;/Value&gt;&lt;/Eq&gt;&lt;/Or&gt;
&lt;/Where&gt;&lt;OrderBy&gt;
&lt;FieldRef Name="Created" Nullable="True" Type="DateTime" Ascending="FALSE"/&gt;&lt;/OrderBy&gt;
</property>"

I'm pretty frustrated and would like to know what could be the problem?  Why would my code be getting "dorked up"?

Any and all help would be appreciated!

Thursday, 13 Sep 2007 10:31 by Rodney Erb
What I tried to send was the "changed code" and the comment was fixed, i.e. it took and made it appear correct in comment 2 - instead of <Eq> the code returned apersand lt; apersandgt or "&lt;Eq&gt".  Why?

Friday, 9 May 2008 08:41 by

Monday, 30 Jun 2008 06:58 by Linda
Hi I really liked the post. I have a problem similar to Rodney - I get a query error when replicating your query in my webpart. I haven't looked at the imported information. Was it something simple? Thanks, Linda

Friday, 26 Sep 2008 01:36 by Frank
Possibly it's more easy to use centrally managed cross-site tree-style categories to tagg the content and then meta-data based cross-type (!)result lists as shown here: http://www.sharepartxxl.com/categories/company/Lists/CompanyItems/SPXXLBy%20Content%20Type.aspx (Click on some category in tree) Moreover you have related items link lists directly in an items detail view with this solution. Check it out, Frank.

Monday, 27 Oct 2008 08:09 by Tasneem
There is syntax error in the above code

Monday, 27 Oct 2008 08:14 by Tasneem
There is syntax error in the above code Where> should be

Thursday, 11 Dec 2008 11:12 by Simon
I am trying to aggregate two Base List types together to display Announcements AND Documents - is this possible?

Friday, 27 Feb 2009 04:29 by Sumant
Hello Everyone I was facing the same problems as Rodney, The CAML got messed up when I export the webpart. I did some quick Google and found this link http://msdn.microsoft.com/en-us/library/aa981241.aspx Gist of it is that there should not be any space between the CAML query. It should be a long continuous statement without spaces. Once I did that it worked like a charm. The Article has a great collection of information on CQWP. Thank you.

Tuesday, 26 May 2009 05:28 by Liz
My question is around the styling. I added the custom style but cannot see it to select when configuring webpart. Also, my CAML works with one EQ but not OR EQ. I'll try removing white space as suggested.

Thursday, 17 Sep 2009 10:07 by Lars
Great post, thanks. This works perfecly, but is there a way to get items from other basetypes into the same CQWP. Say for example i want Announcements aswell as the documents and news items?

Name:

URL:

Email:

Comments: