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}};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(inCtx);
function ItemOverride(itemCtx) {
var result = "";
// when in 'comments' list
if (ctx.ListTitle == "Comments") {
//get number of comments
console.log(ctx.ListData.Row.length);
//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++){
console.log(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;
}
}));
Update:
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