Opticon Stockholm is on Tuesday September 10th, hope to see you there!

Martin Helgesen
Jan 16, 2011
  4786
(0 votes)

Chain of data fetching

I’m sure you are familiar with the EPiServer feature that allows a page to fetch data from another page. A webeditor can easily enable this feature in edit mode, and this works fine.

But what about a page that fetches data from a page that fetches data from another page? In this case we have a chain of 3 pages in inheritance. What does EPiServer do about this?

Let’s try:

  • Page A
    • Heading – “Base Page”
    • MainBody – “Base Page Body”
  • Page B
    • Heading – “Company Page”
    • MainBody – empty
    • Fetches data from Page A
  • Page C
    • Heading – “Department Page”
    • MainBody – empty
    • Fetches data from Page B

When showing Page B the MainBody propertys value will be “Base Page Body”. However, when showing Page C the MainBody property will be empty.

Let’s say you want to set up a site for a big company with subcompnaies and departments. 3 chains. And the top company want to distribute pages to the subcompanies with company guidelines that can be overridden. And, likewise, the subcompanies want to distribute those pages to the departments.

One way of getting this to work is to have chains of data-fetching. In our example, this means that Page C’s MainBody would not be empty when shown. It would have shown data from Page A.

The solution to this is to implement your own GetPropertyHandler. Using reflector, I fetched the code for EPiServer’s default property handler, and modified it:

 

namespace EPiServer.Custom.Handlers
{
    public class PropertyHandlers
    {
        public static PropertyData DefaultPropertyHandler(string name, PropertyDataCollection properties)
        {
            PropertyData data = properties.Get(name);
            if (data != null)
            {
                if (!data.IsNull || data.IsMetaData)
                {
                    return data;
                }

            }
            PropertyData data3 = FetchDataFrom(name, properties);
            if ((data3 != null) && (data3.Value != null))
            {
                return data3;
            }
            return (DynamicPropertyCache.DynamicPropertyFinder.FindDynamicProperty(name, properties) ?? data);
        }

        public static PropertyData FetchDataFrom(string name, PropertyDataCollection properties)
        {
            PropertyData data = properties.Get("PageShortcutType");
            if (((data == null) || (data.Value == null)) || (((PageShortcutType)data.Value) != PageShortcutType.FetchData))
            {
                return null;
            }
            data = properties.Get("PageShortcutLink");
            if (data == null)
            {
                return null;
            }
            PageReference pageLink = (PageReference)data.Value;
            if (!PageReference.IsValue(pageLink))
            {
                return null;
            }
            string languageBranch = properties.Get("PageLanguageBranch").Value as string;
            return GetPropertyFromPage(pageLink, languageBranch, name);
        }

        private static PropertyData GetPropertyFromPage(PageReference fetchPageLink, string languageBranch, string propertyName)
        {
            PageData page;
            if (string.IsNullOrEmpty(languageBranch))
            {
                page = DataFactory.Instance.GetPage(fetchPageLink);
            }
            else
            {
                page = DataFactory.Instance.GetPage(fetchPageLink, LanguageSelector.Fallback(languageBranch, true));
            }
            PropertyData data2 = page.Property.Get(propertyName);
            if (((data2 != null) && !data2.IsLanguageSpecific) && (data2.Value == null))
            {
                return DataFactory.Instance.GetPage(page.PageLink, LanguageSelector.MasterLanguage()).Property.Get(data2.Name);
            }
            if (string.Compare(propertyName, "MainBody", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var data3 = FetchDataFrom(propertyName, page.Property);
                if (data3 != null)
                {
                    return data3;
                }
            }
            return data2;
        }
     

    }
}

As you can see from the code, this fetch data logic is recursive as long as the property is named “MainBody”.

And then, you have hook up this handler in Global.asax Application_Start method:

        protected void Application_Start(Object sender, EventArgs e)
        {
            XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);
            PropertyDataCollection.GetHandler = EPiServer.Custom.Handlers.PropertyHandlers.DefaultPropertyHandler;
        }

I guess there are other ways of solving chains of inheritance, feedback is very welcome Smilefjes

Jan 16, 2011

Comments

bvskanth@gmail.com
bvskanth@gmail.com Jan 17, 2011 04:44 PM

Nice post :)

Please login to comment.
Latest blogs
SNAT - Azure App Service socket exhaustion

Did you know that using HttpClient within a using statement can cause SNAT (Source Network Address Translation) port exhaustion? This can lead to...

Oleksandr Zvieriev | Sep 9, 2024

Micro front-ends are massive for Optimizely One

Optimizely products have evolved. Their new generation of products changes the game.   A multi-year journey for Optimizely. They have engineered...

Mark Everard | Sep 9, 2024 | Syndicated blog

Micro front-ends are massive for Optimizely One

Optimizely products have evolved. Their new generation of products changes the game.

Mark Everard | Sep 9, 2024 | Syndicated blog

Handling Nynorsk and Bokmål in Optimizely CMS

Warning: Blog post about Norwegian language handling (but might be applicable to other languages and/or use cases). Optimizely have flexible and...

Haakon Peder Haugsten | Sep 5, 2024

Remove Unwanted properties for Headless Implementation using Content Delivery API

While working with Headless, whenever we want to send data to the front end, many properties are also shown in JSON that we don't wish to, which...

PuneetGarg | Sep 4, 2024

Optimizely Headless Form Setup

1. Create empty CMS applications First, let’s setup an empty CMS application. Install the NuGet packages in your solution using the NuGet Package...

Linh Hoang | Sep 4, 2024