Copied to clipboard

Flag this post as spam?

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


  • Amalie Wowern 144 posts 273 karma points c-trib
    Aug 08, 2014 @ 13:08
    Amalie Wowern
    0

    MediaService save event

    Hi

    I'm trying to set up some events for a specific mediatype.

    In the end it's suppose to Create a label with a version number, stop the user for changing the document and stop a delete event

    But first up

    I trying to create the version number I have created a MediaService.Created event where i'm setting the version number, and that works a attended. But when a save the media item it deletes the version number Then i tryid to setup mediaservice.saving and saved event. Where it checks if the label is empty and otherwise put the version number in This works but, not in the first save. So it works if i save a second time, and i want to save it the first time.

    Heres the code`public DocumentlibraryEvents() { MediaService.Saving += MediaServiceSaving; MediaService.Deleting += MediaServiceDeleting; MediaService.Created += MediaServiceCreated; MediaService.Saved += MediaServiceSaved; }

        void MediaService_Saved(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<IMedia> e)
        {
    
            foreach (var mediaItem in e.SavedEntities)
            {
                if (mediaItem.ContentType.Alias == "DokumentarkivVersion")
                {
                    foreach (var property in mediaItem.Properties.Where(property => property.Alias == "version"))
                    {
                        if (property.Value == "")
                        {
                            mediaItem.SetValue("version", GetVersion(mediaItem));
                            sender.Save(mediaItem);
                        }
                    }
                }
            }
        }
    
        void MediaService_Created(IMediaService sender, Umbraco.Core.Events.NewEventArgs<IMedia> e)
        {
            var mediaItem = e.Entity;
            if (mediaItem.ContentType.Alias == "DokumentarkivVersion")
            {
                mediaItem.SetValue("version", GetVersion(mediaItem) + 1);
    
                mediaItem.Name = Convert.ToString(GetVersion(mediaItem) + 1);
    
            }
    
    
        }
    
        void MediaService_Deleting(IMediaService sender, Umbraco.Core.Events.DeleteEventArgs<IMedia> e)
        {
            e.Cancel = true;
        }
    
        void MediaService_Saving(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
        {
        }
    
        private int GetVersion(IMedia mediaItem)
        {
            return mediaItem.Parent().Children().Count();
        }`
    

    Also i want to know how to cancel i Delete event.

    I'm using Umbraco 7.1.4

  • Amalie Wowern 144 posts 273 karma points c-trib
    Aug 08, 2014 @ 13:14
    Amalie Wowern
    0

    Heres a small video to illustrate what happens http://screencast.com/t/ZaAuvnY4o4pi

  • Amalie Wowern 144 posts 273 karma points c-trib
    Aug 08, 2014 @ 14:02
    Amalie Wowern
    0

    I figured out to cancel the delete event

    MediaService.Trashing += MediaService_Trashing;
    
        void MediaService_Trashing(IMediaService sender, Umbraco.Core.Events.MoveEventArgs<IMedia> e)
        {
            e.Cancel = true;
        }
    

    But is there a way to stop the delete animation

    See video http://screencast.com/t/uC0hNrRMzi

  • Amalie Wowern 144 posts 273 karma points c-trib
    Aug 11, 2014 @ 08:13
    Amalie Wowern
    0

    I managed to find a solution with the version number I used MediaServiceSaving instead of MediaServiceSaved, And then i could i use IsNewEntity

    Is it possible to compare the new and the old values in the saving/saved event. I want to stop the user from removing og replace a file in fileupload?

    Can still not figure out how to stop the delete animation when i canceling the delete event

  • Amalie Wowern 144 posts 273 karma points c-trib
    Aug 11, 2014 @ 11:43
    Amalie Wowern
    0
    void MediaService_Saved(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<IMedia> e)
            {
                foreach (var mediaItem in e.SavedEntities)
                {
                    if (mediaItem.IsNewEntity())
                    {
                        mediaItem.SetValue("version", GetVersion(mediaItem));
                    }
                    UmbracoContext.Current.Application.Services.MediaService.Save(mediaItem, 0, false);
    
                }
            }
    
  • Jason D 62 posts 194 karma points
    Aug 18, 2020 @ 16:40
    Jason D
    0

    I know this is an old thread now, but I ran into an issue that I'm trying to figure out, which I think is related.

    I am hooked into the Publish event, which triggers a function for saving a PDF file into the Media Library, in which I have it checking for the file existing, and then I save the same object as new. But I seem to have to Publish twice, essentially running the MediaService_Saving event twice, to get the page to show the latest version of the saved file. It works, but only if I do this (publish twice).

    I'm assuming I'm missing updating a version number maybe? Not sure.

Please Sign in or register to post replies

Write your reply to:

Draft