I'm looking for a way to lock certain pages for users. The homepage for instance sould not have a delete option and the document type can only exist once. The content however should be editable for the user.
Is there a way to manage these poperties?
Kind regards,
Frans
edit: I do know that I can prevent deletion per page per user but I am looking for some sort of a global setting for all users except admin for example
And here is a eventhandler to allow certain doctypes only once in your site
namespace Customer.Cms.Events
{
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
/// <summary>
/// Allow document types to be created once
/// </summary>
public class AllowOnce : ApplicationEventHandler
{
/// <summary>
/// Initializes a new instance of the <see cref="AllowOnce"/> class.
/// </summary>
public AllowOnce()
{
ContentService.Creating += this.ContentServiceCreating;
}
/// <summary>
/// Gets the content types that are allowed once under the parent
/// </summary>
private IEnumerable<string> ContentTypes
{
get
{
return new[]
{
"Homepage",
"Settings"
};
}
}
/// <summary>
/// The content service_ creating.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
private void ContentServiceCreating(IContentService sender, Umbraco.Core.Events.NewEventArgs<IContent> e)
{
if (this.ContentTypes.Contains(e.Entity.ContentType.Alias))
{
if (e.ParentId == -1 && e.Entity.ContentType.Alias == DocumentTypes.Homepage.Alias)
{
// only allow one homepage
e.Cancel = true;
}
else
{
IContent parent = sender.GetById(e.ParentId);
if (parent != null)
{
if (parent.Children().Any(x => x.ContentType.Alias == e.Entity.ContentType.Alias))
{
e.Cancel = true;
}
}
}
}
}
}
}
Prevent deletion and prevent double homepage
Hi,
I'm looking for a way to lock certain pages for users. The homepage for instance sould not have a delete option and the document type can only exist once. The content however should be editable for the user.
Is there a way to manage these poperties?
Kind regards,
Frans
edit: I do know that I can prevent deletion per page per user but I am looking for some sort of a global setting for all users except admin for example
I used a eventhandler for that. This prevents deleting and unpublishing for certain doctypes
namespace Customer.Cms.Events { using System.Collections.Generic; using System.Linq; using Umbraco.Core; using Umbraco.Core.Services; /// <summary> /// The prevent delete event handler /// </summary> public class PreventDelete : ApplicationEventHandler { /// <summary> /// Initializes a new instance of the <see cref="PreventDelete"/> class. /// </summary> public PreventDelete() { ContentService.Trashing += this.ContentServiceTrashing; ContentService.UnPublishing += this.ContentServiceUnPublishing; } /// <summary> /// Gets the content types to prevent deleting and unpublishing for /// </summary> private IEnumerable<string> ContentTypes { get { return new[] { "Homepage", "Settings" }; } } /// <summary> /// The content trashing event handler /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void ContentServiceTrashing(IContentService sender, Umbraco.Core.Events.MoveEventArgs<Umbraco.Core.Models.IContent> e) { if (this.ContentTypes.Contains(e.Entity.ContentType.Alias)) { e.Cancel = true; } } /// <summary> /// The content unpublishing event handler /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void ContentServiceUnPublishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e) { foreach (var item in e.PublishedEntities) { if (this.ContentTypes.Contains(item.ContentType.Alias)) { e.Cancel = true; break; } } } } }Dave
And here is a eventhandler to allow certain doctypes only once in your site
namespace Customer.Cms.Events { using System.Collections.Generic; using System.Linq; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Services; /// <summary> /// Allow document types to be created once /// </summary> public class AllowOnce : ApplicationEventHandler { /// <summary> /// Initializes a new instance of the <see cref="AllowOnce"/> class. /// </summary> public AllowOnce() { ContentService.Creating += this.ContentServiceCreating; } /// <summary> /// Gets the content types that are allowed once under the parent /// </summary> private IEnumerable<string> ContentTypes { get { return new[] { "Homepage", "Settings" }; } } /// <summary> /// The content service_ creating. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> private void ContentServiceCreating(IContentService sender, Umbraco.Core.Events.NewEventArgs<IContent> e) { if (this.ContentTypes.Contains(e.Entity.ContentType.Alias)) { if (e.ParentId == -1 && e.Entity.ContentType.Alias == DocumentTypes.Homepage.Alias) { // only allow one homepage e.Cancel = true; } else { IContent parent = sender.GetById(e.ParentId); if (parent != null) { if (parent.Children().Any(x => x.ContentType.Alias == e.Entity.ContentType.Alias)) { e.Cancel = true; } } } } } } }Hi Dawoe,
Thanks for the fast reply!
I'll try this solution in our next site.
Kind regards,
Frans
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.