November Happy Hour will be moved to Thursday December 5th.
AI OnAI Off
November Happy Hour will be moved to Thursday December 5th.
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.GetReferenceInformationForContent, IContentRepository.GetReferencesToContent, ContentSoftLinkRepository.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) { } } }
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:
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:
My question: Am I way over doing thins?