Create image libraries

When creating an album, you must perform the following steps:

  1. Check whether the album already exists.

    Before you create the album, you must check whether an album with the same ID already exists.

  2. Create the album.

    If the album does not exist, you create a new album with the specified ID.

  3. Set the required properties.

    When creating a new album, it is recommended to set at least the following properties:

    • Title
    • LastModified
    • DateCreated
    • UrlName

    You can also set any other properties in this step.

  4. Save the album.

    Save all changes that you have made to the album.

The example below shows you how to create an album with predefined ID.

Creating an album with predefined ID

The following code creates an album with the specified ID and Title. Native API ```C# using System; using System.Linq; using System.Text.RegularExpressions; using Telerik.Sitefinity.Libraries.Model; using Telerik.Sitefinity.Modules.Libraries;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Images.Albums { public partial class AlbumsSnippets { private void CreateAlbumNativeAPI(Guid albumId, string albumTitle) { LibrariesManager librariesManager = LibrariesManager.GetManager(); Album album = librariesManager.GetAlbums().Where(a => a.Id == albumId).FirstOrDefault();

        if (album == null)
        {
            //Create the album.
            album = librariesManager.CreateAlbum(albumId);

            //Set the properties of the album.
            album.Title = albumTitle;
            album.DateCreated = DateTime.UtcNow;
            album.LastModified = DateTime.UtcNow;
            album.UrlName = Regex.Replace(albumTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

            //Recompiles and validates the url of the album.
            librariesManager.RecompileAndValidateUrls(album);

            //Save the changes.
            librariesManager.SaveChanges();
        }
    }
}

}

First, you get an instance of the *LibrariesManager* class. You check whether an album with the same *ID* already exists. To create the album, you call the CreateAlbum method of the *LibrariesManager* class and pass the specified *ID*. You can create an album with either predefined or auto-generated *ID* depending on which overload of the method you use. Then, you set the properties of the album object. It is recommended to set at least the following properties: *Title*, *UrlName*, *LastModified*, *DateCreated*. Finally, to save the album, you call theSaveChanges method of the manager.
 **Fluent API**    ```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Images.Albums
{
    public partial class AlbumsSnippets
    {
        private void CreateAlbumFluentAPI(Guid albumId, string albumTitle)
        {
            //Check whether a album with the same ID already exists.
            var count = 0;
            App.WorkWith().Albums().Where(b => b.Id == albumId).Count(out count);

            if (count == 0)
            {
                //Create the album.
                App.WorkWith().Album().CreateNew(albumId)
                    //Set the properties of the album.
                .Do(b =>
                {
                    b.Title = albumTitle;
                    b.DateCreated = DateTime.UtcNow;
                    b.LastModified = DateTime.UtcNow;
                    b.UrlName = Regex.Replace(albumTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                })
                //Save the changes.
                .SaveChanges();
            }
        }
    }
}

First, you check whether an album with the same ID already exists. To create the album, you call the CreateNew method of the singular album facade. You can create an album with either predefined or auto-generated ID depending on which overload of the method you use. To set the properties of the album, you call the Do method of the facade. Finally, you save the changes.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.