More in this section

Forums / Developing with Sitefinity / Change the Title, Description and other metadata using code

Change the Title, Description and other metadata using code

4 posts, 0 answered
  1. Yosief
    Yosief avatar
    27 posts
    Registered:
    20 Mar 2008
    24 Feb 2012
    Link to this post
    Hi everyone

    I'm tasked with programmatically changing the title, description and other metadata(e.g. keywords) of pages in Sitefinity 4.4.

    I am new to Sitefinity 4.x and don't know of a way to achieve this in the new API's.

    Can you help me please?
    Yosief
  2. Steve
    Steve avatar
    3037 posts
    Registered:
    03 Dec 2008
    24 Feb 2012
    Link to this post
    Hey Yosief,
      It's SUPER easy :)

    Here's how: LINK

    You should be able to use intellisense to browse the properties you can change.

    Steve
  3. Coty
    Coty avatar
    29 posts
    Registered:
    25 Aug 2006
    27 Feb 2013 in reply to Steve
    Link to this post

    Hello Steve or anyone, old thread but the solution doesn't seem to work for me.

    Here is what I have:

    DynamicContent myPartner = GetPartner(myPartnerUrl);  //this gets returns a custom module object by Url
    var currentPage = SiteMapBase.GetActualCurrentNode();
     
    UpdateMetaData(currentPage.Id, myPartner.GetValue<Lstring>("metaTitle").Value, myPartner.GetValue<Lstring>("metaDescription").Value, myPartner.GetValue<Lstring>("metaKeywords").Value);
    UpdateMetaData Function Below:

    public void UpdateMetaData(Guid PageId, string htmlTitle, string description, string keywords)
        {
            PageManager pageManager = PageManager.GetManager();
     
            PageData page = pageManager.GetPageDataList()
                .Where(pD => (pD.Id == PageId && pD.Status == ContentLifecycleStatus.Live))
                .FirstOrDefault();
     
            if (page != null)
            {
                page.HtmlTitle = htmlTitle;
                page.Description = description;
                page.Keywords = keywords;
                pageManager.SaveChanges();
            }
            
        }

    I checked the values my Custom Module is returning by printing them to the the screen and I am getting the correct data back.  The function isn't overriding the Page MetaData Fields though?

  4. Coty
    Coty avatar
    29 posts
    Registered:
    25 Aug 2006
    27 Feb 2013 in reply to Coty
    Link to this post

    Talk about doing things the hard way, doing it the normal way works!

    Page.Title = myPartner.GetValue<Lstring>("metaTitle").Value;
    //Add Meta Data
    HtmlMeta metaDesc = new HtmlMeta();
    metaDesc.Name = "Description";
    metaDesc.Content = myPartner.GetValue<Lstring>("metaDescription").Value;
    Page.Header.Controls.Add(metaDesc);
    HtmlMeta metaKeywords = new HtmlMeta();
    metaKeywords.Name = "Keywords";
    metaKeywords.Content = myPartner.GetValue<Lstring>("metaKeywords").Value;
    Page.Header.Controls.Add(metaKeywords);

4 posts, 0 answered