Bulk import of media libraries

Kelly Leavitt asked on May 22, 2023 20:35

Several times per year we have a bulk update to parts of our media libraries.

In Kentico 11 we could just transfer the new files. Kentico realized they were new and we just re-imported them.

This does not happen in Kentico 13. It is very time consuming to manually upload 100+ files in multiple directories using the media library interface.

I've tried just "uploading" them in bulk, but it creates a new version of the file (e.g. part261.pdf still exists and a new part261_1.pdf is created.

Any thoughts on simplifying this?

Recent Answers

Trevor Fayas answered on June 6, 2023 18:19

Scripting it out would be the best way to go about it. If the file exists though and you use the API to create the MediaInfo and the item exists in the same spot, it will upload and duplicate it.

You should push them to a different folder, then use the media API to create the new media file sourcing from the different folder, that way it won't get the _1, it's annoying i know...

If you need a sample:

// Create a temporary upload path
var uploadPath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/").Trim('/') + "/App_Data/Images_Temp";
            if (!Directory.Exists(movePath))
                CMS.IO.DirectoryHelper.EnsureDiskPath(movePath.Replace("/", "\\"), AppDomain.CurrentDomain.BaseDirectory);
                CMS.IO.DirectoryHelper.CreateDirectory(movePath.Replace("/", "\\"));
            if (!Directory.Exists(movePath))
                throw new Exception("Could not create temp directory, ensure your app pool has proper permissions.");

// Download file there, you can skip this if you are copying the files to a folder instead
using (var client = new WebClient())
                    client.DownloadFile(MediaUrl, uploadPath);
                catch (WebException wex)
                    if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
                        SetMediaDictionaryItem(wPMediaItem, oldUrlForLookup, NOT_FOUND_IMAGE_URL);

            var newFile = new MediaFileInfo(uploadPath, theMediaLibraryID);

            // Update path and everything to be lowercase and no spaces
            newFile.FileName = filePart; // File name without extension, ex "HelloWorld"
            newFile.FilePath = pathAndFile.Trim('/'); // Path + File name and extension, exclude the media library folder, ex "/Some-Folder/HelloWorld.jpg"

            newFile.FileExtension = ".jpg"; // File extension with period
            newFile.FileTitle = mediaTitle.Length > 250 ? mediaTitle.SubString(0, 250) : mediaTitle;
                newFile.FileDescription = alt;

                // insert the new one
                var newUrl = $"/getmedia/{newFile.FileGUID}/{newFile.FileName}{newFile.FileExtension}".ToLower();
0 votesVote for this answer Mark as a Correct answer

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