Querying Child Libraries

Posted by Community Admin on 04-Aug-2018 17:32

Querying Child Libraries

All Replies

Posted by Community Admin on 11-Mar-2015 00:00

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?

Posted by Community Admin on 16-Mar-2015 00:00

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;
        
    

You may also refer to the following forum post where a similar matter has been discussed.

Regards,
Sabrie Nedzhip
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 16-Mar-2015 00:00

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?

Posted by Community Admin on 19-Mar-2015 00:00

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 you can see from the above sample code, you first get the root document library for example by title using the sample from our documentation article for querying document libraries. Then you get the child folder inside this root library (you may again filter by folder title). You may also checkout the following sample for more details.

After you get the folder, the you get the images inside this folder by calling the GetDescendants() method of the libraries manager and pass the folder as a parameter.

Regards,
Sabrie Nedzhip
Telerik
 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

Posted by Community Admin on 20-Mar-2015 00:00

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.

Posted by Community Admin on 25-Mar-2015 00:00

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

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed