Copied to clipboard

Flag this post as spam?

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


  • Sander Kinds 4 posts 46 karma points
    Oct 25, 2016 @ 14:26
    Sander Kinds
    0

    Preventing the deletion of media items

    Hi all,

    I'm trying to to prevent the deletion of media items by some CMS users. I want to do that by canceling the MediaService_Trashing event and show a warning message to the user.

    Is this the way to go and are there any examples on how to do this?

    Any help is appreciated :-)

    Sander

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 25, 2016 @ 19:24
    Dan Diplo
    2

    I'm guessing it would be something like this:

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        MediaService.Trashing += MediaService_Trashing;
    }
    
    private void MediaService_Trashing(IMediaService sender, MoveEventArgs<IMedia> e)
    {
        if (e.CanCancel)
        {
            e.CancelOperation(new EventMessage("Warning!", "You are not allowed to delete this media", EventMessageType.Error));
        }
    }
    

    Not tested, but that's the theory.

  • Sander Kinds 4 posts 46 karma points
    Oct 26, 2016 @ 06:49
    Sander Kinds
    0

    Hello Dan,

    Thanks for your reaction. Your code looks exactly like the thing I tried. :-) But it just doesn't seems to work. The delete gets canceled, but I don't see the message. I think its a bug. :-(

    Greets, Sander

  • Zac 223 posts 575 karma points
    Nov 16, 2016 @ 11:31
    Zac
    0

    Yep, I also have this bug in 7.5.4

  • Nils Frank 19 posts 89 karma points
    Mar 14, 2017 @ 15:10
    Nils Frank
    0

    Same problem here.

    I guess this is really an umbraco related bug. Not something i did.

    Programming rule 1:

    Always find someone to put the blame on!

    This error is shown in the browser console:

    Failed to load resource: the server responded with a status of 400 (Bad Request)
    
    http://localhost:55017/umbraco/backoffice/UmbracoApi/Media/DeleteById?id=1082 
    
  • Angus 34 posts 128 karma points
    Mar 14, 2017 @ 15:15
    Angus
    0

    This may be relevant...

    http://issues.umbraco.org/issue/U4-6872

    A

  • Nils Frank 19 posts 89 karma points
    Mar 14, 2017 @ 15:21
    Nils Frank
    0

    Hi Angus,

    but the issues says that it is implemented in the media service. So what am i missing?

    e.Cancel = true;
    e.Messages.Add(new EventMessage("Warning", "File cannot be delete! It is still in use.", EventMessageType.Warning));
    

    Best regards Nils

  • Angus 34 posts 128 karma points
    Mar 14, 2017 @ 15:52
    Angus
    0

    Hi Nils,

    I think it should read 'Currently only some Content and Media services...'. To date, I've found that messaging has only been enabled for some events. In particular, MediaService.Trashing and ContentService.UnPublishing don't generate messages. It's a real pain!!

    A

  • Nils Frank 19 posts 89 karma points
    Mar 15, 2017 @ 08:01
    Nils Frank
    0

    Hi Angus,

    Uhh thats annoying, but thanks anyway. Im gonna look for a work-around and post it here.

    Best regards Nils

  • trfletch 598 posts 604 karma points
    Mar 14, 2018 @ 17:19
    trfletch
    0

    Did you find a solution or workaround to this?

    I am trying to do the same thing, I need to prevent certain media types from being deleted (which works fine) but show a message to the user to let them know that they cannot delete the item.

  • Piotr Bach 15 posts 129 karma points c-trib
    Nov 11, 2021 @ 09:29
    Piotr Bach
    1

    Hi, check out Protect Used Media package. Best Piotr

Please Sign in or register to post replies

Write your reply to:

Draft