baristaner
06/13/2024, 8:46 AMc#
public SaveAndPublishHandler(IContentService contentService)
{
_contentService = contentService;
}
public void Initialize()
{
ContentService.Saved += ContentService_Saved;
}
public void Terminate()
{
ContentService.Saved -= ContentService_Saved;
}
private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)
{
foreach (var content in e.SavedEntities)
{
if (content.ParentId == 1249)
{
var dataToSend = new { ContentId = content.Id, Title = content.Name };
var json = Newtonsoft.Json.JsonConvert.SerializeObject(dataToSend);
var contentToSend = new StringContent(json, Encoding.UTF8, "application/json");
using (var httpClient = new HttpClient())
{
var endpointUrl = "http://localhost:65137/umbraco/surface/UpdateApprovedAccount/UpdateApprovedAccountV2";
var response = httpClient.PostAsync(endpointUrl, contentToSend).Result;
var responseContent = response.Content.ReadAsStringAsync().Result;
}
}
}
}
Composing :
c#
public void Compose(Composition composition) {
composition.Components().Append<SaveAndPublishHandler>();
}
Lewis Heaton
06/13/2024, 9:04 AMLewis Heaton
06/13/2024, 9:11 AMpublic class DontShoutComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.AddNotificationHandler<ContentPublishingNotification, DontShout>();
}
}
And the actual method that gets run
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Notifications;
namespace Umbraco.Docs.Samples.Web.Notifications;
public class DontShout : INotificationHandler<ContentPublishingNotification>
{
public void Handle(ContentPublishingNotification notification)
{
foreach (var node in notification.PublishedEntities)
{
if (node.ContentType.Alias.Equals("announcement"))
{
var newsArticleTitle = node.GetValue<string>("title");
if (!string.IsNullOrWhiteSpace(newsArticleTitle) && newsArticleTitle.Equals(newsArticleTitle.ToUpper()))
{
notification.CancelOperation(new EventMessage("Corporate style guideline infringement",
"Don't put the announcement title in upper case, no need to shout!",
EventMessageType.Error));
}
}
}
}
}
Lewis Heaton
06/13/2024, 9:12 AMbaristaner
06/13/2024, 9:19 AMLewis Heaton
06/13/2024, 9:20 AMbaristaner
06/13/2024, 9:20 AMLewis Heaton
06/13/2024, 9:21 AMbaristaner
06/13/2024, 9:21 AMbaristaner
06/13/2024, 10:44 AMbaristaner
06/13/2024, 10:45 AMbaristaner
06/13/2024, 10:46 AMLewis Heaton
06/13/2024, 11:25 AMbaristaner
06/13/2024, 12:19 PMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by