Replace a media library file through API

Srikrishna Podduturi asked on November 28, 2019 13:39

Hi Team,

I am synchronizing images to Kentico media library through API. I have to update/replace modified images in media library. I am updating a media library file through API but it is not replacing image in the media library folder. Could you please advise how would i achieve this using API?

Thanks, Krishna.

Recent Answers


David te Kloese answered on November 28, 2019 13:59

Check the example on creating a Media library file in the API examples:

docs.kentico.com/api12/.../Medialibraries-Creatingamedialibraryfile

Depending on where the new file is, but basically:

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

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

Combine that with the retrieved MediaFile you have to store the updated file.

0 votesVote for this answer Mark as a Correct answer

Srikrishna Podduturi answered on November 29, 2019 08:15 (last edited on November 29, 2019 08:21)

Hi David,

Thanks for the information. I have already tried to update a media file by using API but it does not replace image in physical location.

I need the update functionality of media libraries in Kentico admin through API. Is that possible or do i have replace the image and them modify file properties through Kentico API.

Thanks, Krishna.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on November 29, 2019 11:24

Can you share the code you're using?

Can you upload new files, or is there perhaps a file writing permission issue?

Does updating an image using the interface work?

0 votesVote for this answer Mark as a Correct answer

Srikrishna Podduturi answered on December 2, 2019 11:09 (last edited on December 2, 2019 11:10)

Hi David,

Thanks for your reply. Yes, I can upload new files and also updating an image is working using the admin panel. Please find my code below. In IF condition i am creating a new file in media library which is working fine and in ELSE condition i have to replace the file to existing media library file.

if (!File.Exists(Path.Combine(mediaDocsPath, libraryInfo.LibraryFolder, validDocsPath)))
{
    MediaFileInfo mediaFile = new MediaFileInfo(path, libraryInfo.LibraryID);
    mediaFile.FileTitle = "Document Title";
    mediaFile.FileDescription = "Document Description";
    //Replacing local docs path with empty to get path of file to be saved in media library
    mediaFile.FilePath = docsPath.Replace(@"\", @"/");

    MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
}
else
{
    MediaFileInfo updateMediaFile = MediaFileInfoProvider.GetMediaFileInfo(
    libraryInfo.LibraryID,docsPath);
    if (updateMediaFile != null)
    {
        MediaFileInfoProvider.SetMediaFileInfo(updateMediaFile);
    }
}
0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on December 2, 2019 11:15

I think you should update the create media info section:

MediaFileInfo updateMediaFile = MediaFileInfoProvider.GetMediaFileInfo(
    libraryInfo.LibraryID,docsPath);

With something like:

MediaFileInfo updateMediaFile = new MediaFileInfo(docsPath, libraryInfo.LibraryID);

I believe yours will try to get the existing media item instead or creating a new one.

0 votesVote for this answer Mark as a Correct answer

Srikrishna Podduturi answered on December 2, 2019 11:41 (last edited on December 2, 2019 11:43)

This is creating a new record in the Media_File table and instead of replacing the existing file it is creating a new file with "_1".

I need to update the existing record in Media_File table and replace the existing file in media library folder.

0 votesVote for this answer Mark as a Correct answer

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