I have a blog list (SP2016) which I am using to display news items.

On the blog list I have a workflow which sets the Created by(Author) field to system account. for a better ux I am capturing the actual author in a separate field as part of workflow (Author0).

I have managed to do this for the original post. The following code sets the author(author0) and author id (NewAuthorId) on the frontend for the original post.

When I try to do list, it also impacts the 'Comments' below each post.

I have tried to apply the following code to also set the author and author id for the comments, but the code isn't iterating through the code, it is setting the author and id as the value of the last item looped through.

I'm fairly new to this so apologies for the code - I've added comments to help understand what I am trying to do.

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", (function () {
    var inCtx = {};
    inCtx.Templates = {};
    inCtx.Templates.Fields = {'Author': {  'View'  :  ItemOverride}};
    function ItemOverride(itemCtx) {
        var result = "";

        // when in 'comments' list
        if (ctx.ListTitle == "Comments") {
        //get number of comments

            //if the post id is equal to comment parent id
            if (ctx.ListData.Row[0].ID = ctx.ListData.Row[0].PostTitle[0].lookupId ) { 

                // for each item in comments list
                for(var i = 0; (i<ctx.ListData.Row.length); i++){

                // ****not iterated - just setting to value of last row ****
                    //set the authorid to user id to take to profile        
                result = "<a href='/_layouts/15/userdisp.aspx?ID=" + ctx.ListData.Row[i].NewAuthorId + "'>";
                    //set the author name
                result += ctx.ListData.Row[i].Author0; 
                    //close hyperlink
                result += "</a>";

         // when in 'posts' list
        else if (ctx.ListTitle == "Posts") {
            // as above, set id and author name to display.
            result += "<a href='/_layouts/15/userdisp.aspx?ID=" + ctx.ListData.Row[0].NewAuthorId + "'>";
            result += ctx.ListData.Row[0].Author0 
            result += "</a>";
        return result;


is my issue not to do with this line - 'inCtx.Templates.Fields = {'Author': { 'View' : ItemOverride}};'

As it will always replace Author with whatever result is returned?

Is it possible to iterate through the number of authors, and replace with the relevant author from the comments list? enter code here

  • 2
    result ends up with last row values because each iteration resets result variable. – Salah Jan 15 at 10:30
  • ohh the missing += error that we all make – Anand Jan 15 at 17:37
  • Hi , thanks for your response. When I add in a 'return result' in after my For loop, it just displays the first item of the list instead of the last item as it currently does. Any other suggestions ? – Geolog1cal Jan 16 at 14:20

You're missing a +=

 result = "<a href='/_layouts/15/userdisp.aspx?ID=" + ctx.ListData.Row[i].NewAuthorId + "'>";

should be

result += "<a href='/_layouts/15/userdisp.aspx?ID=" + ctx.ListData.Row[i].NewAuthorId + "'>";
  • Hi, thanks for your response.I had the missing '+' previously. When this is present, All the authors for each comment display in the author field. Instead of the relevant author for the relevant comment. – Geolog1cal Jan 16 at 14:18

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.