3

I created a blog that uses the standard sharepoint 2010 blog site template. However as it currently stands the default.aspx page shows the entire blog article. I would like it to only show the first x number of words and then a link to the full article.

I also noticed that it does not allow me to edit the current view only select other views in the webpart setting... If someone could point me in the right direction I would appreciate it.

7

There are just a few easy steps to follow in order to customise you blog page, the best way is to edit the blog.xsl file responsibl of rendring the blog posts.

The standard SharePoint 2010 default page looklike :

Assuming that you have already created a blog site, let's go to the 14 hive of your server and edit the blog.xsl file.

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL\blog.xsl

Create a backup copy of your file in case !

Under the <div class="ms-PostBody"> find the section that looks like this:

<xsl:apply-templates select="$Fields[@Name='Body']" mode="PrintField">
<xsl:with-param name="thisNode" select="$thisNode"/>
<xsl:with-param name="Position" select="$Position"/>
</xsl:apply-templates>

And replace it with:

<xsl:value-of select="substring(@Body, 0, 500)" disable-output-escaping="yes"/>
<xsl:if test="string-length(@Body)>500">…</xsl:if>
<br/>
<a href="{$HttpVDir}/{$thisNode/../@resource.wss.lists_Folder}/{$thisNode/../@resource.wss.blogpost_Folder}/Post.aspx?ID={$thisNode/@ID}">Read More</a>

substring(@Body, 0, 500) will display only the 500 first characters, you can change it as you want.

<xsl:if test="string-length(@Body)>500">…</xsl:if> to display ... to avoid that half words

Save the file, and go your blog page. Edit the web part, expand "Miscellaneous", and in the XSL Link field, type the link to your XSL file. Press "Apply" and then "OK". Your page should now have a read more link!

Before: enter image description here

After: enter image description here

| improve this answer | |
2

You could either change the XSLT that renders the blog article (recommended)

  • Create your own XSL template that does truncating and adds a link (link is same as the article id)
  • There are some XSLT functions that strips off the HTML tags, you may have to use that.

or edit the page and insert a piece of javascript that

Note:

a. you may have to add some CSS to add min-height to the content div and hide the original text(overflow:hidden; visibility: hidden) to avoid flickering and then use jQuery to show (/w animation, effects).

b. For word count, trim html and get the text using jQuery .text() var myText = articleText.split(" ").slice(0, -1).join(" ") + "..."

| improve this answer | |
2

Thank you Nizar for pointing me in the right direction, I was able to use the majority of your answer however I noticed that I encountered some strange issues with HTML within the body of the post and it affecting how many words were displayed. As a result I went with the following steps:

  • Create a new version of the blog.xsl file located in:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL\blog.xsl

  • Add the following just after the line <xsl:param name="NoAJAX" select="1" />

StripHTML (source):

<xsl:template name="StripHTML">
  <xsl:param name="HTMLText"/>
  <xsl:choose>
   <xsl:when test="contains($HTMLText, '&gt;')">
    <xsl:call-template name="StripHTML">
      <xsl:with-param name="HTMLText" select="concat(substring-before($HTMLText, '&lt;'), substring-after($HTMLText, '&gt;'))"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$HTMLText"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

First N Words (source):

<xsl:template name="FirstNWords">
  <xsl:param name="TextData"/>
  <xsl:param name="WordCount"/>
  <xsl:param name="MoreText"/>
  <xsl:choose>
    <xsl:when test="$WordCount &gt; 1 and
        (string-length(substring-before($TextData, ' ')) &gt; 0 or
        string-length(substring-before($TextData, '  ')) &gt; 0)">
      <xsl:value-of select="concat(substring-before($TextData, ' '), ' ')" disable-output-escaping="yes"/>
      <xsl:call-template name="FirstNWords">
        <xsl:with-param name="TextData" select="substring-after($TextData, ' ')"/>
        <xsl:with-param name="WordCount" select="$WordCount - 1"/>
        <xsl:with-param name="MoreText" select="$MoreText"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="(string-length(substring-before($TextData, ' ')) &gt; 0 or
        string-length(substring-before($TextData, '  ')) &gt; 0)">
      <xsl:value-of select="concat(substring-before($TextData, ' '), $MoreText)" disable-output-escaping="yes"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$TextData" disable-output-escaping="yes"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Under the <div class="ms-PostBody"> find the section that looks like this...

<xsl:apply-templates select="$Fields[@Name='Body']" mode="PrintField">
<xsl:with-param name="thisNode" select="$thisNode"/>
<xsl:with-param name="Position" select="$Position"/>
</xsl:apply-templates>

And replace it with this:

<xsl:variable name="BodyText">
<xsl:call-template name="StripHTML">
  <xsl:with-param name="HTMLText" select="@Body" /> 
  </xsl:call-template>
  </xsl:variable>
<xsl:call-template name="FirstNWords">
  <xsl:with-param name="TextData" select="$BodyText" /> 
  <xsl:with-param name="WordCount" select="25" /> 
  <xsl:with-param name="MoreText" select="'...'" /> 
  </xsl:call-template>
  <br /> 
  <a href="{$HttpVDir}/{$thisNode/../@resource.wss.lists_Folder}/{$thisNode/../@resource.wss.blogpost_Folder}/Post.aspx?ID={$thisNode/@ID}">Read More</a>

You can replace the '25' above with the number of words you would like to show...

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.