Media library

Jayesh Patil asked on October 9, 2015 16:23

I written code that convert one images to four different images save to one media library my requirement is to copy one of image to other media library with code.

Recent Answers

Jan Hermann answered on October 12, 2015 10:15


The SetMediaFileInfo should do the job:

private bool CreateMediaFile()
    // Prepare the parameters
    string filePath = "~/CMSAPIExamples/Code/Tools/MediaLibrary/Files/Powered_by_kentico2.gif";

    // Get media library
    MediaLibraryInfo library = MediaLibraryInfoProvider.GetMediaLibraryInfo("MyNewLibrary", SiteContext.CurrentSiteName);
    if (library != null)
        // Create new media file object
        MediaFileInfo mediaFile = new MediaFileInfo(Server.MapPath(filePath), library.LibraryID);

        // Create file info
        FileInfo file = FileInfo.New(Server.MapPath(filePath));
        if (file != null)
            // Set the properties
            mediaFile.FileName = "MyNewFile";
            mediaFile.FileTitle = "My new file title";
            mediaFile.FileDescription = "My new file description.";
            mediaFile.FilePath = "MyNewFolder/MyNewFile.gif";
            mediaFile.FileExtension = file.Extension;
            mediaFile.FileMimeType = "image/gif";
            mediaFile.FileSiteID = SiteContext.CurrentSiteID;
            mediaFile.FileLibraryID = library.LibraryID;
            mediaFile.FileSize = file.Length;

            // Create the media file

            return true;

    return false;

Best Regards,
Jan Hermann

0 votesVote for this answer Mark as a Correct answer

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