I am using Kentico CMS custom tables.

Unal UN asked on November 22, 2017 08:31

I am using Kentico CMS custom tables. I can upload images and save them to the official media library, but the image is not deleted when I unload the database via CMS ?

Help Me !

Recent Answers


Vukasin Andjelic answered on November 22, 2017 10:23

This is because you upload images directly to media library, and in custom table you only keep url to that image. You need to delete this images manually or to write event handler for the custom table.

0 votesVote for this answer Mark as a Correct answer

Unal UN answered on November 22, 2017 10:57

How can I do it write event handler for the custom table ?

where is the sample ? Thank you.

0 votesVote for this answer Mark as a Correct answer

Vukasin Andjelic answered on November 22, 2017 11:09

Custom table event handler here is what i found

0 votesVote for this answer Mark as a Correct answer

Unal UN answered on November 22, 2017 11:14

Thank you Vukasin Andjelic but I still do not understand how I can apply it :(

0 votesVote for this answer Mark as a Correct answer

Unal UN answered on November 22, 2017 11:14

Thank you Vukasin Andjelic but I still do not understand how I can apply it :(

0 votesVote for this answer Mark as a Correct answer

Vukasin Andjelic answered on November 22, 2017 11:29 (last edited on November 22, 2017 11:32)

This is only part solution, because You need to find how you will take files from mediaa library by its url, and then delete them. You can find this in

Media library solution

using CMS.Base;
using CMS.CustomTables;
using CMS.DataEngine;
using CMS.DocumentEngine;

[CustomTableEvents]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the loading of custom handlers.
    /// </summary>
    private class CustomTableEventsAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// The system executes the Init method of the CMSModuleLoader attributes when the application starts.
        /// </summary>
        public override void Init()
        {
            // Assigns custom handlers to events
            ObjectEvents.Delete.Before+= Delete_Before;
        }

        private void Delete_Before(object sender, ObjectEventArgs e)
        {
            switch (e.Object.TypeInfo.ObjectType)
            {
                case "customtableitem.yournamespace.yourclassname":
                    /* do your work here, you need to take media files from media library which are
                        in your custom table and to delete the files from media library, before you     
                        delete the custom table. 
                      */                 
                    break;
                default:
                    break;
            }
        }
    }
}
1 votesVote for this answer Mark as a Correct answer

Unal UN answered on November 23, 2017 08:43

// Thanks Sample Code [CustomTableEvents] public partial class CMSModuleLoader : CMSModuleLoaderBase { /// public CMSModuleLoader(): base("CMSModuleLoader"){}

    private class CustomTableEventsAttribute : CMSLoaderAttribute
    {
        public override void Init()
        {

            CustomTableItemEvents.Delete.Before += CustomTableItem_Before_Delete;

        }

        //customer table
        private void CustomTableItem_Before_Delete(object sender, CustomTableItemEventArgs e)
        {
            string ResimFullPath = e.Item.GetValue("Image").ToString();

            string[] PathSection = ResimFullPath.Split('/');
            string ImageName = PathSection[6].ToString();

            switch (e.Item.TypeInfo.ObjectType)
            {
                case "customtableitem.customtable.myadmindocument":

                    MediaLibraryInfo library = MediaLibraryInfoProvider.GetMediaLibraryInfo("mymedia", SiteContext.CurrentSiteName);

                    if (library != null)
                    {

                        // Gets the media file
                        MediaFileInfo deleteFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID,ImageName);

                        if (deleteFile != null)
                        {
                            // Deletes the media file
                            MediaFileInfoProvider.DeleteMediaFileInfo(deleteFile);
                        }
                    }

                    break;
                default:
                    break;
            }
        }
    }
0 votesVote for this answer Mark as a Correct answer

Unal UN answered on November 23, 2017 08:51 (last edited on November 23, 2017 08:53)

Hello,

How can I do this in a subfolder directory that I want to do?

I want to access the product folder under the mymedia folder you want to do ?

Example mymedia/product/myimage.jpg => I want to delete this picture Image Text

0 votesVote for this answer Mark as a Correct answer

Vukasin Andjelic answered on November 23, 2017 09:31

MediaFileInfoProvider.GetMediaFiles().Where("FilePath", "mymedia/product/"); with this u can get all media files under the product subfolder and then u can iterate and delete files that you want.

0 votesVote for this answer Mark as a Correct answer

Unal UN answered on November 23, 2017 11:20 (last edited on November 23, 2017 11:21)

Hi Vukasin,

I am not understand.



What is this FilePath ?

My File Directory Path => mymedia/product/

My Code sample

MediaFileInfo deleteFile = MediaFileInfoProvider.GetMediaFiles("mymedia/Product/");

 if (deleteFile != null)

  {
      if (deleteFile.FileName == ImageName)
      {
       // Deletes the media file
       MediaFileInfoProvider.DeleteMediaFileInfo(deleteFile);
      }

  }
0 votesVote for this answer Mark as a Correct answer

Vukasin Andjelic answered on November 26, 2017 21:37

FilePath is column name in database, and it is the path for file in media library. So if u taking a file with file path mymedia/Product/ you need to write the query like in SQL.

   MediaFileInfo deleteFiles = MediaFileInfoProvider.GetMediaFiles().WhereLike("FilePath", "mymedia/Product/%");
   if (deleteFiles != null)
  {
      foreach(var deleteFile in deleteFiles)
      {
          if (deleteFile.FileName == ImageName)
          {
           // Deletes the media file
           MediaFileInfoProvider.DeleteMediaFileInfo(deleteFile);
          }
      }
  }
1 votesVote for this answer Mark as a Correct answer

Unal UN answered on November 27, 2017 08:40

Thank you Vukasin Andjelic

Good Work / Best Regards

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.