Try our conversational search powered by Generative AI!

How to get all assets from Asset library

Vote:
 

Hi,

I would like to get all the Assets from the Asset Library in Episerver 8 where a user has tagged them using the Category filter.

Is this possible?

I have seen posts where a user is using the following code (currently the code below was intending to bring back everything - but I do need to use the category filter once I get this bit working)

 public override ActionResult Index(AssetBlock currentBlock)
        {
            var model = new AssetBlockModel();
            var contentRepository = ServiceLocator.Current.GetInstance();
            var RootAssets = contentRepository.GetChildren(SiteDefinition.Current.GlobalAssetsRoot);
            model.Files = RootAssets.Take(20);
            
            return PartialView(model);
        }

But it is bringing back zero results.

Am I using the wrong code?

Jon

#144641
Feb 16, 2016 18:07
Vote:
 

Use either Get descendents or recursive get children like

http://world.episerver.com/forum/developer-forum/-EPiServer-75-CMS/Thread-Container/2014/6/Does-GetDescendents-hit-the-cache/

FindPagesByCriteria is also possible. 

All of these take pretty much memory and cpu so careful if you are displaying then for end users.

#144644
Feb 16, 2016 19:22
Vote:
 

For categories, just check the Category property on page which is a collection of ids basically. Match against the one you want..

http://world.episerver.com/documentation/Class-library/?documentId=cms/9/56D12925

#144646
Feb 16, 2016 20:26
Vote:
 

And use FilterForVisitor to get rid of unpublished pages and access rights...

#144661
Feb 17, 2016 8:21
Vote:
 

Hi, the sample supplied looks for pages I would like to get all the Media assets from the Assets folder(s)

Jon

#144673
Feb 17, 2016 10:59
Vote:
 

Same API after 7.5 when assets were migrated from old VPP and unified file system to being IContent...

So you can traverse the media files using getchildren etc...

#144674
Feb 17, 2016 11:15
Vote:
 

Hi,

I have folders inside folders - is this something that this can do. Has this been done before regarding Media Assets? I cant seem to get it to loop the recursive folders.

Thanks

Jon

#144676
Feb 17, 2016 11:42
Vote:
 

Yup that's the idea. GetChildren only takes the first level. GetDescendants get everything below not matter how many folders you have nested. A recursive GetChildren will do that as well. 

Might be that you are filtering on type that the folder doesn't implement. Try shifting type in call to IContent...everything inherits from that one. Folder is not MediaData...so it will filter out those if you use the above code...

#144679
Feb 17, 2016 12:17
Vote:
 

I got the GetDecendants bit working but there doesnt seem to be a way to filter out the items on Category - for example if an item is tagged with multiple Categories etc.

Jon

#144685
Feb 17, 2016 12:50
Vote:
 

You can cast those content items to ICategorizable if they have support for containing categories. If they do, use the property above to get the list of categories and filter away :)

Hope that helps...

#144686
Feb 17, 2016 12:59
Vote:
 

I see :)

I am currently using this:

var list = contentRepository.GetDescendents(SiteDefinition.Current.GlobalAssetsRoot)
                               .Select(contentRepository.Get<IContent>)
                               .ToList();

Which is great as it is bringing back all the assests.

If I do a loop of list:

foreach (var dd in list)
                {
                    ICategorizable content = contentRepository.Get<IContent>(dd.ContentLink) as ICategorizable;

                }

I can get the Categories which is ok. But what I would ideally like to do is filter the categories in my first call where Im creating "list" as I am currently creating a front end search and there could be 500 or more items.

I hope thsi makes sense,

jon

#144689
Feb 17, 2016 13:18
Vote:
 

.something like 

...

.Select(contentRepository.Get<IContent>)
.Where(c=>c as ICategorizable !=null)
.Where(c=>((ICategorizable)c).Category.Any(cat=>cat.Id ==categoryId))
.ToList()

#144690
Feb 17, 2016 13:24
Vote:
 

.OfType is a nice extension too which will simplify somewhat the above

#144695
Feb 17, 2016 14:13
Vote:
 

Hi,

How do you get all the categories from Admin > Config > Edit Categories? I have a lot of categories or which are children of children.

I am currently using:

 var categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
                Category rootCategory = categoryRepository.GetRoot();

                CategoryCollection childCategories = rootCategory.Categories;

but this is only bringing back the top categories.

Thanks again for all your help

Jon

#144697
Feb 17, 2016 14:35
Vote:
 

Apologies - I can use:

 var categoryRepository = ServiceLocator.Current.GetInstance<CategoryRepository>();
                Category rootCategory = categoryRepository.GetRoot();

                var childCategories = rootCategory.GetList();

Thanks

Jon

#144698
Feb 17, 2016 14:49
Vote:
 

Last question - I promise :)

THe code above is going into a Block which will filter out assets in relation to the page they are on. For example a page can have a categoryList and I need to get all the assets from the asset library that have been tagged with the same tags as the page.

So Im using:

 var pageRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<EPiServer.Web.Routing.PageRouteHelper>();
            PageReference currentPageLink = pageRouteHelper.PageLink;
            PageData currentPage = pageRouteHelper.Page;
            var categoryList = currentPage.Category; //get the current pages categories

To get my current page categgories

and Im using:

var list = contentRepository.GetDescendents(SiteDefinition.Current.GlobalAssetsRoot)
.Select(contentRepository.Get<IContent>)
.Where(c => c as ICategorizable != null)
.Where(c => ((ICategorizable)c).Category.Any(cat => cat.Equals(6))) // category ID 6
.ToList();



To get the assets - but I need to change the cat.Equals(6) to something that filters all the page categories.

I hope this makes sense,

jon

#144703
Edited, Feb 17, 2016 16:04
Vote:
 

Cat=>pagecategories.MemberOf(Cat)... 

something like that? Where page categories is the category property on block/page...

#144723
Feb 18, 2016 7:12
Vote:
 

And of course, this solution will only work if you have a limited amount of items. For larger amounts you will need a search index. EPiServer Find is your best friend there where you can easily build similar questions which have much much better performance.

#145616
Mar 08, 2016 11:44
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.