In Umbraco, you may want to perform certain actions when a content item or member is being saved or created. This post shows you how to do that.

This is relevant for Umbraco projects that use MVC.

Prevent creation or editing of content items which have a certain documentTypeAlias

using System.Linq;
using Umbraco.Core; using Umbraco.Core.Events; using Umbraco.Core.Services; using Umbraco.Core.Models; using Umbraco.Web;
namespace CodeShare.Web.EventHandlers
{
    public class ContentEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saving += ContentService_Saving;
        }
        private void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
        {
            foreach (IContent contentItem in e.SavedEntities.Where(x => x.ContentType.Alias == "aliasFoo"))
            {
                if (contentItem.Id <= 0) //new record
                {
                    e.CancelOperation(new EventMessage("Foo permissions", "You cannot create foo items", EventMessageType.Error));
                }
                else //existing record
                {
                    if (contentItem.IsDirty())
                    {
                        e.CancelOperation(new EventMessage("Foo permissions", "You cannot edit foo records", EventMessageType.Error));
                    }
                }
            }
        }
    }
}

When it finds a new record it cancels the save event and returns an error message

e.CancelOperation(new EventMessage("Foo permissions", "You cannot create foo items", EventMessageType.Error));

You could instead just cancel the event like this

e.Cancel = true;

Send an email to admin when a content item is saved 

using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Services;
using Umbraco.Core.Models;
using Umbraco.Web;
using CodeShare.Library;
namespace CodeShare.Web.Helpers
{
    public class ContentEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saved += ContentService_Saved;         }
        private void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)         {             foreach (IContent contentItem in e.SavedEntities)             {                 if (contentItem.ContentType.Alias == "aliasBar")                 {                     EmailService.SendEmailToAdmin(contentItem.Id);                 }             }         }     } }

Send an email to a member when they are approved

using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Services;
using Umbraco.Core.Models;
using Umbraco.Web;
using CodeShare.Library;
namespace CodeShare.Web.Helpers {     public class MemberEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)        
        {
            MemberService.Saving += MemberService_Saving;
        }
        private void MemberService_Saving(IMemberService sender, SaveEventArgs<IMember> e)
        {
            foreach (IMember member in e.SavedEntities)
            {
                //Check user is approved and the approve status has only just been changed (isdirty)
                if (member.IsApproved && member.HasProperty("umbracoMemberApproved") && member.Properties["umbracoMemberApproved"].IsDirty())
                {
                    EmailService.SendApprovalEmailToMember(member);
                }
            }
        }
    }
}

I hope you find this post useful. As always, please feel free to share with your friends and colleagues.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to ignore a file locally in git without ignoring it from the repository

This post shows you how you can ignore a file in git locally without ignoring it from the repository…

Read Post

How to get the picked item name in Stacked Content and Nested Content using ncNodeName

This post shows you how you can get the name of the picked item in Nested Content or Stacked Content…

Read Post

How I use source control for my Umbraco website source code and media

In this post I share my approach to using source control for my Umbraco website source code and medi…

Read Post

How to solve the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0 ..."

This post will show you how to solve the error "Could not load file or assembly 'Microsoft.CodeDom.P…

Read Post