One of the nicest webparts available in SharePoint 2007 is the Content Query Web Part (a.k.a. CQWP). This web part allows you to query data from any list on the same site collection, and display those list items. This works great if you want to show corporate news on a team’s site, or in my case, show blog entries on various sites throughout the organization.
The scenario: a senior leader has started a blog dedicated to the new project management system that’s being rolled out. Obviously people can subscribe via RSS to the blog, but we also want to display the most recent entry on a project team’s SharePoint site. To do so, we’re going to use the CQWP, and customize the item style, so that we’re displaying the content how we want it to appear.
The setup - create a blog site in SharePoint, and create a web part page (or use an existing one) that is not on the blog site, to host the CQWP.
First, the CQWP.
Edit your host page, and add a Content Query Web Part to one of the zones.

After it’s added, click ‘Modify Shared Web Part’ to open the tool pane.
Expand the Query section, and choose “Show items from the following list”, then click “Browse” and navigate to your blog site, and select the “Posts” list.

Scroll down and expand the “Presentation” section, and locate “Styles” section. Choose “Title and Description” from the “Item Style” pull down.

Click Apply, then OK to get back to your page.

Notice that the CQWP is only showing the title of the blog post, not the body.
Now obviously, we want to display the body of the post which is Rich HTML, and we want to show what day/time the post was submitted. To get that to work, we need to know the internal field name SharePoint uses for the Body field of a blog post, and the Published field of a blog post. Then we’ll edit the XSL Item Style sheet to include the new fields.
Getting the internal field names
So the two fields besides Title we want to display are Body and Published. To find the internal field name of those fields, do the following:
- Navigate to the Blog site
- Click ‘View All Site Content’ on the left hand navigation bar
- Select the ‘Posts’ list
- Click the ‘Settings’ button, then choose ‘List Settings’
- On the List Settings page, you’ll see a section called Columns
- Click on the column you want to add - 'Body'
- Now, look at the address bar, and find where it shows the field name
“&Field=Body”
- Repeat the steps above for 'Published', and you’ll notice that the internal column name for 'Published' is 'PublishedDate'
Now that we have the internal column names, we need to decide what type of fields they are. In the XSL sheet, we must choose one of these field types for each field.
- Text
- Note
- Number
- Currency
- Integer
- Boolean
- DateTime
- Threading
- Lookup
- Choice
- URL
- Counter
- DisplayOnly (DisplayOnly field type has no storage of its own)
- RichHTML
- Image
In this example, Body is going to be “RichHTML” and PublishedDate is going to be “DateTime”.
Editing the CQWP
Now that we have the field names and their types, we need to edit the CQWP to make sure the part is pulling in all the fields we need.
Go to your web part, and on the menu select ‘Export’. Pick a file name for your part, and choose a location to save it.
Edit the .webpart file using Notepad or some other text editor, and locate the line that reads:
Replace this line with the line below:
<property name="CommonViewFields" type="string">Body, RichHTML;PublishedDate, DateTime</property>
If you want the webpart to pull more fields, just keep adding them separated by a semi-colon.
Save the part, and go pack to your web part page.
Remove the CQWP that is there, and then import the .webpart file you just edited.
Click ‘Add Web Part’, then select “Advanced Web Part Gallery and Options”


After you’ve imported and added your part, you won’t see anything different. We’ve told the CQWP to pull in our extra fields, but we still need to edit the XSL stylesheet to display those fields.
Editing the XSL Stylesheet
Open SharePoint Designer, and open the root site of your site collection.
Choose the ‘Style Library’ document library from the root site, open the XSL Style Sheets folder, and edit ITEMSTYLE.XSL. This unghosts the page, but we can reset it if we want at any time.
Each
<xsl:template>
element is a “style” to display an item in the CQWP. Copy any of the styles, and paste it after the last closing </xsl:template>
element and before the closing <xsl:stylesheet>
tag.Edit the name and match attributes of your new template:
<xsl:template name=”MyCustomStyle” match=”Row[@Style=’MyCustomStyle’]” mode=”itemstyle”>
Now, in the last section of your newly created
<xsl:template>
, add a value-of xsl tag to display the fields you want appropriately.<xsl:value-of select=”@PublishedDate” />
<xsl:value-of select=“@Body” disable-output-escaping=”yes” />
NOTE: Notice the
disable-output-escaping=“yes”
attribute of the @Body
element. This tells the webpart to output HTML rather than raw text. - so if you put HTML into the body of your post, it will display as HTML.Save the file. Return to the site (in the browser) and refresh the page. Open the Web Part Tool Pane for the CQWP and expand Presentation, then under Styles change the Item Style to the new custom style in the drop down. Select Apply.
With closer inspection of the
ITEMSTYLE.xsl
, you’ll notice that each <xsl:template>
is wrapped in HTML - simply change that HTML to however you want to display your item, and you’re good to go.~~~~~ Credit ~~~~~
I couldn’t have figured this out with out having read these three posts... Big thanks to the authors.
http://www.heathersolomon.com/blog/articles/CustomItemStyle.aspx
http://blogs.msdn.com/ecm/archive/2006/10/25/configuring-and-customizing-the-content-query-web-part.aspx
http://www.sharepoint-tips.com/2007/01/showing-description-of-page-in-content.html
No comments:
Post a Comment