Get folders
-
This example demonstrates how a folder can be retrieved by its id ```C# using Telerik.Sitefinity.Modules.Libraries;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Folders.ManagingFolders { public partial class FoldersSnippets { public static IFolder FindFolderById() { //gets an isntance of the LibrariesManager var manager = LibrariesManager.GetManager();
//creates an image album(library) var imagesAlbum = manager.CreateAlbum(); imagesAlbum.Title = "ImageAlbumTitle1"; manager.SaveChanges(); //creates folder 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(); //finding the folder by id var savedFolder = manager.FindFolderById(folder.Id); return savedFolder; } }}
Here we are using the **FindFolderById**method of the LibrariesManager by specifying folder id. If there is no folder with the specified id the method returns **null**. -
How to get the child folders of the specified parent folder: ```C# using System.Linq; using Telerik.Sitefinity.Modules.Libraries;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Folders.ManagingFolders { public partial class FoldersSnippets { public static IQueryable
GetChildFolders() { //gets an isntance of the LibrariesManager var manager = LibrariesManager.GetManager(); //creates an image album(library) var imagesAlbum = manager.CreateAlbum(); imagesAlbum.Title = "ImageAlbumTitle1"; manager.SaveChanges(); //creates folder under the image album var folder = manager.CreateFolder(imagesAlbum); folder.Title = "FolderTitle"; folder.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; folder.UrlName = "FolderName"; //creates folder under the root folder var childFolder1 = manager.CreateFolder(folder); childFolder1.Title = "ChildFolderTitle1"; childFolder1.UrlName = "ChildFolderTitle1"; childFolder1.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; //creates another folder under the root folder var childFolder2 = manager.CreateFolder(folder); childFolder2.Title = "ChildFolderTitle2"; childFolder2.UrlName = "ChildFolderTitle2"; childFolder2.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; //always call the SaveChanges() method of the manager in order to commit the operation manager.SaveChanges(); //gets all child folders under the root folder. They should be 2 var childFolders = manager.GetChildFolders(folder); return childFolders; } }}
In this example we are creating first a folder by **CreateFolder**method of the LibrariesManager. Then we are creating two more folders that will be under the first folder. This is done by specifying the parent folder in **CreateFolder**method (another overload). We have to call the **SaveChanges**method in order to save the three folders. Finally we can get all child folders by the **GetChildFolders**method of LibrariesManager again by specifying the parent folder. -
Getting the child items of a specified parent folder as IQueryable: ```C# using System.Linq; 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 IQueryable
GetChildItems() { var manager = LibrariesManager.GetManager(); var imagesAlbum = manager.CreateAlbum(); imagesAlbum.Title = "ImageAlbumTitle1"; manager.SaveChanges(); var folder = manager.CreateFolder(imagesAlbum); var folderId = folder.Id; folder.Title = "FolderTitle"; folder.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; folder.UrlName = "FolderName"; var childFolder1 = manager.CreateFolder(folder); childFolder1.Title = "ChildFolderTitle1"; childFolder1.UrlName = "ChildFolderTitle1"; childFolder1.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; var image = manager.CreateImage(); image.Title = "imageTitle"; image.Parent = imagesAlbum; image.FolderId = folder.Id; image = manager.CreateImage(); image.Title = "FolderTitle"; image.Parent = imagesAlbum; image.FolderId = childFolder1.Id; manager.SaveChanges(); var childItemsUnderFolder = manager.GetChildItems(folder); return childItemsUnderFolder; } }}
This example demonstrates how to get all items under a specific folder. First we are creating an image album (could be video or document library also) via the **CreateAlbum**method of the LibrariesManager. For more information go to [For developers: Create image libraries](slug://for-developers-create-image-libraries) article. Next we are creating a folder with a child folder. For that purpose we are using two of the overloads of the **CreateFolder**method of the LibrariesManager. For more information go to [For developers: Create folders inside libraries](slug://for-developers-create-folders-inside-libraries) article. Then we are creating two images. One is under the parent folder created previously and the other under the child folder. Notice the new FolderId property introduced which specifies the id of the folder under which the image is going to be stored. Do not forget to call SaveChanges method of the LibrariesManager in order to save the folders and images. For more information on creating images go to [For developers: Create images](slug://for-developers-create-images). Finally we are calling the **GetChildItems** method of the LibrariesManager by specifying the folder. -
Here’s an example of getting all folders in parent library:
C#using System; using System.Linq; using Telerik.Sitefinity.Modules.Libraries; namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Folders.ManagingFolders { public partial class FoldersSnippets { public static IQueryable<IFolder> GetAllFoldersInLibrary(Guid albumId) { //gets an isntance of the LibrariesManager var manager = LibrariesManager.GetManager(); //gets the images album var library = manager.GetAlbum(albumId); //creates folder under the album var folder = manager.CreateFolder(library); 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(); //gets all folders under the album var searchResult = manager.GetAllFolders(library); return searchResult; } } }In this example we are creating a folder uder an album for images. The you can use the GetAllFoldersmethod of the LibrariesManager by specifying the library as a parameter in order to find all folder under a specific library. ****
-
Here’s an example of getting all folders in parent folder: ```C# using System; using System.Linq; using Telerik.Sitefinity.Modules.Libraries;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Folders.ManagingFolders { public partial class FoldersSnippets { public static IQueryable
GetAllFoldersInFolder(Guid albumId) { //gets an isntance of the LibrariesManager var manager = LibrariesManager.GetManager(); //gets the images album var library = manager.GetAlbum(albumId); //creates folder under the album var folder = manager.CreateFolder(library); var folderId = folder.Id; 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(); //gets the folder by id folder = manager.GetFolder(folderId); //creates another folder under the other folder var subFolder = manager.CreateFolder(folder); var subFolderId = subFolder.Id; subFolder.Title = "FolderTitle"; subFolder.Description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; subFolder.UrlName = "FolderName"; //do not forget the SaveChanges again manager.SaveChanges(); //getting the sub-folder by id subFolder = manager.GetFolder(subFolderId); //getting all the folders under a parent folder (total - 1) var searchResult = manager.GetAllFolders(folder); return searchResult; } }}
This example is similar to number 4. However this time we have two folders with parent-child relationship within them. So the searchResult will return 2 folders in the end.