Andrey Markeev on SharePoint

Blog on SharePoint: RSS feed

October 30, 2012

The very first question I've got when I published the post about my public blog solution, was this: "But wait, and where is your RSS feed?".

Completely forgotten about, this is where it was :)

RSS feed in SharePoint is an OOTB feature. And since my blog is implemented as a document library, probably I could just publish link to the library feed and be done with it. But have you seen this feed?

Ugly and includes some unnecessary and non-customizable stuff. Not really what I can accept for my blog. I don't want my blog to merely work, you see, I want to it to work great.

So, I started to think about possible ways to provide a custom RSS feed. One idea was to use CQWP and use its RSS feed, another was to build RSS feed from scratch via DataFormWebPart. I wasn't sure that the second way was even possible, so I asked my friend (called Google) about it, and he pointed me to Raymond Mitchel's blog post, which explained exactly this: How to build custom RSS feed with Data Form Web Part.

In brief, it turns out you can just change output type of XSLT stylesheet to XML and generate appropriate RSS XML. It took half an hour for me to do this and to test the ready solution. This is my final code:

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" 
    xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" 
    version="1.0" 
    exclude-result-prefixes="xsl msxsl ddwrt" 
    xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" 
    xmlns:asp="http://schemas.microsoft.com/ASPNET/20" 
    xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:SharePoint="Microsoft.SharePoint.WebControls" 
    xmlns:ddwrt2="urn:frontpage:internal">

  <xsl:output method="xml" indent="yes"/>

  <xsl:decimal-format NaN=""/>

  <xsl:template match="/">

    <rss version="2.0">
      <channel>

        <title>Andrey Markeev on SharePoint</title>
        <link>http://markeev.com</link>
        <description>RSS feed for Andrey Markeev blog posts.</description>
        <generator>Custom RSS via Data Form Web Part</generator>
        <lastBuildDate>
          <xsl:value-of select="ddwrt:FormatDateTime(string(//Rows/Row[1]/@PublishedDate), 1033, 'dddd, MMMM dd, yyyy HH:mm:ss')" />
        </lastBuildDate>
        <ttl>60</ttl>


        <xsl:for-each select="/dsQueryResponse/Rows/Row">
          <item>
            <title>
              <xsl:value-of select="@Title" />
            </title>
            <link>
              http://markeev.com<xsl:value-of select="@FileRef" />
            </link>
            <description>
              <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
              <xsl:value-of select="@Preview" disable-output-escaping="yes" />
              <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
            </description>
            <author>Andrey Markeev</author>
            <pubDate>
              <xsl:value-of select="ddwrt:FormatDateTime(string(@PublishDate), 1033, 'dddd, MMMM dd, yyyy HH:mm:ss')"/>
            </pubDate>
            <guid isPermaLink="true">
              http://markeev.com<xsl:value-of select="@FileRef" />
            </guid>
          </item>
        </xsl:for-each>

      </channel>
    </rss>

  </xsl:template>

</xsl:stylesheet>

Note: all dates should be converted to RSS format.

Also, there is one small inconvenience: even if you remove all the content except DataFormWebPart itself from the page, SharePoint designer automatically generates head tag each time you save your page, as follows:

<head>
<meta name="WebPartPageExpansion" content="full" />
<meta name="ProgId" content="SharePoint.WebPartPage.Document" />
</head>

To workaround this, I had to place this tag inside server-side Panel with Visible set to false:

<asp:Panel runat="server" Visible="false">
<head>
<meta name="WebPartPageExpansion" content="full" />
<meta name="ProgId" content="SharePoint.WebPartPage.Document">
</head>
</asp:Panel>

Also, I had to move the whole fragment and place it to the bottom of the page, after DFWP.

Finally, I've got the desired result:

Summary

DataFormWebPart is a stunningly powerful webpart. And it turns out, that you can use it not only for displaying data from lists or other data sources on a web page, but also for presenting such data in some XML format (not obligatory RSS).

This opens many opportunities for XML-based integration with external systems.

Like it?

Posts of this series

  1. Public blog on SharePoint
  2. Blog on SharePoint: Series
  3. Blog on SharePoint: RSS feed (this post)

About me

I'm SharePoint MVP, published author, frequent speaker, opensource projects creator and online expert.

I'm one of the top 10 experts on SharePoint StackExchange:

profile for omlin at SharePoint, Q&A for SharePoint enthusiasts

You can learn more about me on LinkedIn and Facebook.

Want to follow?

Subscribe to RSS!

Twitter

Blog archive