Dave Kane
Dave Kane

Reputation: 69

Ektron 8.6 - Appending Metadata Tags into the Head tags of a page

I am trying to automatically append some Metadata onto a page in Ektron dynamically.

I have created a Metadata item in the settings area of Ektron called "Twitter Card" which has been set to a text property.

As twitter card Metatags are different they are reliant at page level so by adding this by the use of a Metadata option in the page settings seems the only possibe solution.

I have added the following into the Head section of my Master Template.

asp:Literal ID="SetTwitterCard" runat="server" 

And I have also added this into the .cs part of the page but I'm having no success

protected void SetTwitterCard ()
{
  HtmlMeta meta = new HtmlMeta();
  meta.Name = "TwitterCard";
  meta.Content = this.CurrentPage.GetMetaDataValue(Resources.Metadata.TwitterCard);
  this.Page.Header.Controls.Add(meta);
}

Upvotes: 0

Views: 606

Answers (2)

fooser
fooser

Reputation: 852

Assuming this.CurrentPage.GetMetaDataValue(Resources.Metadata.TwitterCard) works, the following is how I typically have metadata added on my masterpage(s). It may work in your case as well.

Front end:

<meta id="metaTwitterCard" runat="server" name="TwitterCard" content=""/>

Back end:

    protected void SetTwitterCard ()
    {
      /*HtmlMeta meta = new HtmlMeta();
      meta.Name = "TwitterCard";
      meta.Content = this.CurrentPage.GetMetaDataValue(Resources.Metadata.TwitterCard);
      this.Page.Header.Controls.Add(meta);*/

      // Fill in "TwitterCard" content.
      metaTwitterCard.Content = this.CurrentPage.GetMetaDataValue(Resources.Metadata.TwitterCard);
      // Hide meta tag if content invalid or unavailable.
      metaTwitterCard.Visible = !String.IsNullOrWhiteSpace(metaTwitterCard.Content);
    }

Upvotes: 0

Brian Oliver
Brian Oliver

Reputation: 1407

If you are using a literal defined on your ASPX page's front end then you won't need to programmatically add any controls to Page.Header.Controls. Choose one method or the other.

Common Code

First, Add this code to your page class. It loads the ContentData with its metadata from the primary content block if present (id query string parameter) and falls back on the form block if present (ekfrm query string parameter) and finally falls back on the PageBuilder page layout if present (pageid query string parameter). Both option 1 and option 2 require all of this "common code" to be present in your page class in order to function.

private bool _isContentLoaded;
private Ektron.Cms.ContentData _content;

private string GetMetadataFromPrimaryContent(string metadataName)
{
    EnsureContentIsLoadedFromEktron();

    if (_content == null || _content.MetaData == null) return null;

    var data = _content.MetaData.FirstOrDefault(md => md.Name == metadataName);
    return data == null ? null : data.Text;
}

private void EnsureContentIsLoadedFromEktron()
{
    if (_isContentLoaded) return;

    _isContentLoaded = true;
    var cm = new Ektron.Cms.Framework.Content.ContentManager();

    long id;
    long.TryParse(Request.QueryString["id"], out id);

    if (id > 0)
        _content = cm.GetItem(id, true);

    if (_content != null) return;

    long.TryParse(Request.QueryString["ekfrm"], out id);
    if (id > 0)
        _content = cm.GetItem(id, true);

    if (_content != null) return;

    // if no content is returned and we have a page id,
    // try getting that content block, since PageBuilder
    // pages can have metadata on them.
    long.TryParse(Request.QueryString["pageid"], out id);
    if (id > 0)
        _content = cm.GetItem(id, true);
}

Option 1: Using a literal.

<asp:Literal ID="ltlTwitterCard" runat="server" />
protected void Page_Load(object sender, EventArgs e)
{
    var metadataValue = GetMetadataFromPrimaryContent("TwitterCard");
    if (!string.IsNullOrEmpty(metadataValue))
        ltlTwitterCard.Text = "<meta name=\"TwitterCard\" content=\""
                                + metadataValue
                                + "\" >";
}

Option 2: Programmatically add the control to the Head.

If using this route, please note the following from www.asp.net:

Notice that the <head> element contains a runat="server" attribute, which indicates that it is a server control (rather than static HTML).

protected void Page_Load(object sender, EventArgs e)
{
    var meta = new HtmlMeta
    {
        Name = "TwitterCard",
        Content = GetMetadataFromPrimaryContent("TwitterCard") ?? ""
    };

    this.Page.Header.Controls.Add(meta);
}

Upvotes: 0

Related Questions