Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Set PageName as Heading

Vote:
 

On the standard page we have a property Heading, which is editable on page. How can I set the Heading property to fallback to PageName (if Heading is not set) on all the existing pages (site updated from 6 to 7.5x)

#114247
Dec 08, 2014 13:56
Vote:
 
public virtual string Heading
{
            get
            {
                return this.GetPropertyValue(p => p.Heading) ?? PageName;
            }
            set
            {
                this.SetPropertyValue(p => p.Heading, value);
            }
}

Hope this helps.

Frederik

#114263
Dec 08, 2014 19:44
Vote:
 

Just wanna add to what Frederik just wrote, you need to import the EPiServer.Core namespac aswell, since those methods are extension methods. However this will not work if you're using webforms and the property webcontrol.

This is a working solution if you're on webforms:

[PagePlugIn(
    "Property fallbacks", 
    "Handles fallbacks for properties on a global basis.")]
public class PropertyFallbacks
{
    private static Hashtable FallbackProperties { get; set; }

    public static void Initialize(int optionFlags)
    {
        PropertyDataCollection.GetHandler = FallbackPropertyHandler;

        FallbackProperties = new Hashtable { { "PageHeading", "PageName" } };
    }

    public static PropertyData FallbackPropertyHandler(string name, PropertyDataCollection properties)
    {
        var data = properties.Get(name);

        if (data != null && (!data.IsNull || data.IsMetaData))
        {
            return data;
        }

        var dataFromOtherPage = PropertyGetHandler.FetchDataFrom(name, properties);

        if (dataFromOtherPage != null && dataFromOtherPage.Value != null)
        {
            return dataFromOtherPage;
        }

        if (FallbackProperties.ContainsKey(name))
        {
            var dataFallback = FallbackPropertyHandler(FallbackProperties[name].ToString(), properties);

            if (dataFallback != null && dataFallback.Value != null)
            {
                return dataFallback;
            }
        }

        return DynamicPropertyCache.DynamicPropertyFinder.FindDynamicProperty(name, properties) ?? data;
    }
}

This might also be interesting http://tedgustaf.com/blog/2014/11/fallback-property-values-in-episerver-using-attributes/.

#114266
Edited, Dec 08, 2014 22:05
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.