Media libraries


List of examples:

Media libraries

Creating a media library



// Creates a new media library object
MediaLibraryInfo newLibrary = new MediaLibraryInfo();

// Sets the library properties
newLibrary.LibraryDisplayName = "New library";
newLibrary.LibraryName = "NewLibrary";
newLibrary.LibraryDescription = "This media library was created through the API.";
newLibrary.LibraryFolder = "NewLibrary";
newLibrary.LibrarySiteID = SiteContext.CurrentSiteID;

// Saves the new media library to the database
MediaLibraryInfo.Provider.Set(newLibrary);

> Back to list of examples

Updating a media library



// Gets the media library
MediaLibraryInfo updateLibrary = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

if (updateLibrary != null)
{
    // Updates the library properties
    updateLibrary.LibraryDisplayName = updateLibrary.LibraryDisplayName.ToLower();

    // Saves the updated media library to the database
    MediaLibraryInfo.Provider.Set(updateLibrary);
}

> Back to list of examples

Updating multiple media libraries



// Gets all media libraries on the current site whose code name starts with 'New'
var libraries = MediaLibraryInfo.Provider.Get()
                                            .WhereEquals("LibrarySiteID", SiteContext.CurrentSiteID)
                                            .WhereStartsWith("LibraryName", "New");


// Loops through individual media libraries
foreach (MediaLibraryInfo library in libraries)
{
    // Updates the library properties
    library.LibraryDisplayName = library.LibraryDisplayName.ToUpper();

    // Saves the updated media library to the database
    MediaLibraryInfo.Provider.Set(library);
}

> Back to list of examples

Deleting a media library



// Gets the media library
MediaLibraryInfo deleteLibrary = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

if (deleteLibrary != null)
{
    // Deletes the media library
    MediaLibraryInfo.Provider.Delete(deleteLibrary);
}

> Back to list of examples

Media folders and files

Creating a media library folder



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

if (library != null)
{
    // Creates the "NewFolder" folder within the media library
    MediaLibraryInfoProvider.CreateMediaLibraryFolder(SiteContext.CurrentSiteName, library.LibraryID, "NewFolder");
}

> Back to list of examples

Creating a media library file



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

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.FileSiteID = SiteContext.CurrentSiteID;
        mediaFile.FileLibraryID = library.LibraryID;
        mediaFile.FileSize = file.Length;

        // Saves the media library file
        MediaFileInfo.Provider.Set(mediaFile);
    }
}

> Back to list of examples

Updating a media library file



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

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
        MediaFileInfo.Provider.Set(updateFile);
    }
}

> Back to list of examples

Updating multiple media library files



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

if (library != null)
{
    // Gets all .png files from the "NewFolder" folder of the specified media library
    var mediaFiles = MediaFileInfo.Provider.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
        MediaFileInfo.Provider.Set(mediaFile);
    }
}

> Back to list of examples

Deleting a media library file



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

if (library != null)
{
    // Gets the media file
    MediaFileInfo deleteFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, "NewFolder/Image.png");

    if (deleteFile != null)
    {
        // Deletes the media file
        MediaFileInfo.Provider.Delete(deleteFile);
    }
}

> Back to list of examples

Deleting a media library folder



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

if (library != null)
{
    // Deletes the "NewFolder" folder within the media library
    MediaLibraryInfoProvider.DeleteMediaLibraryFolder(SiteContext.CurrentSiteName, library.LibraryID, "NewFolder", false);
}

> Back to list of examples

Media library security

Setting the security options for a media library



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

if (library != null)
{
    // Allows the content of the media library to be viewable for all users                                    
    library.Access = SecurityAccessEnum.AllUsers;

    // Allows management of the media library's files and folders only for assigned roles
    library.FileCreate = SecurityAccessEnum.AuthorizedRoles;
    library.FolderCreate = SecurityAccessEnum.AuthorizedRoles;
    library.FolderModify = SecurityAccessEnum.AuthorizedRoles;
    library.FileDelete = SecurityAccessEnum.AuthorizedRoles;
    library.FolderDelete = SecurityAccessEnum.AuthorizedRoles;
    library.FileModify = SecurityAccessEnum.AuthorizedRoles;

    // Saves the updated media library to the database
    MediaLibraryInfo.Provider.Set(library);
}

> Back to list of examples

Allowing a media library action for a role



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

// Gets the role
RoleInfo libraryRole = RoleInfo.Provider.Get("Admin", SiteContext.CurrentSiteID);

// Gets the "Create file" media library permission
PermissionNameInfo libraryPermission = PermissionNameInfoProvider.GetPermissionNameInfo("FileCreate", "CMS.MediaLibrary", null);

// Checks that all of the objects exist
if ((library != null) && (libraryRole != null) && (libraryPermission != null))
{
    // Creates an object representing a relationship between the media library permission and the role
    MediaLibraryRolePermissionInfo rolePermission = new MediaLibraryRolePermissionInfo();
    rolePermission.LibraryID = library.LibraryID;
    rolePermission.RoleID = libraryRole.RoleID;
    rolePermission.PermissionID = libraryPermission.PermissionId;

    // Assigns the role to the media library permission
    // In this case, members the role are authorized to create files in the media library
    MediaLibraryRolePermissionInfo.Provider.Set(rolePermission);
}

> Back to list of examples

Removing a role’s authorization for a media library action



// Gets the media library
MediaLibraryInfo library = MediaLibraryInfo.Provider.Get("NewLibrary", SiteContext.CurrentSiteID);

// Gets the role
RoleInfo libraryRole = RoleInfo.Provider.Get("Admin", SiteContext.CurrentSiteID);

// Gets the "Create file" media library permission
PermissionNameInfo libraryPermission = PermissionNameInfoProvider.GetPermissionNameInfo("FileCreate", "CMS.MediaLibrary", null);

// Checks that all of the objects exist
if ((library != null) && (libraryRole != null) && (libraryPermission != null))
{
    // Gets the object representing the relationship between the media library permission and the role
    MediaLibraryRolePermissionInfo rolePermission = 
        MediaLibraryRolePermissionInfo.Provider.Get(library.LibraryID, libraryRole.RoleID, libraryPermission.PermissionId);

    if (rolePermission != null)
    {
        // Removes the role from the media library permission
        MediaLibraryRolePermissionInfo.Provider.Delete(rolePermission);
    }
}

> Back to list of examples

Retrieving media file URLs using IMediaFileUrlRetriever



/* 
 * The IMediaFileUrlRetriever is a service class that provides URL resolution for media files
 * in Xperience web applications (projects using the Kentico.Xperience.AspNet.Mvc5 or 
 * Kentico.Xperience.AspNetCore.WebApp NuGet packages).
 */

// Initializes an instance of a service used for media file URL retrieval
IMediaFileUrlRetriever urlRetriever = Service.Resolve<IMediaFileUrlRetriever>();

// Gets an arbitrary media file object
MediaFileInfo mediaFile = MediaFileInfo.Provider.Get(42);

// Gets an IMediaFileUrl object that contains the absolute and relative URLs to the given media file
IMediaFileUrl fileUrl = urlRetriever.Retrieve(mediaFile);

// Additionally, you can further parameterize retrieved URLs via extension methods on the IMediaFileUrl object
string fileUrlParameterized = urlRetriever.Retrieve(mediaFile)
                                 // Scales the retrieved image to be no larger than 400px on either side, 
                                 // maintaining aspect ratio
                                .WithSizeConstraint(SizeConstraint.MaxWidthOrHeight(400))
                                 // Gets the relative path to the media file
                                .RelativePath;

string fileUrlParameterized2 = urlRetriever.Retrieve(mediaFile)
                                // Sets the Content disposition of the URL to 'attachment.' 
                                // Accessing the link results in an immediate download of the media file. 
                                .WithOptions(new FileUrlOptions { AttachmentContentDisposition = true })
                                // Gets the relative path to the media file
                                .RelativePath;

> Back to list of examples