Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Recommended way to inherit properties from parent pages

Vote:
 

Hi there,

I am looking to inherit properties from parent pages based on a specific flag. I had a look at the FAQs section of the website and saw that dynamic properties are recommended for this, see https://world.episerver.com/FAQ/Items/How-do-I-inherit-values-between-pages/

I am currently on EpiServer v11 - Is this FAQ item still relevant?

Cheers,
Tom.

#208553
Oct 27, 2019 23:13
Vote:
 

Hi Tom,

I think the FAQ item you referred is for version 4 and 5.

So I think you can create a property on the BasePage and then use it where you want because it is available on all the pages and you can write your own logic to retrieve it.

#208558
Edited, Oct 28, 2019 11:01
Vote:
 

Hi Tom

You could emulate the deprecated Dynamic Properties behaviour, by adding the field to a base page type, an shared interface or similar. Then when you need the property value, you can look at the current page first to see if the property has a value.

If it has a value, return it. If it doesn't have a value, traverse up to the first immediate ancestor and check if that has a value for the property. If it has a value, return. If it doesn't, you continue upwards, until you reach a page that doesn't have the property.

#208561
Oct 28, 2019 11:41
Vote:
 

You could create an extension method, something like this:

private static object GetPropertyFromCurrentPageOrParent(this PageData pageData, string propertyName)
{
   if (pageData.Property[propertyName] != null)
      return pageData.Property[propertyName].Value;

   if (pageData.ContentLink == ContentReference.RootPage)
      return null;

   return ServiceLocator.Current.GetInstance<IContentLoader>().Get<PageData>
}

Use it to get property value by property name. If the property is null, property with the same name from the parent will be returned.

Calling ServiceLocator from within a recursive function is probably not the best idea. So you could rewrite it, like this:

private static object GetPropertyFromCurrentPageOrParent(this PageData pageData, string propertyName)
{
   while (true)
   {
      if (pageData.Property[propertyName] != null) return pageData.Property[propertyName].Value;
      if (pageData.ContentLink == ContentReference.RootPage) return null;
      pageData = ServiceLocator.Current.GetInstance<IContentLoader>().Get<PageData>(pageData.ParentLink);
      }
}
#208619
Edited, Oct 28, 2019 22:40
Vote:
 

Thank you all for your help.

I managed to solve this through the use of an interface shared across multiple page types (which provided the shared fields I needed).

I then made use of an ActionFilter - PageContextActionFilter : IResultFilter to execute the code before the page loaded (if the type is correct). I also used _contentLoader.GetAncestors(currentContentLink) to get a list of the parent pages. I looped through the list to check if my inherit field had been set and if it had then I updated the values in the view model of the current page.

Cheers,
Tom.

#208620
Oct 28, 2019 23:30
Vote:
 

Another solution is described here:
https://www.codeart.dk/blog/2018/9/good-ol-dynamic-properties/

And a slightly modified version near the end of this:
https://www.codeart.dk/blog/2019/10/ascend-2019-code-mania/

#208673
Oct 30, 2019 9:47
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.