Get Page Metadata Inherited from Parent via API

Gustavo Quevedo asked on August 1, 2019 18:18

Hi,

I'm trying to retrieve pages metadata by using the API, from a Kentico 12 MVC site as below:

var doc = DocumentHelper.GetDocuments<PageType>()
   .Columns(new[] { "DocumentPageTitle", "DocumentPageDescription", "DocumentPageKeyWords" })
   .OnSite(SiteContext.CurrentSiteName)
   .Published()
   .PublishedVersion()
   .TopN(1)
   .WhereEquals("NodeGUID", guid)
   .FirstOrDefault();

This works fine and stores the values entered on the Pages / Properties / Metadata tab e.g. in doc.DocumentPageTitle.

However, if I leave the "Inherit" check box ticked e.g. in the page keywords field, I get an empty string for doc.DocumentPageKeyWords.
I would expect to get a value, as I can see the inherited value grayed out in the disabled text area.

I have tried to retrieve the value by using:

  • doc.GetInheritedValue("DocumentPageKeyWords");
  • doc.LoadInheritedValues(new[] { "DocumentPageKeyWords" }); and then doc.DocumentPageKeyWords
  • doc.LoadInheritedValues(new[] { "DocumentPageKeyWords" }); and then doc.GetInheritedValue("DocumentPageKeyWords");

but nothing seems to work.

I suspect that I should load the values from parent nodes in my initial query but I wouldn't know how to do that.

Any help would be appreciated.

Kind regards, Gustavo

Correct Answer

Dmitry Bastron answered on August 2, 2019 11:06

Hi Gustavo,

You were almost there! This code is working without coupled values, tested myself:

var doc = DocumentHelper.GetDocuments<PageType>()
    .Columns("DocumentPageTitle", "DocumentPageDescription", "DocumentPageKeyWords", "NodeAliasPath", "NodeSiteID", "DocumentCulture")
    .OnSite(SiteContext.CurrentSiteName)
    .Published()
    .PublishedVersion()
    .TopN(1)
    .WhereEquals("NodeGUID", guid)
    .FirstOrDefault();
doc.LoadInheritedValues(new[] {"DocumentPageTitle"});
var hereIsYourInheritedTitle = doc.DocumentPageTitle;
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on August 2, 2019 05:15

I'd suggest taking a look at this post as it seems the OP has figured it out.

0 votesVote for this answer Mark as a Correct answer

Gustavo Quevedo answered on August 2, 2019 12:18 (last edited on August 2, 2019 12:19)

Yes, adding the "NodeAliasPath", "NodeSiteID" and "DocumentCulture" columns to the query made it work. Thank you!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.