Wednesday, February 15, 2012

Creating a basic news system

A regular task which is used in SharePoint is the creation of a customised news feed. A basic approach to this is a simple three step approach:

1 - Modify the content type to include the fields not included in the default article content type
2 - Create a new page layout which includes the newly added fields for editing
3 - Modify the ItemStyle.xsl to create a new Content Query web part. You can use a data view web part (by pointing to the Pages library and filter on the content type matching the required value if you'd prefer).

Step 1
Create a new Content Type in the System Settings based on the existing Page Layout -> Article page (I called mine News Page). This part isn't particularly tricky, although it's worth pointing out that I added two fields, one called RollupText and one call ShowOnHomePage.

Step 2
This can be a bit trickier. The easiest approach is to copy one of the Article page layouts, then make the required changes. You can use the tag to add sections which are only visible when the page is in edit mode (useful for adding metadata which isn't meant to be visible on the news page itself).

Here is a snippet of my News.aspx page layout with my new RollupText and ShowOnHomePage fields:



<PublishingWebControls:editmodepanel runat="server" id="editmodepanel2">
<table class="header">
<tr>
<td class="rollup-text">
<SharePointWebControls:NoteField FieldName="RollupText" runat="server"/>
</td>
<td class="rollup-text-description">
<span>This text will appear in the rollup summary web part effectively acting as an article summary. Only plain text can be entered into this field (i.e. no font formatting, images or HTML will be accepted).</span>
</td>
</tr>
<tr>
<td class="show-on-home-page">
<SharePointWebControls:BooleanField FieldName="ShowOnHomePage" runat="server"/>
</td>
</tr>
</table>
</PublishingWebControls:editmodepanel>


Step 3
The hardest part of all is the small little things that can go wrong if you've never tried it before. Things I encountered were:
- ItemStyle.xsl can be difficult to recreate due to my inexperience with XML. Here is my XML chunk which I ended up using for my web part:

<xsl:template name="ImageAlternate" match="Row[@Style='ImageAlternate']" mode="itemstyle">
<xsl:variable name="CurPosition" select="count(./preceding-sibling::*)" />
<xsl:variable name="css-class">
<xsl:choose>
<xsl:when test="($CurPosition mod 2 = 0)">news-rollup-image-right</xsl:when>
<xsl:otherwise>news-rollup-image-left</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<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="LinkTarget">
<xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>
</xsl:variable>
<div class="news-rollup-container" link="{$SafeLinkUrl}">
<div id="linkitem" class="news-rollup-item">
<xsl:if test="string-length($SafeImageUrl) != 0">
<div class="news-rollup-image {$css-class}">
<img class="news-rollup-image" src="{$SafeImageUrl}" alt="{@ImageUrlAltText}" />
</div>
</xsl:if>
<div class="news-rollup-text">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<div class="news-rollup-title">
<xsl:value-of select="$DisplayTitle"/>
</div>
<div class="news-rollup-description">
<xsl:value-of select="@RollupText" disable-output-escaping="yes"/>
</div>
</div>
</div>
</div>
</xsl:template>

- After you add the Content Query web part to the page that you intend to use, then you need to make sure you've selected your new page layout as the source. After you've done this you MUST EXPORT THE WEBPART and add in the CommonViewFields property the new properties that you've added if you want to view them in the web part. CQWPs have a very limited selection of fields to display.

- After this it was really all styling and javascript to make it look and operate correctly. I like to add a 'link' attribute to the outer containing div for each element which links to the news article, then use javascript on the page load to set up a mouse over CSS style and click event. I would prefer to user the CSS :hover tag, but versions of IE that I must support doesn't work with it.

- Also worth changing can be the UseCache setting in the web part.  This is set to True by default, but can prevent certain fields coming through if they are recently added.  I had to set this to false as I could see content in my CQWP in edit mode, but then nothing would appear in the standard view.

- If you can't find out what the field type for the new column you've added is, try creating a new page layout in SharePoint designer, and add the fields manually.  Designer should create the correct fields for you, so even if you don't want to use designer for creating your page layouts, it can help with small annoying things (like exactly what field type a "Yes/No" field is (it's a SharePointWebControls:BooleanField).