DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Customize ItemStyle.xsl for ContentQuery Webpart

Posted by PANVEGA on June 3, 2008

If you want to preserve the look and feel of a Web page by applying certain custom styles to the page content by including a WepPart, you can change the definition of style sheets “ItemStyles.xsl” without affecting the page content in the future.You find the file in under your domain/Style Library/XSL Style Sheets. Open the file in an editor of choice (eg. Microsoft Office SharePoint Designer). Next check it out to be able to make changes to the file.

Using the Content Query Web Part you can display a dynamic view of content from your site on a web page. The web part can be configured to retrieve items from a single list, a single site and subsites or the entire site collection. It builds a CAML (Collaborative Application Markup Language) query and uses the new Microsoft.SharePoint.SPSiteDataQuery class to query the server. Filtering, sorting, ordering and grouping can all be configured in the Web Part properties.

The approach to use if you are using a different style than the out of the box style is to deploy your own .webpart of the Content Query Web Part that references a different style sheet than the ItemStyle.xsl file. A Content Query Web Part displays a dynamic view of content on a page in your Microsoft Office SharePoint Server 2007 site. The Web Part runs a query that can span lists and sites in your site collection, and then presents the results of that query on the page. You can, for example, use this Web Part to display the five most recently published articles in your Web site, regardless of where they are stored.

Now edit the the template like you normaly do XSLT formating. I also included for example a JS funktion in the Style for custom Window Popup configuration.

Here is an example:

<xsl:template name=”Name” match=”Row[@Style=’Name’]” mode=”itemstyle”>
<xsl:variable name=”SafeLinkUrl”>
<xsl:call-template name=”OuterTemplate.GetSafeLink”>
<xsl:with-param name=”UrlColumnName” select=”‘LinkUrl'”/>
<xsl:variable name=”DisplayTitle”>
<xsl:call-template name=”OuterTemplate.GetTitle”>
<xsl:with-param name=”Title” select=”@Title”/>
<xsl:with-param name=”UrlColumnName” select=”‘LinkUrl'”/>
<xsl:variable name=”LinkTarget”>
<xsl:if test=”@OpenInNewWindow = ‘True'” >_blank</xsl:if>
<td id=”linkitem” class=”item link-item”>
<a href=”” target=”_blank” onclick=”javascript:customPopup(‘{$SafeLinkUrl}’);return false;”>
<xsl:value-of select=”$DisplayTitle”/> –
<xsl:value-of select=”@Name_x005F_x0020_Year” />
(<xsl:value-of select=”@Languages” />, <xsl:value-of select=”@Document_x005F_x0020_Type” />)
<tr height=”8px”>
<script type=”text/javascript”>
function customPopup(url)
Report =,’Report’,’status=1,width=750,height=650,resizable=no,scrollbars=no’);


In the XSL file you’ll find xsl:template elements for each selectable style in the Content Query Web Part style drop down. As a best practice I won’t modify any of the default styles, but rather adding my own style.

The easy approach is to copy the xsl:template element of the style that matches your style the most,and make alterations to it.

– Copy the <xsl:template name=”Default” match=”*” mode=”itemstyle”>element
– Change the name attribute to “DetailedStyle”
– Change the match attribute to “Row[@Style=’DetailedStyle’]”

Here you can select your template you created before. You have to add a simple CQWP to your page and select your site or list, type and aproriate Content Type. In the next step export the Webpart to your local maschine an edit the file.

By default, the XSL style sheet that controls the presentation of the Content Query Web Part renders the Title, Description, ImageUrl, and LinkUrl columns (if present) of the items the query returns. The Web Part renders the ImageUrl and LinkUrl columns as an image and as a hyperlink, respectively. The Content Query Web Part uses XSL style sheets to render these columns by default. If you want to query for custom content types and render specific columns from those content types in the Web Part, you need to perform some minor customization in the .webpart file.

You only have to do change the webaprt file, when you want to present your own columns in the style in adition to default values. Open the .webpart go to line <property name=”CommonViewFields” type=”string”>. Follow the instruction from this post CommonViewFiled Types.

E.g. <property name=”CommonViewFields” type=”string”>Previews, PublishingRollupImage;FileSizeDisplay,Computed;File_x0020_Size,Lookup;Product, Choice;ProductGroup, Choice;DownloadIT, String;</property>

Important Note!

You cannot use your columns’ display names in this property. You can find the internal column names by checking the properties of the column in the list or document library. Right-click the column, and then click Properties. The internal name of a column appears in the Address (URL) property after 'Field='. In the Knowledge Base Articles scenario, the columns are Knowledge Base Article and Product and their internal names are KB_x0020_Title and Product, respectively.

In the next step, import your new webpart file to the CQWP. Choose your custom ItemStyle in the list and thats all.



8 Responses to “Customize ItemStyle.xsl for ContentQuery Webpart”

  1. on3 said

    Hi There,

    i’ve just override (OnLoad() method) the OOTB content query web part.
    The functionality is fine until i found out that i cannot apply any ootb style sheets from “Presentation” section.
    If i go to “Modify Shared Web Part – Presentation – Styles – Item Style”
    then i choose “Bulleted title”, the style dont apply to my customized CQWB.

    Do you know what am i missing?


  2. PANVEGA said

    have you override the xsl:template name=”Bullets” match=”Row[@Style=’Bullets] ?
    It is better to copy the original template and change the names.
    E.g. xsl:template name=”CustomBullets” match=”Row[@Style=’CustomBullets’]

    Have you already checked the CommonViewFields when exporting the WP?
    If you want to apply to custom fields you must add your fields and types to the tag. Finally import the custom WP in the gallery.

    I hope that helps!

  3. […] Customize ItemStyle.xsl for ContentQuery Webpart […]

  4. […] […]

  5. freddy said

    i wanna know if we can extend the itemstyle.xsl so we can have more than one itemstyle file and all the templates inside them will appear to us in the dropdown
    fast reply is appreciated

    • PANVEGA said

      Hi Freddy, I think you can have more than one ItemStyles.xsl. However you can apply just one Style to a Webpart dropdown template.
      You have to deploy the new Style like: Snippet from the element.xml

      Module Name=”WebPartDefaultStyles” Url=”Style Library/XSL Style Sheets” RootWebOnly=”TRUE”
      File Url=”/MyItemStyle.xsl” Type=”GhostableInLibrary”

      and add the properties to the .webpart file

      /Style Library/XSL Style Sheets/MyItemStyle.xsl
      I have not try it yet, but this must the way how it shoul work.

      Good luck

  6. […] Customise ItemStyle.xsl for content query web part Share and Enjoy: […]

  7. megan said

    this may be a dumb question but how do I stop the template from repeating everything.
    for example I want my result to be:


    I obviously dont want to repeat the UL tags? how do I stop that?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: