Episerver - How to send Notification Message during EventsPublishedContent Event

563 Views Asked by At

I have seen several examples of how to send validation messages back to the UI, while editing content, like this.

public class StandardPageValidator : IValidate<standardpage>
{
    IEnumerable<validationerror> IValidate<standardpage>.Validate(StandardPage instance)
    {
        // Silly example to validate if the PageName and MainBody properties start with the same letter
        if (instance.PageName.StartsWith(EPiServer.Core.Html.TextIndexer.StripHtml(instance.MainBody.ToHtmlString().Substring(0, 1), int.MaxValue)))
        {
            return new[] { new ValidationError() { 
                ErrorMessage = "Main body and PageName cannot start with the same letter", 
                PropertyName = "PageName", RelatedProperties = new string[] { "MainBody" }, 
                Severity = ValidationErrorSeverity.Error, 
                ValidationType = ValidationErrorType.AttributeMatched
            } };
        }

        return new ValidationError[0];
    }
}

However I would like to send a message back to UI after intercepting the Published Content Event, but this method returns void so how can I do this?

    public void Initialize(InitializationEngine context)
    {
        var events = ServiceLocator.Current.GetInstance<IContentEvents>();
        events.PublishedContent += EventsPublishedContent;
    }
    private void EventsPublishedContent(object sender, ContentEventArgs e)
    {
    if (e.Content is myType)
    {
        //do some business logic work....


       //How can I send a Info Message back to the UI here?
    }
}
1

There are 1 best solutions below

4
On

You can do this inside EventsPublishedContent in your code sample:

e.CancelAction = true;
e.CancelReason = "This message will be displayed in the UI.";