0

Is it possible to show the first 600 characters of a post in a blog site? Can i use JSlink to achieve this in a blog site?

Thanks in Advance

  • @RohitWaghela How does that link help? – Christoffer Apr 18 '18 at 10:56
  • Ohh I am sorry. This link was meant for different question .... I mistakenly post it here... I have deleted the link. Thanks for pointing out ...:) This is the question for which that link was meant for - sharepoint.stackexchange.com/questions/240189/… – Rohit Waghela Apr 18 '18 at 10:58
  • @RohitWaghela Haha, thought it was meant for something else. Carry on! :) – Christoffer Apr 18 '18 at 11:07
1

Threw this together, works for me in SharePoint 2013.

I bet there is room for improvement as I am no JavaScript guru. Add this to a script editor webpart on the blog-post site. This truncates all the posts when the length of the post is more than 600 characters.

<script type="text/javascript">
window.onload = function truncatePosts() {
var element = document.getElementsByClassName("ms-blog-postBody");
for (var i = 0; i < element.length; i++) {
    var text = element[i].innerText;
        if(text.length >= 600) {
            var truncatedText = text.substring(0,600) + ' ...';
            element.innerText = truncatedText;
            document.getElementsByClassName("ms-blog-postBody")[i].innerText = element.innerText;
        }
    };
};
</script>
0

You can use CSR to achieve this. Here is a nice article on Trimming the content of multiline text using CSR.

I have modified it a little bit which you can try. Create a js file, paste the following code and use that js file as JS Link in the blog post web part.

(function () {
      function registerRenderer() {
        var ctxObj = {};
        ctxObj.Templates = {};  
        ctxObj.Templates = {
            Fields : {
                'Body': {
                    View : function (ctx) {
                        var bodyValue = ctx.CurrentItem.Body;
                        //This regex expression is used to delete html tags from the Body field
                        var regex = /(<([^>]+)>)/ig;
                        bodyValue = bodyValue.replace(regex, "");
                        var newBodyValue = bodyValue;
                        var returnString = "<span title='" + bodyValue + "'>" + newBodyValue + "</span>";
                        if (bodyValue && bodyValue.length >= 600){
                            newBodyValue = bodyValue.substring(0, 600) + " ...";
                            var displayItemFormUrl = "/blogs/Lists/Posts/Post.aspx?ID=" + ctx.CurrentItem.ID;
                            returnString = "<span title='" + bodyValue + "'>" + newBodyValue + "</span>&nbsp;<span><a href='"+displayItemFormUrl+"'>Read More</a></span>";
                        }
                        return returnString;
                    }
                },
            }
        };
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctxObj);
    }
    ExecuteOrDelayUntilScriptLoaded(registerRenderer, 'clienttemplates.js');
})();

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.