Content Query Web Part

Links:

Snippets:

Identify column names for reference in ItemStyle.xsl

Add the following template to the ItemStyle.xsl. Add corresponding CQWP to page, select template and present 1 item. Will list actual column names (as opposed to display names)

<xsl:template name=”Sys-Columns” match=”Row[@Style='Sys-Columns']” mode=”itemstyle”>
<xsl:for-each select=”@*”>
P:<xsl:value-of select=”name()” /><br />
</xsl:for-each>
</xsl:template>

Source: Heather Solomon

How to supply fixed sizes for CQWP images

<xsl:if test=”$ImageWidth != ” and $ImageHeight != ””>
<img class=”image” src=”{$SafeImageUrl}” title=”{@ImageUrlAltText}” style=”width:50px;height=50px”/>
</xsl:attribute>
</xsl:if>

Source: MSDN Social, Sep 2011

Displaying different columns in the web part

You can choose columns for the title, description and image (leave the link well alone).  Simply add the column title to the appropriate box in the web part properties.  If you add more than one column to any of the boxes, the first will be displayed by default. If the first column is empty for the item being displayed, the second column will be used instead, and so on.

Displaying rich text instead of plain

By default, the content query web part displays plain text in the Description field, regardless of the content contained in those fields.  If you want to display rich text/HTML instead of plain text, you need to edit the ItemStyle.xsl file:

  • Open the site/site collection root in SharePoint Designer
  • Navigate to: All Files – Style Library – XSL Style Sheets
  • Edit the ItemStyle.xsl  (Note: save a copy or make sure versioning is on in case you make a mistake)
  • Locate the template you want to modify (e.g. Image on the right)
  • Update the Description tag:
    • From: <xsl:value-of select=”@Description” />
    • To:      <xsl:value-of select=”@Description” disable-output-escaping=”yes” />
  • Save and publish the file

Source: http://www.sharepoint-tips.com/2007/01/showing-description-of-page-in-content.html

Create your own ItemStyle.XSL files and reference in your own CQWPs

  • Within Site Collection root (note: if not root SC in Web App, add managed path to URls)
  • Copy and paste/rename the itemstyle.xsl file
  • Add a standard CQWP to a site page and then export it.
  • Open the exported CQWP in Notepad (or a proper XML editor)
  • Edit the following property (replace ‘MyItemStyle.xsl’ with the name of your file)
    <property name=”ItemXslLink” type=”string”>/Style Library/XSL Style Sheets/MyItemStyle.xsl</property>
  • Edit the following property to add your own columns for referencing within templates (e.g. is column  name DocType of type Text)
    <property name=”CommonViewFields” type=”string”>DocType, Text</property>
  • Import the edited CQWP back into your site collection
  • You can now edit your own ItemStyle.xsl to create your own formatting.
  • Rinse and repeat if you want to use multiple different CQWP formats.

Data types for columns: Text, Note, DateTime, Number, Integer, Boolean, Currency, URL, Choice, Lookup, Counter, Threading, Image, RichHTML (Source: Microsoft and  Heather Solomon)

Formatting custom CQWPs (e.g. grid layout) with data formats instead of default date time

Following is a copy from source: Custom Date Formats in SharePoint (storing here in case the original ever disappears, was hard to track down):

All you need to do to make sure the “FormatDate” function is available in your custom XSL files is to make sure you reference the ddwrt namespace.

xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime

Once this has been added to the rest of your namespace declarations at the top of your <xsl:stylesheet> tag, you should be able to use the “FormatDate” function anywhere you like.  Here is sample of what a full XSL file would look like that does this.

<xsl:stylesheet
  version="1.0"
  exclude-result-prefixes="x d xsl msxsl cmswrt"
  xmlns:x="http://www.w3.org/2001/XMLSchema"
  xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
  xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
    <xsl:template name="Default" match="*" mode="itemstyle">
	<xsl:value-of select="ddwrt:FormatDate(@ArticleStartDate, 2057, 3)"/>
    </xsl:template>
</xsl:stylesheet>

Here are the details on the different formats you can get by changing the parameters.  March 23 12:00 AM was used as input for all outputs.

Output Locale Format
3/23/2009 1033 1
3/23/2009 12:00 AM 1033 2
Monday, March 23 2009 1033 3
12:00 AM 1033 4
Monday, March 23, 2009 12:00 AM 1033 7
3/23/2009 12:00:00 AM 1033 13
Monday, March 23, 2009 12:00:00 AM 1033 15
23/03/2009 2057 1
3/23/2009 12:00 AM 2057 2
23 March 2009 2057 3
00:00 2057 4
23/03/2009 00:00 2057 5
23 March 2009 00:00 2057 7
00:00:00 2057 12
23/03/2009 00:00:00 2057 13
23 March 2009 00:00:00 2057 15

You can also get a list of all the available locale’s here.

© 2006 - 2012 Joining Dots Ltd. All rights reserved.

Content is for information purposes only with no warranties or guarantees regarding accuracy = use at your own risk. Ditto for links to other sites.
All product names, logos, brands and other trademarks referred to within this site are the property of their respective trademark holders.