Create folders inside libraries

NEW TO SITEFINITY?
  1. The following code creates a folder under a specified parent by parent

    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 librariesTo 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:

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?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?

Next article

Get folders