Change the Title, Description and other metadata using code

Posted by Community Admin on 04-Aug-2018 11:07

Change the Title, Description and other metadata using code

All Replies

Posted by Community Admin on 24-Feb-2012 00:00

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

Posted by Community Admin on 24-Feb-2012 00:00

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

Posted by Community Admin on 27-Feb-2013 00:00

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?

Posted by Community Admin on 27-Feb-2013 00:00

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);

This thread is closed