2

We are using O365/Sharepoint Online 2013.

We are using a Sharepoint Online Blog as the official intranet news channel and it's all good but we are irritated about having the posts with full content showing on the blog summary/blog main page as some of our posts are quite long. How could we limit how much users can see of the posts' content on the summary page? I found one guide with a Javascript code for blog post excerpt but it doesn't fully work. Here is the code:

(function () {
//   Initialize the variables for overrides objects
    var overrideCtx = {};
    overrideCtx.Templates = {};
    /*
     * Using the Fields override leaves the rest of the rendering intact, but 
     * allows control over one or more specific fields in the existing view
     */
    overrideCtx.Templates.Fields = {
        'Body': { 'View' : CustomField }
    };

    /*
     * Register the template overrides.
     */
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();


function CustomField(ctx) {
    // Really basic field-level conditional formatting
    var ret

    ret = ctx.CurrentItem.Body;
    if (ret.length > 400)
    {
        ret = ret.substring(0, 400) + '&nbsp;&nbsp;&nbsp;<b><a href="/Blog/Lists/Posts/Post.aspx?ID=' + ctx.CurrentItem.ID + '">More ...</a></b>';
    }

    return ret;
}

I pasted that code into a empty file and saved it as "blogexcerpt.js" into the Site Collection root "Site assets" -folder. Then I went into the blog site with the Admin account and edited the Posts -web part and added "~sitecollection/_catalogs/masterpage/blogexcerpt.js" and applied and saved + closed the web part configuration window/box.

The posts were truncated but they only showed about one line of text and the alignment of the post boxes were a bit messed up too. There were no space between the post boxex/entries and they had "object/Object" -text under the post titles.

EDIT: I have almost no skills in writing JS and the code is from this discussion: http://social.technet.microsoft.com/Forums/sharepoint/en-US/9bb0033e-d17b-4c4f-b6c3-2df3d4eb00bf/show-blogs-in-excerpt-format

EDIT 2: I noticed the code that I found from the link above is quite old so is it possible that something in the SPO core code has changed and the code is referencing to some old core code terms or something like that?

I will be eternally grateful to aynone who can help us out with this problem! :-)

3

Since Body field is a Rich-Text field, the below example demonstrates how safely to truncate Html content. In the specified example, text content is retrieved first and then text is truncated.

Template file (Blogs.js)

(function () {

    var rCtx = {};
    rCtx.Templates = {};
    rCtx.Templates.Fields = {
        'Body': { 'View' : renderHeadline }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(rCtx);
})();


function renderHeadline(ctx) {
    var headlineLimit = 400;
    var body = ctx.CurrentItem.Body;  
    var headline = $(body).text(); 
    if (headline.length > headlineLimit)
    {
        headline = headline.substring(0,headlineLimit) + '&nbsp;&nbsp;&nbsp;<b><a href="' + ctx.listUrlDir + '/Post.aspx?ID=' + ctx.CurrentItem.ID + '">More ...</a></b>';
    }
    return headline;
}

Dependency: jQuery library: .text() function

How to apply changes

  1. Upload JavaScript template file into Site Assets library
  2. Open Blog home page in Edit mode (Edit page). Specify for Posts web part JS Link property: ~sitecollection/SiteAssets/Blogs.js

Result

enter image description here

  • 1
    Thank you so much! Really detailed and good answer with a 100% correct answer! :-) – Tenttu Sep 4 '14 at 6:03

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.