Saturday, April 19, 2008

Content Query Web Part Customization 101

Many of us have heard that we can create a content query web part that can display more than one column. To achieve this you have to familiar yourself with following files.

  • ContentQueryMain.xsl
  • ItemStyle.xsl
  • the .webpart file you get by exporting the content query web part

For the two .xsl files, my recommendation would be "create a copy of your own". do not change these two files directly. Because all content query web parts that currently exist in the system all references these two files. If something is wrong with these two files, all content query web parts on your server will all  go wrong. You do not want this happen.

In Short, all you need to do can be summarized into following steps:

1. Identify the actual column name and type of the column for the column you would like to display.

2. Let your webpart know what to display by changing the .webpartfile

3. Define how to display content by defining the xslt in the ContentQueryMain.xsl and ItemStyle.xsl

Let's dive into the details.

1. Identify the actual column name and type of the column for the column you would like to display.

Personally, I think this is the most important step. Without knowing the correct name, you can not go to anywhere. Heather has done a good job on how to do this. For me, the method she mentioned in the [The Other Internal Column Names & Finding Field Types] section works.

The most important thing is to find the right file - SCHEMA.XML.

You can find it at following location:

[SharePoint Installation Drive]:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\[Feature Name]

where Feature Name is the name of the list type you are querying.

In the file, browse the <FIELD> property, GUESS which one is the column you need.

Yes, you have to guess. Most of the time, when you see the Name attribute in the FIELD property, you know this is the one.

2. Let your webpart know what to display by changing the .webpartfile

How do we achieve this?

remember the .webpart file we were talking about earlier?

locate the CommonViewFields property in the file, and modify it based on the syntax below:

<property name="CommonViewFields" type="string">InternalColumnNameGoesHere, FieldType</property>

Fill in the column name and column type in the SCHEMA.XML you found in step 1.

Normally we use notepad to do this. But I would like to share a great tool with you.

The web part developed by MSIT: CQWP Dynamic QueryOverride.

This is a replacement for Content Query Web Part.

This is a codeplex project. Use it at your own risk.

Not only the CommonViewFields, but also lots of other things you can manipulate with this web part.

3.Define how to display content by defining the XSLT in the ContentQueryMain.xsl and ItemStyle.xsl

Starting from here, it all depends on how good you are with XSLT.

And before you begin to change anything, change the reference to the ContentQueryMain.xsl and ItemStyle.xsl in the .webpart file to your own copy. Do not know how to make your own copy. You can follow Kyle's instruction at here. Again the CQWP Dynamic QueryOverride also allow you to change the reference in "Modify this web part".

Following is the how Microsoft describe these two files:

  • ContentQueryMain.xsl: You can use the Content Query Web Part to aggregate content from multiple data sources across your Web site, and then present it all in one place.
  • ItemStyle.xsl: Contains templates that define how to display an item. These templates receive and process one row of data at a time, ensuring that the style and data in the item rows is consistent.

The Paul Gavin's article about Display Content Query Web Part Results in a Grid / Table helps me a lot on understanding what should I do with these two files

Now you should know when I think find the column name is the most important thing when you customize the CQWP.

Because the CQWP Dynamic QueryOverride web part can assist you to complete the task. This does not include XSLT part. That is another topic.

[Useful Tools]

Web Part: CQWP Dynamic QueryOverride by MSIT

[Useful Links]

Customizing the Content Query Web Part and Custom Item Styles by Heather soloman
Content query web part: customisation and adding custom fields by Kartic
Display Content Query Web Part Results in a Grid / Table by Paul Gavin
Using a Custom ContentQueryMain.xsl by Kyle

How to: Customize XSL for the Content Query Web Part from MSDN

How to: Customize Styles from MSDN