1

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?
    }
}
Ayo Adesina
  • 2,231
  • 3
  • 37
  • 71

1 Answers1

1

You can do this inside EventsPublishedContent in your code sample:

e.CancelAction = true;
e.CancelReason = "This message will be displayed in the UI.";
Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72
  • That would be to cancel the action, I don't want to cancel the action I just want to give the user a notification message. – Ayo Adesina Apr 18 '18 at 09:58
  • Oh, I misunderstood, I thought you wanted to invalidate the action. Have you looked at notifications? https://world.episerver.com/documentation/developer-guides/CMS/using-notifications/ – Ted Nyberg Apr 18 '18 at 11:08
  • Haven't seen that before, I'll check it out, do the messages end up looking the same as the validation ones as in top right when you click publish? – Ayo Adesina Apr 18 '18 at 14:32
  • No, they would appear among the user notification messages. The red number next to the Publish button is reserved for validation errors, so from a UX perspective it would be better to display your messages somewhere else. You can always create your own Dojo widget to have full control over how/where your UI feedback is displayed. – Ted Nyberg Apr 18 '18 at 14:55