Thanks!

Content Query Web Part – image on left with formatted date and description

There are tons of blog posts about using the Content Query Web Part to show for example news from an Announcement list. I myself learned a lot from this Heather Solomon post a couple of years ago.

In this post I am going to show you step by step how to create an Item Style for your CQWP that displays an announcements image, title, date and the first 250 characters of the description. This works for Sharepoint Server 2010, and unlike 2007 you don’t have to export the CQWP and change the code – no notepad this time!

 

1. For best practise, I created my own Content Type inheriting from the Announcement Content Type. Lets call this CompanyAnnoucement. To CompanyAnnoucement I added an existing site colum: Rollup Image. Add CompanyAnnoucement to the list(s) you want. If lots of people needs direction on this step, I’ll make a blog post about it.

 

2. Open your top-level site in Sharepoint Designer, click “All Files – Style Library – XSL Style Sheets – ItemStyle.xsl” and click Edit (Also, make a copy here before you make any changes, if you break this file, e.g. have a typo, none of your CQWP’s will work and display this error message:

Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator.

 

3. In the top of the file, add this line so we can format the date further down the road:

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

 

4. Copy the Default Template (This is the “Image on Left” style).  Change the first line to

<xsl:template name="CompanyAnnouncement" match="Row[@Style='CompanyAnnouncement']" mode="itemstyle">

 

5.  Add this at the bottom of the variables section:

<xsl:variable name="Created"> <xsl:value-of select="ddwrt:FormatDateTime(string(@Created) ,1033 ,'dd.MM.yyyy')" /></xsl:variable>

 

6. Change the title-part (the xsl:value-of select=”$DisplayTitle”-section to:

<strong><xsl:value-of select="$DisplayTitle"/> - <xsl:value-of select="$Created"/></strong></a>
<div> <xsl:value-of select="substring(@Description,1,250)" disable-output-escaping="yes" /><xsl:if test="string-length(@Description) &gt; 250">...</xsl:if></div>

This displays the created-date in your previously specified format, and the first 250 characters of the description, and ... if there are more text in the description than being displayed.
Save and remember to publish ItemStyle.xsl!

 

7. Add a CQWP where you want it. In the CQWP settings, set the Source under Query as you like.

Under Presentation – Styles – Item style, choose CompanyAnnouncement that you should see here now.

Under Fields to display, set Image to “Rollup Image;”, Title to “Title” (both should already be set), and Description to “Body;”.

Click OK and you’re set to go:

 



And yes – you are free to style this more beautiful than I can:)

 

 

If you don’t want to enter Sharepoint Designer and do this without code (and without being able to style the output yourself and including date, you can take a look at this blog post.

 

Update: complete code below

Here is the code in part 3), starting from the absolute top of ItemStyle.xsl:

<xsl:stylesheet   version=”1.0″
exclude-result-prefixes=”x d xsl msxsl cmswrt”
xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime”
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”>

 

And here is the code in part 4)-6), starting just below the closing tag of the Default template. In my ItemStyle this is line 82.

<xsl:template name=”CompanyAnnouncement” match=”Row[@Style='CompanyAnnouncement']” mode=”itemstyle”>
<xsl:variable name=”SafeLinkUrl”>
<xsl:call-template name=”OuterTemplate.GetSafeLink”>
<xsl:with-param name=”UrlColumnName” select=”‘LinkUrl’”/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name=”SafeImageUrl”>
<xsl:call-template name=”OuterTemplate.GetSafeStaticUrl”>
<xsl:with-param name=”UrlColumnName” select=”‘ImageUrl’”/>
</xsl:call-template>        </xsl:variable>        <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:call-template>
</xsl:variable>
<xsl:variable name=”Created”>
<xsl:value-of select=”ddwrt:FormatDateTime(string(@Created) ,1044 ,’dd.MM.yyyy’)” />
</xsl:variable>
<div>
<xsl:if test=”string-length($SafeImageUrl) != 0″>
<div>
<a href=”{$SafeLinkUrl}” onclick=”return OpenDialog(‘{$SafeLinkUrl}’);”>
<xsl:if test=”$ItemsHaveStreams = ‘True’”>
<xsl:attribute name=”onclick”>
<xsl:value-of select=”@OnClickForWebRendering”/>
</xsl:attribute>
</xsl:if>
<xsl:if test=”$ItemsHaveStreams != ‘True’ and @OpenInNewWindow = ‘True’”>
<xsl:attribute name=”onclick”>
<xsl:value-of disable-output-escaping=”yes” select=”$OnClickTargetAttribute”/>
</xsl:attribute>
</xsl:if>
<img src=”{$SafeImageUrl}” title=”{@ImageUrlAltText}”>
<xsl:if test=”$ImageWidth != ””>
<xsl:attribute name=”width”>
<xsl:value-of select=”$ImageWidth” />
</xsl:attribute>
</xsl:if>
<xsl:if test=”$ImageHeight != ””>
<xsl:attribute name=”height”>
<xsl:value-of select=”$ImageHeight” />
</xsl:attribute>
</xsl:if>
</img>
</a>
</div>
</xsl:if>
<div>
<xsl:call-template name=”OuterTemplate.CallPresenceStatusIconTemplate”/>
<a href=”{$SafeLinkUrl}” title=”{@LinkToolTip}” onclick=”return OpenDialog(‘{$SafeLinkUrl}’);”>
<xsl:if test=”$ItemsHaveStreams = ‘True’”>
<xsl:attribute name=”onclick”>
<xsl:value-of select=”@OnClickForWebRendering”/>
</xsl:attribute>
</xsl:if>
<xsl:if test=”$ItemsHaveStreams != ‘True’ and @OpenInNewWindow = ‘True’”>
<xsl:attribute name=”onclick”>
<xsl:value-of disable-output-escaping=”yes” select=”$OnClickTargetAttribute”/>
</xsl:attribute>
</xsl:if>
<strong>
<xsl:value-of select=”$DisplayTitle”/> –  <xsl:value-of select=”$Created”/></strong> </a>
<div>
<xsl:value-of select=”substring(@Description,1,250)” disable-output-escaping=”yes” />
<xsl:if test=”string-length(@Description) &gt; 250″>…</xsl:if>
</div>
</div>
</div>
</xsl:template>

In this complete version I have also added functionality to open the announcement in a modal dialogue, and I am using Nowegian Date formatting (1044) instead of English (1033).

 

 

5 comments to Content Query Web Part – image on left with formatted date and description

  • Jamie W

    Hey there!

    Thanks a million for posting. I’ve visited the same blogs mentioned above and started developing a good repository of content queries depending on client need.

    I’ve tried copy /pasting based on your notes but haven’t had the best of luck. Would you mind posting the ‘companyannouncement’ itemstyle in long form. I’m not sure if I’ve simply confused one or two areas while dragging and dropping!

    Loooking forward to seeing the end result!

    Jamie

  • I have added the “complete” code in the post now.

  • Максим

    Thanks a million, Audun!!
    It is exactly what I was searching for. It works!

  • bijesh

    i get an arror as soon as i do the first step and publish it. I tried copying the entire code but still does not work…can u help me in this matter

  • Nick

    This is great. Is there a way to have the image be the user profile image?

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>