November Happy Hour will be moved to Thursday December 5th.

query to get all media files in episerver 8

Vote:
 

Hi,

I want to fetch all files which resides under media Tab and display it in SiteMap.

is there any query by which i cvan fetch all the media files?

Help appreciated

#172185
Nov 25, 2016 12:19
Vote:
 

Since media is simply content like everything else you can use IContentLoader or IContentRepository to list children and dig your way down (or similar). The root for media you can get from SiteDefinition...

Some sample code for the first level...(notice though that you will get some folders as well that are not media here)

            var assetsRoot = SiteDefinition.Current.GlobalAssetsRoot;
            var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
            var mediaChildrenReferences = contentLoader.GetChildren<IContent>(assetsRoot);
            foreach (var child in mediaChildrenReferences)
            {
                var name = child.Name;
                var childMedia = child as MediaData;
                if (childMedia != null)
                {
                    var url = UrlResolver.Current.GetUrl(childMedia.ContentLink);
                }
            }

To get all levels you can either use GetDescendants() method or do a recursive GetChildren(). Remember to filter on access etc using filter for visitor or similar...
Pretty expensive operation so probably want to cache the result as well...

#172192
Edited, Nov 25, 2016 13:22
Vote:
 

Hi Daniel,

Thanks for the reply, but i am unable to fetch MediaData if i am using GetDescendants.

i want to fetch whole SiteMediaData exdept images from media tab?

 

#172274
Nov 29, 2016 6:49
Vote:
 

Something like...

var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
var descendants = repo.GetDescendents(assetsRoot).Where(p => repo.Get<IContent>(p) is SiteMediaData).Select(repo.Get<SiteMediaData>)

GetDescendents will give you a list of references basically. Then you can fetch each page and check the type etc to filter out what you need...

Hope that helps you on the way... :)

Pretty heavy operation so not something you want to run on every request :)

#172277
Edited, Nov 29, 2016 9:05
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.