Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sebastian Dammark 581 posts 1385 karma points
    Sep 28, 2023 @ 10:07
    Sebastian Dammark
    0

    Any newer solution to this ? (Only allow user to create a certain doctype once)

    Hello mighty community,

    Since the thread below is almost 10 years old, I was wondering if there is a smarter way of solving this today ?

    Old thread: https://our.umbraco.com/forum/using/ui-questions/46240-Only-allow-user-to-create-a-certain-doctype-once

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Sep 28, 2023 @ 19:42
    Marc Goodson
    100

    Hi Sebastian

    Yes, there is! - but it's super secret and you mustn't tell anyone about it...

    It looks like as of Umbraco V9.5 this PR was accepted into the Core:

    https://github.com/umbraco/Umbraco-CMS/pull/12116

    This introduces the concept of a 'SendingAllowedChildrenNotification, which is issued whenever somebody goes to create a new node underneath a node...

    and this notification contains a list of all the types of things that the current editor is allowed to create... and you can 'check who is logged in' or 'check to see whether a certain node has already been created' and modify the list of things that the editor is allowed to create...

    The example in the PR description shows checking for a 'Settings' node and not allowing it to be created if it already exists:

    public class TestCode : INotificationHandler<SendingAllowedChildrenNotification>
        {
            public void Handle(SendingAllowedChildrenNotification notification)
            {
                var children = notification.Children.ToArray();
                if (children.All(it => it.Alias != "settings"))
                    return;
    
                var currentSettings = notification.UmbracoContext.Content.GetSingleByXPath("//settings");
                if (currentSettings is null)
                    return;
    
                notification.Children = children.Where(it => it.Alias != "settings");
            }
        }
    

    The old way still works of allowing a Settings node to be created and then you create it yourself, then disallow it from being created again... but this programmatically way would allow infinite possibilities, only allow people to create a certain type of page on a Wednesday for example....

    https://docs.umbraco.com/umbraco-cms/reference/notifications/sendingallowedchildrennotifications

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft