Example of how to use a SendingAllowedChildren Notification
The SendingAllowedChildrenNotification enables you to manipulate the Document Types that will be shown in the create menu when adding new content in the backoffice.
Usage
With the example below we can ensure that a Document Type cannot be selected if the type already exists in the Content tree.
usingSystem.Web;usingUmbraco.Cms.Core.Events;usingUmbraco.Cms.Core.Notifications;namespaceUmbraco.Docs.Samples.Web.Notifications;publicclassSendingAllowedChildrenNotificationHandler:INotificationHandler<SendingAllowedChildrenNotification>{publicvoidHandle(SendingAllowedChildrenNotification notification) {conststring contentIdKey ="contentId"; // Try get the id from the content item in the backoffice var queryStringCollection = HttpUtility.ParseQueryString(notification.UmbracoContext.OriginalRequestUrl.Query);
if (!queryStringCollection.ContainsKey(contentIdKey)) {return; }var contentId =queryStringCollection[contentIdKey].TryConvertTo<int>().ResultOr(-1);if (contentId ==-1) {return; }var content =notification.UmbracoContext.Content?.GetById(true, contentId);if (content isnull) {return; } // Allowed children as configured in the backofficevar allowedChildren =notification.Children.ToList();if (content.ChildrenForAllCulturesisnotnull) { // Get all children of current pagevar childNodes =content.ChildrenForAllCultures.ToList(); // If there is a Settings page already created, then don't allow it anymore // You can also use the ModelTypeAlias property from your PublishedModel for comparison, // like Settings.ModelTypeAlias if you have set models builder to generate SourceCode modelsif (childNodes.Any(x =>x.ContentType.Alias=="settings")) {allowedChildren.RemoveAll(x =>x.Alias=="settings"); } } // Update the allowed childrennotification.Children= allowedChildren; }}
You also need to register this notification handler. You can achieve this by updating the Program class like: