Umbraco 13
Hi,
I am trying to build a NotificationHandler that checks specific content on a specific document type, I can get it to work, if the contents already been saved.
But if it a new content item, I can’t work out how to get the image type I have on there and work out how to get the custom fields.
Here the code for my NotificationHandler.
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
namespace UmbracoCMS.App_Code.Handler
{
public class saveNotificationHandler : INotificationHandler
{
private IMediaService _mediaService;
private IContentService _contentservice;
private readonly IUmbracoContextFactory _umbracoContextFactory;
public saveNotificationHandler(IMediaService mediaService, IContentService contentservice, IUmbracoContextFactory umbracoContextFactory)
{
_mediaService = mediaService;
_contentservice = contentservice;
_umbracoContextFactory = umbracoContextFactory;
}
public void checknewsArticalImages(IContent node, ContentPublishingNotification notification)
{
if (node.ContentType.Alias.Equals("newsArticle"))
{
IContent c = _contentservice.GetById(node.Id);
if (c != null) {
using (var ctxRef = _umbracoContextFactory.EnsureUmbracoContext())
{
IPublishedContent publishedContent = ctxRef.UmbracoContext.Content.GetById(node.Id);
if (publishedContent != null)
{
IPublishedContent img = publishedContent.Value<IPublishedContent>("newsArticleImage");
var alttext = img.Value<string>("altText");
if (alttext.Length == 0)
{
notification.CancelOperation(new EventMessage("News Error",
"Please check the ALT tag on the image, it dose not seem to be set!",
EventMessageType.Error));
}
}
}
} else
{
//None Saved Content.
string s = "";
}
}
}
public void Handle(ContentPublishingNotification notification)
{
foreach (var node in notification.PublishedEntities)
{
checknewsArticalImages(node, notification);
}
}
}
}
Any help would be grate!
Thanks
Darren