Set page metadata fields from api

Targutai Yesugei asked on July 28, 2017 14:29

Hello again!

I tried _treeNode.SetValue("DocumentPageTitle", title); it doesn't work.

TreeNode's DocumentPageDescription and DocumentPageTitle have only getters.

So, i require your help, to solve my problem. Thanks in advance!

Recent Answers


Zach Perry answered on July 28, 2017 15:45

see if this previous question solves your problem.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on July 28, 2017 15:48 (last edited on July 28, 2017 16:14)

Can you publish your code? How do you get _treeNode? _treeNode.SetValue("DocumentPageTitle", title) is supposed work.
P.S. Oh yes , Did you call _treeNode.Update() after all your manipulations with _treenode;

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on July 28, 2017 16:33

Zachary, thanks! I already checked this question, but it didn't help me.

Peter, i'm generating treenodes manually via TreeNode.New() and after setting, i'm updating it. I set lot's of properties and fields but after treenode inserting there are all of them presenting in CMS except DocumentPageTitle and DocumentPageDescription fields. So, i don't think that problem appear because i'm not inserting or updating treenodes.

0 votesVote for this answer Mark as a Correct answer

Zach Perry answered on July 28, 2017 16:40

You might have to set it to not inherrited based off this code from MetaDataControlExtender:

/// <summary>
    /// Sets value to TreeNode property given by field name.
    /// </summary>
    /// <param name="node">Current instance of edited node</param>
    /// <param name="fieldName">Name of field which can have inherited value</param>
    private void SetField(TreeNode node, string fieldName)
    {
        node.SetValue(fieldName, null);

        CMSCheckBox chk = GetCheckBox(fieldName);
        if (!chk.Checked)
        {
            // Set not inherited value
            node.SetValue(fieldName, Control.FieldControls[fieldName].Value);
        }
    }
0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 28, 2017 17:45

Documents and Tree Nodes can be 'tricky' because depending on your settings, it can involve different steps.

If you have Workflow enabled, you will probably need to follow the workflow example.

If you have check in / check out, that will also alter your code.

Check the API example page here, and look through the various code snippets, and pick the right one depending on your Workflow/Versioning settings.

https://docs.kentico.com/api10/content-management/pages

https://docs.kentico.com/api10/content-management/page-workflow-and-versioning

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on July 31, 2017 09:44 (last edited on July 31, 2017 09:44)

Zachary, i didn't find CMSCheckBox class, so i can't use it. But I don't understand how this GetCheckBox() method will acquire value, if it even doesn't know what page's metadata it working with.

Trevor. thanks for your answer, but i don't know anything about Workflow, so i don't understand how it will help me.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on July 31, 2017 15:28

Targutai, when you dive into doing things in the API it will help to understand the back end structure of the Pages.

I'll try my best to summarize, but depending on how you have things set up, it will alter what you need to do to set a value on a page through the API.

  1. Each Node on the Tree has a single entry in the CMS_Tree table, along with that it also will have a CMS_Document entry that represents the language version of that node. Lastly depending on what type of page type it is, it will have an entry in that page type's table (ex content_news) that holds the page type specific information (such as news story)
  2. If you have a workflow turned on (Publishing and/or versioning) when you save a document, it actually makes a change to a temporary table until it's published or made live, this table is the CMS_VersionHistory, likewise it also has Workflow tables it needs to adjust (CMS_WOrkflowStep, CMS_WorkflowHistory, etc).

This is why, depending on your setup, there are different API steps that need to occur.

So let me ask you, are any of these pages on a workflow (In the Kentico Admin, go to the Workflow module, and see if any have a Scope that cover your documents).

If not, then use the API here. Otherwise look at the links and find the right one!

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on January 31, 2018 22:24 (last edited on February 1, 2018 01:13)

Targutai, did you ever get this worked out? Thanks!

UPDATE - Here is how I finally got it ... I was pulling the title and description from the first item in a DocumentsDataSource, but however you need to get your titles, most of the rest of this should work.

<script runat="server">

  string _title = String.Empty;
  string _desc = String.Empty;  

  protected override void OnInit(EventArgs e) 
  {
    base.OnInit(e);
    System.Data.DataView dv = (System.Data.DataView)this.srcDocuments.DataSource;
    System.Data.DataTable dt = dv.ToTable();
    if(dt.Rows.Count > 0)
    {
      _title = CMS.GlobalHelper.ValidationHelper.GetString(dt.Rows[0]["DocumentPageTitle"], "");
      _desc = CMS.GlobalHelper.ValidationHelper.GetString(dt.Rows[0]["DocumentPageDescription"], "");

      if(!String.IsNullOrEmpty(_desc))
      {
        CMSContext.CurrentDescription = _desc;
      }
    }
  }

  protected override void OnPreRender(EventArgs e)
  {
    if(!String.IsNullOrEmpty(_title))
    {
      Page.Title = _title;
    }
  }

</script>
0 votesVote for this answer Mark as a Correct answer

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