Import media file

Naresh Ede asked on October 4, 2018 09:43


I'm doing media migration from my old site to new site in kentico.

So I'm directly copying the media files to kentico media library folder, but they are showing in media library with warning icon(not imported files).

I want to import the files which are not in media library with kentico 11 API, how can i achieve this?Image Text


Naresh Ede

Recent Answers

David te Kloese answered on October 4, 2018 10:11

The icon just means Kentico detected a file but has no Database record of it. If you hit the import button it will create a record and generate a GUID for it.

Is the old site also Kentico? Even still there probably are references to that images which you'll break.

So you probably need to create a import for the pages and make sure all links to media is correctly mapped.

Check out the api samples for some pointers.

if you just need a way of importing all files not in Kentico yet, you'll have to create a method that reads all folders and files and tries to map it to the records in the Database.

Have a look at CMSModules/MediaLibrary/Controls/MediaLibrary/MediaLibrary.ascx for a starting point.

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on October 4, 2018 12:21

Here is some code that I have used to import files into a MediaLibrary. As already suggested you would need to read the files on the disk so that you can create the Media Library file object.

Note: that the code below is a subset of a larger block that reads an old database and copies the files before creating the Media Library File Object.

// Prepares a CMS.IO.FileInfo object representing the local file
CMS.IO.FileInfo file = CMS.IO.FileInfo.New(filePath);

if (file != null && file.Exists)
    // the path to the image within the library
    string mediaPath = "Subfolder/Images/" + file.Name;

    // Creates a new media library file object
    MediaFileInfo mediaFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, mediaPath);
    if (mediaFile == null)
        mediaFile = new MediaFileInfo(filePath, library.LibraryID);

    // Sets the media library file properties
    mediaFile.FileName = Path.GetFileNameWithoutExtension(file.Name);
    mediaFile.FileTitle = ValidationHelper.GetString(row["images_caption"], "");
    mediaFile.FileDescription = ValidationHelper.GetString(row["images_description"], "");
    mediaFile.FilePath = "Subfolder/Images/"; 
    mediaFile.FileExtension = file.Extension;
    mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(file.Extension);
    mediaFile.FileSiteID = 1;
    mediaFile.FileLibraryID = library.LibraryID;
    mediaFile.FileSize = file.Length;

    MediaFileInfoProvider.SetMediaFileInfo(mediaFile, false, userId);
0 votesVote for this answer Mark as a Correct answer

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