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

Try our conversational search powered by Generative AI!

Setting child property value as per the parent page

Vote:
 

I have a master page and child page .
on master page i have "keyword" property with value in that
I want that to be inherited in child page .
For that i have created a keyword property in child page
as follows:

[Display(GroupName = GroupNames.MetaData, Order = 2)]

[Editable(false , AllowInitialValue = true)]
public virtual string Keyword
{
get
{
if (myMaster != null)
{
return myMaster.GetPropertyValue(p => p.MetaData.Keywords);

}


return string.Empty;
}
set
{
this.SetPropertyValue(p => p.Keyword, value);
}
}

but the parent value is not getting set in the child page .

how do I get the value of keyword value of parent in child keyword property ?

#199135
Nov 16, 2018 10:54
Vote:
 

The reason your value isn't being saved to your content item is that there's nothing setting it, you simply return the value without modifying the content.

Depending on how you want it to work, I think you've got 2 main options. If you're looking to set the value when the page is first created then you should use "SetDefaultValues" on your model like this:

public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);
    //fetch your master page here
    var myMaster = GetMyMasterPageContent();

    //Set the value here
    if (myMaster != null)
    {
        Keyword = myMaster.Keyword;
    }
}

This is run once when the page is first created and used to set the initial values of the properties.

If you want the value to always be the same as the "master" page, even if the value in the "master" page changes after the child page has been created then you'll need to look up the value from the master page when it's needed. The code you've posted above looks like this option.

#199141
Edited, Nov 16, 2018 12:40
Vote:
 

Hi Paul,

I have made the changes as above and also created one IIniatialization Module as below :

[IInitializationModule]
[ModuleDependency(typeof(IInitializationModule)]

public void Initialize(InitializationEngine context)
{
var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
contentEvents.CreatedContent += InheritedPageValues;
}

private void InheritedPageValues(object sender, ContentEventArgs e)
{
PopulateMetadata(e);
}


private void PopulateMetadata(ContentEventArgs e)
{
var inheritedPage = e.Content as InheritStandardPageType;

if (inheritedPage != null)
{
var srvlok = ServiceLocator.Current.GetInstance<IcontentRepositry>;

if (!string.IsNullOrEmpty(result))
{
var srvcLoc = ServiceLocator.Current.GetInstance<IContentRepository>();
MasterStandardPage mymaster =ServiceLocator.Current.GetInstance<IContentRepository>().get<MasterStandardPageType> ;
inheritedPage.keywords = mymaster.metadata.keywords;
srvlok.Save(inheritedPage, SaveAction.Publish, EPiServer.Security.AccessLevel.Administer);
}
}
}

public void Uninitialize(InitializationEngine context)
{
var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
contentEvents.CreatedContent -= InheritedPageValues;
}
}

But the above code only fetches the parent keyword in the local when the parent page is published again.

How do i get the parent keyword in the local without publishing ?

#199166
Nov 19, 2018 8:53
Vote:
 

Based on the InitializationModule posted above, it looks like you're trying to set the 'keywords' property of each page of type InheritStandardPageType when it's created then never change that value for the lifetime of the page. Is that correct or does the 'keywords' value in each InheritStandardPageType page need to change when the value of the MasterStandardPage changes?

Are the InheritStandardPageType pages all created as direct children of MasterStandardPage pages or do the MasterStandardPage pages live elsewhere?

#199217
Nov 20, 2018 15:14
* 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.