Create folders inside libraries

  1. The following code creates a folder under a specified parent by parent ```C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Telerik.Sitefinity.Libraries.Model; using Telerik.Sitefinity.Modules.Libraries;

    namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Folders.ManagingFolders { public partial class FoldersSnippets { public static void CreateFolder() { //gets an instance of the LibrariesManager var manager = LibrariesManager.GetManager(); var title = "ImageAlbumTitle1";

             //creates an image album(library)
             var imagesAlbum = manager.CreateAlbum();
             imagesAlbum.Title = title;
    
             var url = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
    
             imagesAlbum.UrlName = url;
             imagesAlbum.ItemDefaultUrl = "/" + url;
    
             imagesAlbum.Urls.Add(new LibraryUrlData
             {
                 Id = Guid.NewGuid(),
                 Url = imagesAlbum.ItemDefaultUrl,
                 ApplicationName = manager.Provider.ApplicationName
             });
    
             manager.SaveChanges();
    
             //creates a folder under the album
             var folder = manager.CreateFolder(imagesAlbum);
             folder.Title = "FolderTitle";
             folder.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
             folder.UrlName = "FolderName";
    
             //always call the SaveChanges() method of the manager in order to commit the operation
             manager.SaveChanges();
         }
     }

    }

First, you get an instance of the LibrariesManager class. Then create an album. See here more info for For developers: Create image libraries. To create the album, you call the CreateAlbum method of the LibrariesManager class. 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. In our sample we are only setting the Title property. Finally, to save the album, you call the SaveChanges method of the manager. Then we create a folder using the CreateFolder method of the LibrariesManager by using the album created in the previous step as a parameter. Folders have the following properties: - Id- Title- Description- UrlName- ParentId
In our example we are not setting the Id, leaving it to be created automatically, set the Title, Description and UrlName. Finally you have to call the SaveChanges method of the manager in order to save the folder.
2. This example creates new folder under a specified parent by specifying folder id and parent: ```C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Telerik.Sitefinity.Libraries.Model; using Telerik.Sitefinity.Modules.Libraries;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Folders.ManagingFolders { public partial class FoldersSnippets { public static void CreateFolderById() { //gets an instance of the LibrariesManager var manager = LibrariesManager.GetManager(); var title = "ImageAlbumTitle1";

        //creates an image album(library)
        var imagesAlbum = manager.CreateAlbum();
        imagesAlbum.Title = title;

        var url = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

        imagesAlbum.UrlName = url;
        imagesAlbum.ItemDefaultUrl = "/" + url;

        imagesAlbum.Urls.Add(new LibraryUrlData
        {
            Id = Guid.NewGuid(),
            Url = imagesAlbum.ItemDefaultUrl,
            ApplicationName = manager.Provider.ApplicationName
        });

        manager.SaveChanges();

        //creates folder under the album wth a specified id
        var folderId = Guid.NewGuid();
        var folder = manager.CreateFolder(folderId, imagesAlbum);
        folder.Title = "FolderTitle";
        folder.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
        folder.UrlName = "FolderName";

        //always call the SaveChanges() method of the manager in order to commit the operation
        manager.SaveChanges();
    }
}

}

Here we are creating an album first similar to the first example and later we are creating  a folder by specifying id and album.
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.
New to Sitefinity?