Try our conversational search powered by Generative AI!

Getting the page for MediaData

Vote:
 

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?

#79492
Dec 18, 2013 16:35
Vote:
 

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.

#79518
Dec 19, 2013 10:24
Vote:
 

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);
        }
    }

 

#79568
Dec 20, 2013 9:19
Vote:
 

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?

 

#79979
Edited, Jan 13, 2014 10:13
Vote:
 

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.

#79981
Jan 13, 2014 10:59
Vote:
 

Ok, hadent seen that scheduelled job. Tnx.

All these new concepts... :D

#79982
Jan 13, 2014 11:02
Vote:
 

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.

#79983
Jan 13, 2014 11:02
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.