Thursday, 5 July 2012

SharePoint 2010 - RSS Viewer Web Part - Customise the style

For the last few days I've been looking for an example on how to customise SharePoint 2010's OOTB RSS Viewer Web Part, and finally I've found a solution that although it has quite a few steps, the final result is clean code with the ability to use CSS to style the Feed.

http://zioed.wordpress.com/2012/02/24/customizing-the-sharepoint-2010-rss-viewer-web-part-part-1-of-a-two-part-post-2/

Thank you Ed.

To add to Ed's fantastic post, I will show you a method to create styled RSS feeds without SharePoint Designer.  Once you have found an RSS feed that you would like to manipulate, add the URL to your RSS Viewer Web Part (If you cannot find it in your list of web parts, you need to enable the SharePoint Server Standard Site Collection feature), click Apply, then click the 'XSL Editor...' button.  Copy all of the code into Notepad for ease of editing, and paste it back into the XSL Editor when you are finished.

The section of XSL you are looking for is within: <xsl:template name="RSSMainTemplate.body"
and the area that we are modifying is between <xsl:if test="($CurPosition &lt;= $rss_FeedLimit)"> and </xsl:if>.  Everything within that If statement declares what information is shown, and what it looks like.

For my example, I modified MiningAustralia's RSS Feed (http://www.miningaustralia.com.au/rss.xml).  I deleted all the code inside the if statement mentioned above, and replaced it with the following.

<div class="RSS">
   <div class="RSS-date">
      <xsl:variable name="day">
         <xsl:value-of select="substring(pubDate,1,2)" />
      </xsl:variable>
      <xsl:variable name="month">
         <xsl:value-of select="substring(pubDate,4,2)" />
      </xsl:variable>
      <xsl:value-of select="$day"/><br/>          
      <xsl:choose>
         <xsl:when test="$month = 01">JAN</xsl:when>
         <xsl:when test="$month = 02">FEB</xsl:when>
         <xsl:when test="$month = 03">MAR</xsl:when>
         <xsl:when test="$month = 04">APR</xsl:when>
         <xsl:when test="$month = 05">MAY</xsl:when>
         <xsl:when test="$month = 06">JUN</xsl:when>
         <xsl:when test="$month = 07">JUL</xsl:when>
         <xsl:when test="$month = 08">AUG</xsl:when>
         <xsl:when test="$month = 09">SEP</xsl:when>
         <xsl:when test="$month = 10">OCT</xsl:when>
         <xsl:when test="$month = 11">NOV</xsl:when>
         <xsl:when test="$month = 12">DEC</xsl:when>
      </xsl:choose>
   </div>
   <a href="{link}" target="_blank"><xsl:value-of select="title" /></a><br/>
   <xsl:value-of select="description" /><br/><br/>
</div>

Basically:
  • the DIV's are used to make the RSS Feed look pretty  (remove them if they are causing issues)
  • I've created two variables, one named Day, one Month, which query the RSS field $Pubdate and pull back the correct numbers for the date of the article.
  • Displayed the date
  • Displayed the month as a three letter string depending on what numbers are retrieved
  • Displayed the title of the RSS feed as a hyperlink that opens up in a new window
Takes a little trial and error, but once you get a feel for XSL it's actually quite simple.

Obviously there are other fields in an RSS Feed that you can manipulate, I've listed them below in case anyone wishes to take it further:

These are available on all feeds: 

{rss:title} - Displays the title of the item
{rss:description} - Displays the short description
{rss:link} - Provides a link back to the original item.

These are not available on all feeds. The feed should be previewed before being saved to ensure that these are populated: 

{rss:content:encoded} - Displays the full text of the item. This applies to RSS2 feeds only.
{rss:dc:creator} - Displays the name of the item author
{rss:pubDate} - Displays the date the item was published
{rss:category} - Displays the category that the item is in
{rss:guid} - Displays the permanent link for the item.

(Field information pulled from: https://www.vbulletin.com/forum/showthread.php/234216-RSS-Feed-Variables)