November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
I see now that i dident formulate my problem correctly. When i said "connected to a page" i meant that the file is located in "page files". Basicly what you could check with the .IsPageFile() extention-method in Find in previous versions.
And i also want also check what the page for thouse "page files" is, like GetPageReference() that used to exist in the previous version of Find. The files dosent necceceraly need to be used by the page, just included in the "page files"-folder.
Ok sp i found that you can get a the page that owns the MediaData-item with the ContentLoader instead of using FindPagesWithCriteria. So i guess my approach is ok then.
Teh codes:
public static class MediaDataExtentions
{
public static bool IsPageMedia(this MediaData media, IContentRepository contentRepository = null)
{
if (contentRepository == null)
contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
ContentAssetFolder contentAssetFolder = GetContentAssetFolder(media.ParentLink, contentRepository);
return contentAssetFolder != null;
}
public static IContent GetPageMediaOwner(this MediaData media, IContentRepository contentRepository = null)
{
if (contentRepository == null)
contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
ContentAssetFolder contentAssetFolder = GetContentAssetFolder(media.ParentLink, contentRepository);
var owner = contentRepository.Get<IContent>(contentAssetFolder.ContentOwnerID);
return owner;
}
private static ContentAssetFolder GetContentAssetFolder(ContentReference parentReference, IContentRepository contentRepository)
{
var folder = contentRepository.Get<IContent>(parentReference) as ContentFolder;
if (folder == null)
return null;
if (folder is ContentAssetFolder)
return folder as ContentAssetFolder;
return GetContentAssetFolder(folder.ParentLink, contentRepository);
}
}
Now that i have run this with some real data i found that sometimes i cant get the content for a ContentOwnerID i get a ContentNotFoundException on some items.
Is it normal that a ContentAssetFolder still has a ContentOwnerId but that content may be removed?
Yes, that can be the case.
When a content item is deleted its corresponding ContentAssetFolder (if any) will not be removed immediately. There is a sceduled job "Remove unrelated content assets" that will cleanup abandoned content asset folders.
An alternative to catch ContentNotFoundException is to call
var contentLinkMap = ServiceLocator.Current.GetInstance<IPermanentLinkMapper>().Find(ContentOwnerID) as PermanentContentLinkMap
that will return null if content does not exist.
For ContentReference there is a TryGet method on IContentLoader that does not throw ContentNotFound exception, however there is no overload that takes a guid.
I have been trying to find a way to determine if a MediaData-item is connected to a page and getting a content reference to that page. I havent found any direct support in the api of doing it unfortunally. Im hoping that i have just missed it though.
I have been able to hack something together that works but its not very efficient since i have to traverce the parentlinks of the MediaData-item until i find a ContentAssetFolder. The folder dosent contain any reference to the page its connected to though so then i would have to do something like a FindPagesWithCriteria on the entire pagetree looking for a page with the right PageContentAssetsID...
Any Ideas?