November 20th, 2009Customizing the Content Query Web Part
I found myself in a situation, the other day, where I needed to display items from a Links List on one of my subsites.
The trouble was that the Links List resided on the parent site. Well, that should be easy right? Wrong.
I started by using the Content Query Web Part (CQWP). I selected to “Show items from the following list” and selected the Links list on the parent site.
The list resolved, however, this is what it looked like:

- Hmmmm...That dont look right
It turns out that the CQWP doesn’t know what to do with the URL and Title fields on the Links List.
Also, when you click on the “(Blank)” link, it takes you to the item view of the link and not the page you’re linking to.
So I started Googling and found Ishai Sagi’s clever little tip.
Basically, he exports the web part and makes some modifications. Essentially, creating a custom web part.
1. I would start by configuring any settings you will want to carry over to your custom web part (Query, Appearance, Title, Sorting, Audience Targeting, etc.) This will save you from having to make those adjustments after importing the web part to your pages.
2. Click the “edit” button on the web part and select “Export…”

- Select "Export..." from the edit menu on the web part
- Rename “NoImage” to “LinkList”.
- Change the “href” attribute to point to the URL column, the text before the column.
- Change the link text to point to the URL column, the text after the column.
3. Save the .webpart file to your hard disk. Then open it using your favorite XML editor.
4. You’re going to be looking for a property “CommonViewFields”:
Change it to this:
5. Now we need to add a custom “Item Style”, so the web part will understand what to do with the new “CommonViewFields” property. This will require editing the item style XSL of the site.
6. Open up SharePoint Designer and navigate to your site or parent site.
7. Check out and open Style Library > XSL Style Sheets > ItemStyle.xsl
8. We are now looking for the template for the style “NoImage”:
9. Make a copy of the complete template:
from
to
and paste it under the “NoImage” template.
10. Here’s the complete code:
<xsl:variable name="DisplayTitle">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="@URL"/>
<xsl:with-param name="UrlColumnName" select="’URL’"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="LinkTarget">
<xsl:if test="@OpenInNewWindow = ‘True’" >_blank</xsl:if>
</xsl:variable>
<div id="linkitem" class="item" >
<div class="bullet link-item">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<a>
<xsl:attribute name="href">
<xsl:value-of select="substring-before($DisplayTitle,’, ‘)">
</xsl:value-of>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@Description">
</xsl:value-of>
</xsl:attribute>
<xsl:value-of select="substring-after($DisplayTitle,’, ‘)">
</xsl:value-of>
</a>
</div>
</div>
</xsl:template>
If you make any mistakes with this part of the styling, it will affect ANY other CQWP you might have deployed on other pages. Might be a good idea to check on those right now.
11. If everything looks gravy, save the changes to the file and go to the Site Settings of your site or parent site.
12. Under “Galleries”, select “Web Parts”
13. Upload the .webpart file. Some other settings will come up. Give it a file name, Title, Description. I also like to specify a “Custom” Group value. That way the web part is easy to find in the “Add Web Parts” dialog.
14. Go to your site page or subsite page and Add Web Part. Add the custom web part to the page.
15. Modify the web part and under Presentation > Styles > Item Style, select “LinkList” from the dropdown menu:
The links should be showing correctly. Plus, when you click on them, they go directly to the page you are linking to.
Nice work Ishai!
November 22nd, 2009 at 4:23 pm
[...] Customizing the Content Query Web Part [...]