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: