UPDATED (12/07/2007): Issue arises in last block of code when calling $bodyContent, should be $formattedBodyContent. Also fixed the case issue as noted by Matthew. 🙂

Well I am currently working on a project that has a great need to use the Content Query Web Part for exposing different content onto a central page. The solution will be made up of a top level publishing portal with various different types of private and public sites and site collections that will exist underneath. In this post I wanted to take you through a step-by-step look at exposing the content into a CQWP and customising it slightly to display the correct fields. There are various people who have already posted about elements of this such as:

Steven Van de Craen


To begin with we will need to look at creating some custom content types that will be used within the portal solution. Just for information the basic structure of the sites will be as below:

Now that we have the basic structure let’s look at the various content that we need. Firstly we will create a content type called “Demo News” and create various columns to it. This will then be added to a list that we will create on the department public site.

To create the content type, open up the portal and select the following:

Once the page loads you need to select the “Site Content Types” from the “Galleries” section.

Press the “Create” button and complete as below:

Now we need to add some site columns to the content type, this is done by pressing the “Add from new site column” link shown below.

In this example we will add two extra columns as per the table below:

Column Name

Column Type

Column Required

Column Values


Date & Time



News Category







When the above have been created the list should then look as below:

No we need to access department public site and create a new list based on the announcement template. Once this has been created we need to add the new custom list content type to it. To create the list open up the public site and select the “View All Content” link from the “Site Actions” menu. The select the “Create” button and select the link shown below.

The list has been configured as below.

Once this has been created we then need to firstly allow management of content types and then associate the “Demo News” content type with this list. This is done by accessing the properties of the list and selecting the “Advanced Settings” option. Then we can enable the following option.

Now we can select the content type from the list of existing ones as below.

When we look at the content types that are associated with the list now, we should see the standard one plus the “Demo News” one.

So now we have the plumbing let’s add some sample news, be sure to select the menu next to the “New” button and select “Demo News” as the content type.

I have added the following items and ensured that I selected a different new category as we will use these later.

So now we have some news items and the main structure we need to add a Content Query Web Part to the main parent portal and then attach this to the correct data.

To do this open up the main portal page and select the “Edit Page” option from the “Site Actions” menu.

Then when the page has loaded navigate to the web part zone you wish to use and select the “Add a Web Part” link.

From the popup windows select the “Content Query Web Part” from the “All Web Parts” section.

Once this is done it will be loaded onto the main page and automatically populated with some content from the site. In order to display our custom data we need to modify the configuration for the web part. To do this press the “Edit” button on the web part and then the “Modify Shared Web Part” menu item.

The configuration for the web part should then load. The configuration that needs modifying is contained within the “Query” section. The default configuration is shown below.

We will now change the configuration to the following:

After you apply the above, the content query web part should then display the following:

Even though is a good view it does not show the rest of detail, also I would like the content to be grouped by the “News Category”. To group the content modify the web part and select the presentation section. Within the Group dropdown menus you will see the “New Category” field.

Select the “New Category” and apply. The web part should then render as below.

To make it look a little more exciting than the plain standard style we can use some of the built in styles. To change the styles modify the web part and select the “Presentation” section again but this time scroll down to the styles section.

As a test to show you what the base ones look like simply select the “Title with Background” option and apply this to the web part. This should then be displayed as below.

As you can see they are quite basic and probably not what you would want to use. So let’s look at changing it slightly but using XSL to display extra fields and change the layout. Firstly you need export the web part to our machine. To do this, select the export menu item from the edit menu of the web part.

You will then be prompted to save the file as below.

Now open up the file in any text editor, I will use Visual Studio but notepad will work just as well. Now in the file there is a property called “CommonViewFields”. This needs to be changes as below.


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


<property name=”CommonViewFields” type=”string”>Body, Text</< span>property>

Now we need to upload the newly edited web part into the page and remove the current one. This is done by editing the page again clicking the “X” icon to close the current one then clicking the “Add a web part” button again. This time instead of picking a web part from the windows press the “Advanced Web Part gallery and options” link as below.

This will now load the gallery of web parts into the right hand side of the window. At the top of the new window click the arrow on the right side of the browse button and select the “Import” menu item.

Now navigate to your edited web part and press ok to import. This should then appear in the list below and can be dragged onto the page just like any other web part.

The web part will now load as before but still not show the extra field we added in. To add this field in we need to make some changes to the XSLT for the content query web part. All of the styles that we looked at earlier for the content query web part are stored within two XSL files. These files are stored in a style library stored within the site. The easiest way to make the changes is to simply take a copy of the “ItemStyle.xsl” and open it in Visual Studio. Once this is opened you can change the relevant template section as below. For this example we will use the “Image on Left” style. Within the template is a section as below:

This needs to be changed as below, so it will now show the “@Body” field instead of the description one listed by default. The new code is listed as below:

If you now save the file and upload this file into the “Style Library” and select the overwrite with a version checkbox. Once this is done, simply refresh the main page and the web part should now list as below:

Notice that the CQWP is showing the HTML code, obviously we do not want this for our live solution. To fix this we can create a function within the XSLT to fix this. A great example of all of the work I am doing here and a specific function can be found here:

The function that we will use is as below.

This needs to be added to the “ItemStyle.xsl” file. The code block is shown below.


exclude-result-prefixes=”x d xsl msxsl cmswrt”




xmlns:xsl=”” xmlns:msxsl=”urn:schemas-microsoft-com:xslt”>

The display code we changed earlier now needs to be changed as below.

No we have done this and saved the file back, when we refresh the page the web part should now display as below.

Now what we would also like to see is a [more] button instead of showing the full details as above. This can be added by simply using the “substring” function to split the body up and then add some extra fields to the XSLT to turn the [more] into a hyperlink. The code would look as below.



The CQWP should then display as below.

So as you can see with a little modification you can create highly customized CQWP to display your content. Just want to say, a great big thanks to George Perantatos who posted some of the content I have used here. J

Hopefully you will find this useful, I had great fun in working out all the steps that needed to be taken to achieve this.

Category: SharePoint

Published: 15/01/2007 20:21

BlogTitleForUrl: moss2007-–-content-query-web-part-step-by-step

Categories: Development;SharePoint;Office System 2007

MOSS2007 – Content Query Web Part step by step
Liam Cleary
Tue, 03 Jul 2007 18:58:35 GMT