labs.steveottenad.com

Sharepoint 2010 XSLT Date Formatting

The main goal of this article is to outline the snags I hit while trying to configure a SharePoint 2010 Content Query Web Part (CQWP) to display a feed of my recent calendar events. I was presented a very specific layout to use for displaying the latest results from a users calendar, and I knew I would have to jump in and write some custom XSLT for the CQWP to comsume.

Creating a New Style

Hopefully you have access to your site in SharePoint Designer 2010. If so, open your site and navigate to the “/Style Library/XSL Style Sheets” folder, and open “ItemStyle.xsl”. From here, you’ll want to do a few things right off the bat. First, create a new <xsl:template> that your CQWP can comsume. Do this by pasting this code into the bottom of the file BEFORE the <xsl:template name=”HiddenSlots”… line

<xsl:template name="EventDisplay" match="Row[@Style='EventDisplay']" mode="itemstyle">
	<xsl:variable name="dateTime" select="ddwrt:FormatDate(string(@EventDate), 1033, 3)" />
	<xsl:variable name="dateTimeCondensed" select="ddwrt:FormatDate(string(@EventDate), 1033, 2)" />
	<xsl:variable name="date"  select="substring-before(substring-after($dateTime, ', '), ', ')" />
	<xsl:variable name="month" select="substring-before($date, ' ')" />
	<xsl:variable name="day"   select="substring-after($date, ' ')" />
	<xsl:variable name="time"  select="substring-after($dateTimeCondensed, ' ')" />
	<xsl:variable name="SafeLinkUrl">
		<xsl:call-template name="OuterTemplate.GetSafeLink">
		     <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
		</xsl:call-template>
	</xsl:variable>
	<div class="item link-item">
		<div class="description xslCal">
			<div class="left">
				<span class="month"><xsl:value-of select="$month" /></span>
				<span class="day"><xsl:value-of select="$day" /></span>
			</div><!--/Left-->
 
			<div class="right">
				<span class="title"><xsl:value-of select="@Title" /></span>
		        <span class="location">Location: <xsl:value-of select="@Location" /></span>
				<span class="time">Time: <xsl:value-of select="$time" /></span>
		   </div><!--/Right-->
		</div><!--/description-->
	</div><!--/item-->
</xsl:template>

EDIT

You’ll need to double check that you have

xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime”
in the top of your xsl file

This will create a new style in your Content Query Web Part’s “Item Style” dropdown.

Add Some CSS

To get this new XSL style to look decent, we’ll add some CSS to make it stand out a bit more. If you already have a custom stylesheet attached to your masterpage, open that up. If not, visit This page for more information on how to do so.

So, with your CSS open, paste in these styles

/*Calendar XSLT Styles*/
.xslCal .left{width:82px; position:relative; left:-21px; float:left;}
.xslCal .right span{display:block;}
.xslCal .month{text-transform:uppercase; background:#71cdf4; color:#fff; padding:3px 0 3px 18px; width:68px; display:block; }
.xslCal .day{font-size:61px; width:86px; display:block; line-height:70px; text-align:right;}
.xslCal .title{color:#0775aa; display:block; padding-top:3px;}

Adding/Configuring a CQWP

Now that we have the proper XSLT and CSS in place, we need to create our data and a Content Query Web Part to show everything. Start by going to Site Actions -> View all site content. Then click the “Create” button in the upper left of the main pane. Select a “Calendar” list type, name it “Main Calendar” and click “Create”. Go into that list, and create a couple sample events on various dates.

Finally, navigate to the page where you want to display your calendar events, and click Site Actions->Edit Page. Then add a Content Query Web Part to the zone you want. Get into the toolpane for that web part by clicking the small black arrow in the title, and selecting “Edit Web Part”. This will open the toolpane on the right side of the screen. Fill out your query by clicking the “Show items from the following list” radio button, and using the “Browse” button, goto the “Main Calendar” list you created earlier. Under the “Content Type” heading, select “List Content Types” from the first dropdown, then “Event” from the second.

Open up the “Presentation” tab and make sure that the Group Style is “Default” and Item Style is “EventDisplay”. Then, to ensure that the correct data goes in the correct slots, setup the “Fields to Display” like shown. If you got it all done correctly, it should look roughly like this:

36 Comments

  1. Cool! Thank you. You can also add the CSS to an Content Editor WebPart.

  2. Benjamin Lloyd says:

    Wonderful post!

    I am also trying to display the description field. I can get it working but its not displaying as html, so i get the tags around everything.

    I have tried searching for a solution but nothing has worked so far…..can you help me?

    Cheers,
    Ben

  3. Mark Leighton says:

    Very neat. But when I open a drop down menu higher up the screen, the blue date block and the day number remain on top of the menu items and obscure them – any idea what I can do about that?

  4. Peter says:

    Did you notice that those styles and transformations are not applied when users are not logged in?

    • sottenad says:

      I’ve never noticed anything like that while using this. Make sure that the xsl and css are checked in and (importantly) have a major version present.

      • Peter says:

        Oh, you are right. I haven’t published it as major version. Thanks!

        Is there any fast and simple way to restrict displayed events (by filtering in CQWP)? I can do it somehow in XSLT but I would like to know if there is some other way to achieve that.

  5. Doug says:

    This is very nice. I do not see my locations coming through, though. Don’t know what I could have done…

  6. Doug says:

    Never mind – I just had to add the group:
    Location [Core Contact and Calendar Columns];

    Thank you for this post!!

  7. [...] is a good example what we can do with xslt. Like this:GillaBli först att gilla denna [...]

  8. Vinod kumar says:

    Thanks for sharing, just what i wanted. :)

  9. Prakash says:

    Is there a way you can link the title to its item?
    Please help me.

    • sottenad says:

      You should be able to change the

       <span class="title"><xsl:value-of select="@Title" /></span> 

      line to something like
      <span class="title"><a href="<xsl:value-of select="$SafeLinkUrl" / rel="nofollow">"><xsl:value-of select="@Title" /></a></span>

      Just off the top of my head that might work.

    • Matt says:

      I’m so close on this, but I can’t get the link to format within the HREF. Outside of it, all works well. Thanks for any help!

      Works:

      <span><xsl:value-of select="$SafeLinkUrl" /></span>
      

      Does not work:

      <span class="title"><a href='$SafeLinkUrl' /><xsl:value-of select="@Title" /></a></span>
      

      • Matt says:

        Actually, I just got it!

        <span class="title"><a href="{$SafeLinkUrl}"><xsl:value-of select="@Title" /></a></span>
        

  10. Andrei says:

    Hi! Thank you for this post!

    Is there a way to filter data after Start Time? I wan’t to show items when Start Time is greater than Today Date. If I try to apply a filter at Content Query Tool Part Properties (Query), Start Time doesn’t exist in the drop down button.

    Thanks

  11. Candice says:

    Hi,

    Could you please repost the code need for the ItemStyle.xsl?

    Thanks,
    Candice

  12. Paul Cheeseman says:

    Hi there,

    I’ve added the XSL in but it’s killing all my existing CQWPs. The error just says there is a problem displaying the webpart. I’ve already got some custom entries in the itemstyle.xsl file for announcements and link lists.
    Do I need to do any work beforhand to get this to work ?
    This will run against the OOTB sharepoint caldendar lists won’t it ? I’m not a sharepoint dev. person, just a tinkerer.

    Thanks
    Paul

    • Cliff Bradshaw says:

      Hi Paul,

      This usually indicates that something is wrong with your XSLT code. Look for error indications in your editor and correct them. Otherwise post a snippet of your code.

      • Paul says:

        I’ve just copied the code above into the OOTB ItemStyle.xsl file on a fresh install of Sharepoint and existing CQWP with OTTB formatting break with the “Unable to Display this webpart” error. Do I need add any pre-reqs to the server? I’ve pasted in the CSS and XSLT from above. I don’t expect you to fix it for me, but if you can point out something stupid I’m doing that would be brilliant.
        Thanks
        Paul

      • Sean says:

        I get the same issue with just copy and pasting the code. I published it as a major version and made sure to past the code in before the HiddenSlots area. Rather odd.

  13. Paul says:

    Fixed it and I love the results. Thank you !!
    I was missing the following from the top of the XSLT file.
    xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime”

  14. Praveen says:

    Thanks a lot for such a great article…. you made my day.
    and thanks paul for adding the missing part.

  15. [...] Check out the site here. It’s a simple edit of the ItemStyle.xsl file and adding some CSS: http://labs.steveottenad.com/sharepoint-2010-xslt-date-formatting/ [...]

  16. [...] Sharepoint 2010 XSLT Date Formatting: 9,459 Views [...]

  17. Rahul Sharam says:

    This is fantastic.
    i just copy and paste the above code snippets.

  18. Paul says:

    Hi there,

    I saw this the other day, then when googling for a similar topic, I found your article had been more or less copied out here: http://www.tuyrcorp.com/blog/sharepoint-2010-content-query-web-part-cqwp-to-display-a-feed-of-my-recent-calendar-events/

    Just thought you might like to know.

  19. [...] the formatting of the Calendar listitems, Steve Ottenad’s post on Sharepoint 2010 XSLT Date Formatting is very handy. Steve’s itemstyle.xsl modifications give a neat layout and CSS for the items, for [...]

  20. cferrer says:

    this is cool!!, but now how can I paging if I have more than 4 items?

  21. Paul says:

    Yeah, it does not work. I copied your snippet, pasted exactly where you said, published the item style sheet and it wrecked all my other CQWP’s.

  22. cferrer says:

    this post is nice but with lacking of pagination.

    I could create pagination by extending the CQWP (http://blog.mastykarz.nl/imtech-content-query-web-part-v14-exslt/) also modifying ItemStyle.xsl

    now this CQWP can go from Page 1 to Page x.

    CF

Leave a Reply