Copied to clipboard

Flag this post as spam?

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


  • lori ryan 239 posts 573 karma points
    Jan 30, 2020 @ 09:11
    lori ryan
    0

    Only allow one instance of a dooc type on Root

    Hi

    Just wondering whats the best way of stopping users from creating certain doctypes. For example sitemap doctype where you would only want one on the root of the site.

  • lori ryan 239 posts 573 karma points
    Jan 31, 2020 @ 08:34
    lori ryan
    0

    anyone???

  • Magnus Eriksson 122 posts 362 karma points
    Jan 31, 2020 @ 08:41
    Magnus Eriksson
    100

    I usually create the page for the client then after disable the creation of the doc type.

    Regards, Magnus

  • Ambert van Unen 175 posts 819 karma points c-trib
    Jan 31, 2020 @ 13:27
    Ambert van Unen
    1

    Basically what Magnus said.

    Create the page, then disallow the creation of it. You could also do it with permissions, but that is more of a hassle I guess.

  • lori ryan 239 posts 573 karma points
    Jan 31, 2020 @ 14:38
    lori ryan
    0

    Thanks guys was looking for a more elegant solution but sometimes the easy root will do it :-)

  • Russell 13 posts 85 karma points c-trib
    1 week ago
    Russell
    0

    This answer is a bit late but there's a possibility that someone may find this useful.

    1. Make sure the root page is the only page that allows that type as a child

    2. Use a ContentService notification such that if an attempt is made to save a second node with that doc type then cancel that operation.

    Example below for a doc type with alias "subSettings"

    public class RestrictPageType : INotificationHandler<ContentSavingNotification>
    {
        private readonly IContentService _contentService;
        private readonly IContentTypeService _contentTypeService;
    
        public RestrictPageType(IContentService contentService, IContentTypeService contentTypeService)
        {
            _contentService = contentService;
            _contentTypeService = contentTypeService;
        }
    
        public void Handle(ContentSavingNotification notification)
        {
            string thereCanBeOnlyOne = "subSettings";
            IContentType subSettingType = _contentTypeService.Get(thereCanBeOnlyOne) ?? throw new Exception($"Invalid alias: {thereCanBeOnlyOne}");
            foreach (var node in notification.SavedEntities)
            {
                if (node.ContentType.Alias.Equals(thereCanBeOnlyOne))
                {
                    IContent? publishedPage = _contentService
                         .GetPagedOfType(subSettingType.Id, 0, 9999, out long _, null)
                         .FirstOrDefault();
                    if (publishedPage != null && publishedPage.Id != node.Id)
                    {
                        notification.CancelOperation(new EventMessage($"There can be only one {thereCanBeOnlyOne}",
                        $"Only one page of type {subSettingType.Name} can be in the site",
                        EventMessageType.Error));
                    }
                }
            }
        }
    }
    
    public class RestrictPageTypeComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.AddNotificationHandler<ContentSavingNotification, RestrictPageType>();
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft