Content approval in Episerver CMS has been around for a while now, but coding examples using it are still fairly hard to find. Here is a simple one that might come in handy.
A current client of mine has a lot of editors contributing important content that needs to be reviewed before publishing, and for that they are using Episerver Content Approvals.
It would be useful for them if all pages came with a little helpful for them if all content came with a little built-in infobox, only shown in edit-mode, that let's editors know who created the content, who has changed it, who is the 'official owner' and who is going to review it.
I just threw a few lines of code together to show just this case, and I figured it might come in handy for others as there are fairly few examples of working programmatically with Content Approvals.
For this example I'm using an Alloy site, and I have 2 users: Admin and Approver.
The approval flow is very simple and looks like this:
My code is really simple; I basically just created an Html extension methods that outputs an ugly input box (apologies for the terrible html/css, this is purely a demo). It's then inserted in the top of header.cshtml. like this:
@Html.ContentInfoBox(Model.CurrentPage.ContentLink)
public static IHtmlString ContentInfoBox(this HtmlHelper helper, ContentReference cref)
{
if (PageEditing.PageIsInEditMode)
{
var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
var apdefrepo = ServiceLocator.Current.GetInstance<IApprovalDefinitionRepository>();
IContent pd = repo.Get<IContent>(cref);
var apdef = apdefrepo.ResolveAsync(cref).Result;
var revs = apdef?.Definition?.Steps?.SelectMany(st => st.Reviewers).Select(r => r.Name).ToArray();
string reviewers = (revs == null || apdef.Definition.IsEnabled==false) ? "(No Approval Flow)" : string.Join(",", revs);
string creator = (pd as IChangeTrackable).CreatedBy;
string modifier = (pd as IChangeTrackable).ChangedBy;
return
new HtmlString($"<div style=\"border: 1px solid #000000; width:200px;\"><b>Created by:</b> {creator}<br/><b>Changed by:</b>{modifier}<br/><b>Reviewers:</b>{reviewers}</div>");
}
else return new HtmlString(string.Empty);
}
The end result looks like this:
Recent posts