Copied to clipboard

Flag this post as spam?

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


  • Hefin Jones 39 posts 161 karma points
    Oct 19, 2016 @ 14:11
    Hefin Jones
    0

    Prevent Deleting / Unpublish and display notification to editor

    Hello

    I'm currently trying to prevent deletion and unpublishing of certain pages and media. I've got the following code:

    private void ContentService_UnPublishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
        foreach (var item in e.PublishedEntities)
        {
            if (this.IDsNotToBeDeleted.Contains(item.Id.ToString()))
            {
                e.Cancel = true;
                e.Messages.Add(CreateWarningMessage(item.ContentType.Name));
                break;
            }
        }
    }
    
    private void ContentService_Trashing(IContentService sender, MoveEventArgs<IContent> e)
    {
        foreach (var moveInfoItem in e.MoveInfoCollection)
        {
            if (this.IDsNotToBeDeleted.Contains(moveInfoItem.Entity.Id.ToString()))
            {
                e.Cancel = true;
                e.Messages.Add(CreateWarningMessage(moveInfoItem.Entity.Name));
                break;
            }
        }
    }
    
    private static EventMessage CreateWarningMessage(string name)
    {
        var eventMessage = new EventMessage("Warning", string.Format("Can not delete {0}", name), EventMessageType.Error);
        return eventMessage;
    }
    

    The message appears for the Trashing event (content), but doesn't work for Unpublishing event (content), the Unpublish button just hangs with the spinner spinning, and in the console I get the following error:

    Failed to load resource: the server responded with a status of 400 (Bad Request) - http://localhost:888/umbraco/backoffice/UmbracoApi/Content/PostUnPublish?id=1066
    

    With Media Trashing event, I get a similar problem - no message is displayed and an error appears in the console:

    POST http://localhost:888/umbraco/backoffice/UmbracoApi/Media/DeleteById?id=1129 400 (Bad Request)
    

    Any information on how to get these working would be great :-)

    Cheers!

Please Sign in or register to post replies

Write your reply to:

Draft