Copied to clipboard

Flag this post as spam?

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


  • Terry Clancy 204 posts 944 karma points
    Sep 08, 2015 @ 07:14
    Terry Clancy
    0

    How can I programmatically delete / remove Umbraco Media ( Umbraco.Models.Core.IMedia)

    Hi all,

    I am trying to delete programmatically delete / remove Umbraco Media ( Umbraco.Models.Core.IMedia) but not sure how to do it. I have tried

    tTMedia.DisposeIfDisposable();
    

    as shown at // <<<<<<< in code below but that does not work. Other methods as shown in Visual Studio Intellisence do not seems appropriate ?

    Any suggestions would be appreciated.

    Thanks Terry Clancy ClanceZ

        public void TTDelMedia(TT_UCAdmin.Request_TTDelMedia request_TTDelMedia)
        {
            // Log Progress
            TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete TTCatalogMedia <br/>";
    
            if (ApplicationContext.Current != null)
            {
                // Instantiate the MediaService so we can do Media Operations
                var ms = ApplicationContext.Current.Services.MediaService;
                var rootMediaList = ms.GetRootMedia();
                int tTImagesFolderIndex = -1;
                tTImagesFolderIndex = rootMediaList.FindIndex(x => x.Name == "TTImages");
                if (tTImagesFolderIndex >= 0)  //  I think null list returns -1 
                {
                    int tTImagesFolderID = rootMediaList.ElementAt(tTImagesFolderIndex).Id;
                    var ChildMediaList = ms.GetChildren(tTImagesFolderID);
                    // int matchingImageIndex = -1;
                    // matchingImageIndex = ChildMediaList.FindIndex(x => x.Name == productImage_Thumb_FileName);
                    foreach (IMedia tTMedia in ChildMediaList)
                    {
                        TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete " + tTMedia.Name.ToString() + "<br/>";
                        tTMedia.DisposeIfDisposable();    // <<<<<<<
                    }
                }
            }
        }
    
  • Terry Clancy 204 posts 944 karma points
    Sep 08, 2015 @ 17:39
    Terry Clancy
    101

    Hi again,

    Never mind - I solved the problem - clearly I just needed a good nights sleep :-)

    The solution is to use the "Delete" method of my instance of the ApplicationContext.Current.Services.MediaService object. That line of code now flagged by // <<<<<<<<<< in my updated code below:

        // =================================================================================(80)
        // Delete TTCatalogMedia
        // =================================================================================(80)
        public void TTDelMedia(TT_UCAdmin.Request_TTDelMedia request_TTDelMedia)
        {
            // Log Progress
            TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete TTCatalogMedia <br/>";
    
            if (ApplicationContext.Current != null)
            {
                // Instantiate the MediaService so we can do Media Operations
                var ms = ApplicationContext.Current.Services.MediaService;
                var rootMediaList = ms.GetRootMedia();
                int tTImagesFolderIndex = -1;
                tTImagesFolderIndex = rootMediaList.FindIndex(x => x.Name == "TTImages");
                if (tTImagesFolderIndex >= 0)  //  I think null list returns -1 
                {
                    int tTImagesFolderID = rootMediaList.ElementAt(tTImagesFolderIndex).Id;
                    var ChildMediaList = ms.GetChildren(tTImagesFolderID);
                    foreach (IMedia tTMedia in ChildMediaList)
                    {
                        TT_UCAdminStatic.RequestStatusStatic.EventLogIn += "About to Delete " + tTMedia.Name.ToString() + "<br/>";
                        ms.Delete(tTMedia);    // <<<<<<<<<<
                    }
                }
            }
        }
    

    Thanks

    Terry Clancy

    ClanceZ

Please Sign in or register to post replies

Write your reply to:

Draft