Media libraries
List of examples:
Dependency injection
Initialize required services
C#
// Initializes all services and provider classes used within
// the API examples on this page using dependency injection.
private readonly IInfoProvider<MediaLibraryInfo> mediaLibraryInfoProvider;
private readonly IInfoProvider<MediaFileInfo> mediaFileInfoProvider;
private readonly IMediaFileUrlRetriever mediaFileUrlRetriever;
public MediaLibrariesServices(IMediaFileUrlRetriever mediaFileUrlRetriever,
IInfoProvider<MediaFileInfo> mediaFileInfoProvider,
IInfoProvider<MediaLibraryInfo> mediaLibraryInfoProvider)
{
this.mediaFileInfoProvider = mediaFileInfoProvider;
this.mediaLibraryInfoProvider = mediaLibraryInfoProvider;
this.mediaFileUrlRetriever = mediaFileUrlRetriever;
}
Media libraries
Create media libraries
C#
// Creates a new media library object
MediaLibraryInfo newLibrary = new MediaLibraryInfo()
{
// Sets the library properties
LibraryDisplayName = "New library",
LibraryName = "NewLibrary",
LibraryDescription = "This media library was created through the API.",
LibraryFolder = "NewLibrary"
};
// Saves the new media library to the database
mediaLibraryInfoProvider.Set(newLibrary);
Update media libraries
C#
// Gets the media library
MediaLibraryInfo updateLibrary = mediaLibraryInfoProvider.Get("NewLibrary");
if (updateLibrary != null)
{
// Updates the library properties
updateLibrary.LibraryDisplayName = updateLibrary.LibraryDisplayName.ToLower();
// Saves the updated media library to the database
mediaLibraryInfoProvider.Set(updateLibrary);
}
Delete media libraries
C#
// Gets the media library
MediaLibraryInfo deleteLibrary = mediaLibraryInfoProvider.Get("NewLibrary");
if (deleteLibrary != null)
{
// Deletes the media library
mediaLibraryInfoProvider.Delete(deleteLibrary);
}
Media folders and files
Create a media library folder
C#
// Gets the media library
MediaLibraryInfo library = mediaLibraryInfoProvider.Get("NewLibrary");
if (library != null)
{
// Creates the "NewFolder" folder within the media library
MediaLibraryInfoProvider.CreateMediaLibraryFolder(library.LibraryID, "NewFolder");
}
Create a media library file
C#
// Gets the media library
MediaLibraryInfo library = mediaLibraryInfoProvider.Get("NewLibrary");
if (library != null)
{
// Prepares a path to a local file
string filePath = @"C:\Files\images\Image.png";
// 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);
// Sets the media library file properties
mediaFile.FileName = "Image";
mediaFile.FileTitle = "File title";
mediaFile.FileDescription = "This file was added through the API.";
mediaFile.FilePath = "NewFolder/Image/"; // Sets the path within the media library's folder structure
mediaFile.FileExtension = file.Extension;
mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(file.Extension);
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileSize = file.Length;
// Saves the media library file
mediaFileInfoProvider.Set(mediaFile);
}
}
Update a media library file
C#
// Gets the media library
MediaLibraryInfo library = mediaLibraryInfoProvider.Get("NewLibrary");
if (library != null)
{
// Gets the media file
MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, "NewFolder/Image.png");
if (updateFile != null)
{
// Updates the media library file properties
updateFile.FileDescription = updateFile.FileDescription.ToLower();
// Saves the media library file
mediaFileInfoProvider.Set(updateFile);
}
}
Update multiple media library files
C#
// Gets the media library
MediaLibraryInfo library = mediaLibraryInfoProvider.Get("NewLibrary");
if (library != null)
{
// Gets all .png files from the "NewFolder" folder of the specified media library
var mediaFiles = mediaFileInfoProvider.Get()
.WhereEquals("FileLibraryID", library.LibraryID)
.WhereEquals("FileExtension", ".png")
.WhereStartsWith("FilePath", "NewFolder");
// Loops through individual media library files
foreach (MediaFileInfo mediaFile in mediaFiles)
{
// Updates the media file properties
mediaFile.FileDescription = mediaFile.FileDescription.ToUpper();
// Saves the media library file
mediaFileInfoProvider.Set(mediaFile);
}
}
Delete a media library file
C#
// Gets the media library
MediaLibraryInfo library = mediaLibraryInfoProvider.Get("NewLibrary");
if (library != null)
{
// Gets the media file
MediaFileInfo deleteFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, "NewFolder/Image.png");
if (deleteFile != null)
{
// Deletes the media file
mediaFileInfoProvider.Delete(deleteFile);
}
}
Delete a media library folder
C#
// Gets the media library
MediaLibraryInfo library = mediaLibraryInfoProvider.Get("NewLibrary");
if (library != null)
{
// Deletes the "NewFolder" folder within the media library
MediaLibraryInfoProvider.DeleteMediaLibraryFolder(library.LibraryID, "NewFolder", false);
}
Retrieve media file URLs using IMediaFileUrlRetriever
C#
/*
* The IMediaFileUrlRetriever is a service class that provides URL resolution for media files
* in Xperience web applications (projects using the Kentico.Xperience.AspNetCore.WebApp NuGet package).
*/
// Gets an arbitrary media file object
MediaFileInfo mediaFile = mediaFileInfoProvider.Get(42);
// Gets an IMediaFileUrl object that contains the absolute and relative URLs to the given media file
IMediaFileUrl fileUrl = mediaFileUrlRetriever.Retrieve(mediaFile);
// Additionally, you can further parameterize retrieved URLs via extension methods on the IMediaFileUrl object
string fileUrlParameterized = mediaFileUrlRetriever.Retrieve(mediaFile)
// Scales the retrieved image to be no larger than 400px on either side
// while maintaining the aspect ratio
.WithSizeConstraint(SizeConstraint.MaxWidthOrHeight(400))
// Gets the relative path to the media file
.RelativePath;