1

I'm replacing the "created by" field in a blog posts list with a custom user defined "author" field using a simple jslink script. This works fine but when a user posts a comment, the jslink change appears to get carried over to the comments list and since there's no "author" field present, returns "undefined".

(function () {

var overrideCtx = {};

overrideCtx.Templates = {};

overrideCtx.Templates.Fields = {'Author':{'View':CBody}};        

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);

})();

function CBody(ctx) {

var ret = "<span >" + ctx.CurrentItem.Author0 + " </span>" 


return ret;

} 
6
  • Put a undefined check on Author Field. Commented May 29, 2015 at 15:22
  • What about hidding the created by instead of replacing it completely? and if Author is undefined you can show the Created by
    – yngrdyn
    Commented May 29, 2015 at 15:25
  • @AmalHashim can you elaborate?
    – matt
    Commented May 29, 2015 at 16:48
  • Can you post relevant code? Commented May 29, 2015 at 16:49
  • @yngrdyn that might be a good idea. I'll give it a shot.
    – matt
    Commented May 29, 2015 at 16:49

2 Answers 2

3

By when a user posts a comment, you probably mean that user is located on the page Post.aspx, right?

Assume the custom field PostAuthor have to be displayed instead of standard Author field in blog page and the following template is used for that purpose:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({

      Templates: {
           Fields: {
               "Author": {
                   View: renderPostAuthor
               }
           }
      },

      ListTemplateType: 301,

    });

});


function renderPostAuthor(cxt) {
  return "<span >" + ctx.CurrentItem.PostAuthor[0].title + " </span>";
}

In order to display additional field on Posts.aspx page, the view that is rendered on this page need to modified, in particular custom field have to be added into the view.

Since the view that is rendered on Post.aspx page is hidden the following example demonstrates how to add field into view (PostAuthor in my case) using CSOM API:

using (var ctx = new ClientContext(webUri))
{     
     var list = ctx.Web.Lists.GetByTitle("Posts");
     var result = ctx.LoadQuery(list.Views.Where(v => v.BaseViewId == "7"));
     ctx.ExecuteQuery();
     var views = result.ToList();
     if (views.Count > 0)
     {
         var view = views[0];
         view.ViewFields.Add("PostAuthor");
         view.Update();
         ctx.ExecuteQuery();
      }
} 

Once the view is updated and template is applied the blog page might look like this:

enter image description here

2
  • I can add the custom author field to a blog post using a script called by jslink without issue. That works but then somehow breaks the Author field in the comments list. I assume the script I'm using gets attached to both the posts and comments list due to a faulty design.
    – matt
    Commented Jun 1, 2015 at 15:46
  • Hi, I tried above solution and when I am running ctx.CurrentItem.PostAuthor[0].title on console it is giving proper output. but when i am using same ctx.CurrentItem.PostAuthor[0].title in JSLink, I am getting [object Object]. why?
    – Anil Pal
    Commented Feb 1, 2017 at 15:50
1

Try

function CBody(ctx) {
    if(ctx.CurrentItem.hasOwnProperty("Author0") && ctx.CurrentItem.Author0 != undefined) {
        var ret = "<span >" + ctx.CurrentItem.Author0 + " </span>" 
        return ret;
    }
    return "";
} 
4
  • Hmm, that gives me "[object Object]undefined [object Object]undefined"....
    – matt
    Commented May 29, 2015 at 18:34
  • Correction - that shows correctly in post, but nothing in comments list. (Author is empty).
    – matt
    Commented May 29, 2015 at 18:43
  • Try my edited answer. Commented May 29, 2015 at 18:46
  • No luck....I should be adding this to the posts not the comments list, right?
    – matt
    Commented May 29, 2015 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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