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.
This answer is a bit late but there's a possibility that someone may find this useful.
Make sure the root page is the only page that allows that type as a child
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>();
}
}
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.
anyone???
I usually create the page for the client then after disable the creation of the doc type.
Regards, Magnus
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.
Thanks guys was looking for a more elegant solution but sometimes the easy root will do it :-)
This answer is a bit late but there's a possibility that someone may find this useful.
Make sure the root page is the only page that allows that type as a child
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"
is working on a reply...