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

Get Page inside a block

Vote:
 

Hi!

So I had a problem, inside a selection factory of a block I needed properties of the page (or block) where the block was added. I knew that the block only could be added to a single type of page since I had added the "AllowedTypes" attribute. I ended up with a generic recursive method where I could do something like this:

var somePage = contentService.GetPage(contentMetadata.OwnerContent as IContent);

Where OwnerContent is the ExtendedMetadata from the "GetSelections" method of the selection factory.

So I have a method where I can from the ownerContent of a block inside a SelectionFactory can get to wathever page/block up in the hierarchy.

The GetPage method:

public T GetPage(IContent ownerContent)
        {
            if (ownerContent is T)
                return (T)ownerContent;

            if (ownerContent is ContentAssetFolder)
            {
                var assetFolder = ownerContent as ContentAssetFolder;
                return GetPage(_contentLoader.Get(assetFolder.ContentOwnerID));
            }

            if (ownerContent is IContent && ownerContent.ParentLink != null)
            {
                return GetPage(_contentLoader.Get(ownerContent.ParentLink));
            }

            return (T)(object)null;
        }

My question: Am I way over doing thins?

#139453
Oct 02, 2015 14:21
Vote:
 

Hi,

Your code will work when then block was created in "For this page" folder. If the block was placed somewhere else than it could not work (I didn't compiled your code, just how it looks like). 

I think that with this limitation it should be most efficient way to find the "owner" page and everything is correct.

If you would like to place block somewhere else, then you could use ContentStore.GetReferenceInformationForContentIContentRepository.GetReferencesToContentContentSoftLinkRepository.Load methods.

The code snippet is below:

    public class ContactPageSelectionFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            var contentReference = ((EPiServer.Core.IContent)metadata.Parent.Model).ContentLink;

            ContentStore contentStore = ServiceLocator.Current.GetInstance<ContentStore>();
            IEnumerable<ReferenceInformation> referencedLinks = contentStore.GetReferenceInformationForContent(contentReference, false);
            foreach (var referenceInformation in referencedLinks)
            {
                
            }
            
            IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            List<ReferenceInformation> referenceInformations = contentRepository.GetReferencesToContent(contentReference, true).ToList();
            foreach (var referenceInformation in referenceInformations)
            {
                
            }

            ContentSoftLinkRepository softLinkRepository = ServiceLocator.Current.GetInstance<ContentSoftLinkRepository>();
            IList<SoftLink> softLinks = softLinkRepository.Load(contentReference.ToReferenceWithoutVersion());
            foreach (var softLink in softLinks)
            {

            }

 
        }
    }
#139472
Oct 02, 2015 20:43
Vote:
 

Thanks  Grzegorz! I will give that a go :)

#139485
Oct 03, 2015 19:30
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.