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