Querying Child Libraries
I have created some document libraries with one library at the root, and the rest as children of that library.
I am now trying to query these child libraries by Guid using LibrariesManager:
LibrariesManager.GetManager().GetDocumentLibrary(LibraryID)
This returns the error "You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user." I have checked the database and the sf_libraries table only contains the root libraries.
However, if I change the Guid to point to a root library, the query works as it should.
Is this a bug in Sitefinity, or is there another way I should be querying child libraries?
Hi,
Please note that all the child libraries in the top level library are considered folders. So, what I can suggest is to take a look at the following documentation article for more details on how you can manage the folders using the folders API.
You may also refer to the section for querying folders and use the sample code for getting all folders withing a library by library id:
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;
Hi Sabrie,
Thanks for your reply. I did eventually realise they were folders and tried to use IFolder to get documents within the folder, but the query for folders seems to return Media objects rather than documents. Is there any way around this?
Hi Haroon,
Can you please try to use the sample code below in order to get the documents inside a folder inside the root document library:
var manager = LibrariesManager.GetManager();
var documentLibrary = manager.GetDocumentLibraries()
.Where(l => l.Title ==
"ParentLibraryTitle"
)
.FirstOrDefault();
var folder = manager.GetChildFolders(documentLibrary)
.Where(f => f.Title ==
"ChildFolderTitle"
)
.FirstOrDefault();
var documentsInsideFolder = manager.GetDescendants(folder)
.Where(d => d.Status == ContentLifecycleStatus.Live);
As I have the GUID of the folder I need I already tried using LibrariesManager.GetManager.GetFolder(folderID) to get an IFolder object, and then passed it to GetDescendants to get the document list, however the GetDescendants method returns IQueryable<MediaContent> rather than IQueryable<Document>. Is there an equivalent way of doing this for documents? Casting the result of GetDescendants to document objects did not work.
Hi Haroon,
I have just tested this on my side and after executing the code I get IQueryable collection of type Telerik.Sitefinity.Libraries.Model.Document. I am also sending attached a video demonstrating the results on my side.
Regards,
Sabrie Nedzhip
Telerik