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...
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?
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 :)
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