Wednesday, August 08, 2007

How to put a latest post of your blog on sharepoint homepage

Recently, we had a requirement to create a blog site and also put a latest blog entry on the homepage of the

sharepoint site. To make things difficult, customer wanted only the preview of the latest post i.e. first few lines

and an out of the box solution :)

I was stuck with an awesome idea to accomplish this with DataForm webpart in MOSS 2007 and bingo, it worked ! Here is how to do it :

Open the page on which you want to place the blog entry in Sharepoint Designer. If its a publishing page, SPD wont allow you to directly open it, but instead it will open the page layout instead. To circumvent this, create a webpart page through sharepoint and open it in sharepoint Designer.

Once the page is open in sharpoint designer, Go to Insert -> Data View. It will open a data sources pane to select the data source. However our data source is a seperate blog site. Thus, Choose Connect to another library, Enter a display name and enter the URL of the blog site.

Now you will get the lists of all lists and libraries from the blog site. Choose the list Posts as it is the list in which blog posts are contained by default.

Once you have inserted the dataview, now we have to customize the XSLT of the DataForm webpart to make it show us only a preview of single post.

For this,

* Strip of the headers of the list, called Title, Body etc. enclosed inside the <th> tags, we dont want to show the headers obviously.

* Now to show the preview, we will make use of the function

<xsl:value-of select="substring-before(@Body,'<p')" disable-output-escaping="yes"/>

What this function does is, gives us all of the string upto the string <p. Now since @Body is in actual an HTML representation of the body post,

we will get the first paragraph of the blog in the result. disable-output-escaping="yes" will make sure that it does not renders the HTML as it is.

* You can also sort it by Modified to show the latest entry automatically.

For those, who does not want to make these modifications themselves, I am giving the snippet of the XSL which I used here :

<xsl:stylesheet xmlns:x="" xmlns:d="" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="" xmlns:asp="" xmlns:__designer="" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:output method="html" indent="no"/>
<xsl:decimal-format NaN=""/>
<xsl:param name="dvt_apos">'</xsl:param>
<xsl:variable name="dvt_1_automode">0</xsl:variable>
<xsl:template match="/" xmlns:x="" xmlns:d="" xmlns:asp="" xmlns:__designer="" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
<xsl:call-template name="dvt_1"/>

<xsl:template name="dvt_1">
<xsl:variable name="dvt_StyleName">Table</xsl:variable>
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/><table border="0" width="100%" cellpadding="2" cellspacing="0">
<tr valign="top">
<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<th class="ms-vh" width="1%" nowrap="nowrap"></th>
<xsl:call-template name="dvt_1.body">
<xsl:with-param name="Rows" select="$Rows"/>
<xsl:template name="dvt_1.body">
<xsl:param name="Rows"/>
<xsl:for-each select="$Rows">
<xsl:call-template name="dvt_1.rowview"/>
<xsl:template name="dvt_1.rowview">
<td class="ms-vb">
Posted : <xsl:value-of select="ddwrt:FormatDate(string(@Created),1033,3)"></xsl:value-of> by <xsl:value-of select="@Editor" disable-output-escaping="yes" />
<b><xsl:value-of select="@Title"></xsl:value-of></b><p/>
<xsl:value-of select="substring-before(@Body,'<p')" disable-output-escaping="yes"/>
</td><td class="ms-vb">
</td><xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
<td class="ms-vb" width="1%" nowrap="nowrap">
<span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
<td><a class="ms-vb" href="">Read More</a></td>