+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Page Title, Keywords, Description

Page Title, Keywords, Description

16 posts, 0 answered
  1. Rob
    Rob avatar
    9 posts
    Registered:
    12 Jun 2008
    29 Oct 2008
    Link to this post
    I have a custom control that retrieves data from the database and displays it on the page.  I want to uses some of the fields from the database to set the page title, meta Keywords, and meta Description.  How can I accomplish that?
  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    08 Dec 2016
    30 Oct 2008
    Link to this post
    Hi Rob,

    Title, Keywords and Description are properties of the ICmsPage object. So you could create a user control that casts the current page to CmsPageBase and gets the value of the required properties. Here is a simple example:

    .ascx
    <strong>Title:</strong> <asp:Label ID="title" runat="server"></asp:Label><br /> 
    <strong>Keywords:</strong> <asp:Label ID="keywords" runat="server"></asp:Label><br /> 
    <strong>Description:</strong> <asp:Label ID="description" runat="server"></asp:Label> 

    .ascx.cs
    protected void Page_Load(object sender, EventArgs e) 
        { 
            Telerik.Cms.Web.CmsPageBase page = this.Page as Telerik.Cms.Web.CmsPageBase; 
            if (page != null
            { 
                this.title.Text = page.CmsPage.Title; 
                this.keywords.Text = page.CmsPage.Keywords; 
                this.description.Text = page.CmsPage.Description; 
            }       
        } 

    Hope this helps.

    Sincerely yours,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Rob
    Rob avatar
    9 posts
    Registered:
    12 Jun 2008
    30 Oct 2008
    Link to this post
    I want to do the opposite.  Can I set the title, keywords and description like

    page.CmsPage.Title = strTitle;
    page.CmsPage.Keywords = strKeywords;
    page.CmsPage.Description = strDescription;

    I want to do this because the custom control gets data dynamically from the database.

  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    08 Dec 2016
    31 Oct 2008
    Link to this post
    Hi Rob,

    Here is an example for the required functionality:

    .ascx
    <div><strong>Title:</strong> <asp:TextBox ID="title" runat="server"></asp:TextBox></div
    <div><strong>Keywords:</strong> <asp:TextBox ID="keywords" runat="server"></asp:TextBox></div
    <div><strong>Description:</strong> <asp:TextBox ID="description" runat="server"></asp:TextBox></div
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" /> 

    .ascx.cs
    protected void Button1_Click(object sender, EventArgs e) 
        { 
            Telerik.Cms.Web.CmsPageBase page = this.Page as Telerik.Cms.Web.CmsPageBase; 
     
            if (page != null
            { 
                Telerik.Cms.CmsManager manager = new Telerik.Cms.CmsManager(); 
                Telerik.Cms.ICmsPage cmsPage = manager.GetPage(page.CmsPage.ID, trueas Telerik.Cms.ICmsPage; 
                 
                cmsPage.Title = this.title.Text; 
                cmsPage.Keywords = this.keywords.Text; 
                cmsPage.Description = this.description.Text; 
     
                manager.SavePage(cmsPage); 
            } 
        } 

    Hope this helps.

    Kind regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Shawn
    Shawn avatar
    10 posts
    Registered:
    09 Jan 2009
    26 May 2009
    Link to this post
    Hi Pepi,

    I would like to set my Title, Description, and Keywords also programmatically like above, but I don't want to set them for the current page then save.. I just want to set them for the current request..  Could you lead me in the right direction?  When I cast this.Page to a CmsPageBase and then try to set .CmsPage.Title = "Blah" I get an error about not being in a transaction. 

    Basically all I want to do is override Title, Keywords, and Description on pages that contain my control.  Thanks!

    --shawn
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    27 May 2009
    Link to this post
    Hello Shawn,

    The sample below illustrates how the set the page properties every time when you page is requested. We are getting the current page( on which you have added your user control) and set our properties.

        protected void Page_Load(object sender, EventArgs e) 
        { 
     
            CmsManager manager = new CmsManager(); 
            CmsSiteMapNode currentNode = SiteMap.CurrentNode as CmsSiteMapNode; 
            ICmsPage currentPage = currentNode.CmsPage as ICmsPage;       
            ICmsPage page = manager.GetPage(currentPage.ID, trueas ICmsPage; 
            page.Title = "MyTitle"
            page.Keywords = "MyKeyWords"
            page.Description = "This is a page that uses my custom user control"
            // here you can set additonal page properties. 
            manager.SavePage(page); 
        } 

    The error you are getting generally occurs when the has not been got by the manager.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Ruben Herrera
    Ruben Herrera avatar
    38 posts
    Registered:
    10 Jul 2009
    19 Apr 2010
    Link to this post
    Hello,

    Ivan, I´m using your method but page does not show title well until second refresh, It is remember last request.

    ex:
    http://www.axesor.es/registro/Civil/Almeria
    (first level directory: http://www.axesor.es/registro )

    Regards
    Rubén
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    20 Apr 2010
    Link to this post
    Hi Ruben Herrera,

    There is a better way to do this. You have to create a custom class that inherits from InternalPage and there override SetTitle method. You do not have to call the base. You could call directly this.Title = "somevalue "

    You have to replace the default internal page with your custom one in Sitefinity/cmsentrypoint.aspx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Cms.Web;
     
    /// <summary>
    /// Summary description for InternalPageCustom
    /// </summary>
    public class InternalPageCustom : InternalPage
    {
        public InternalPageCustom()
        {
     
        }
     
        protected override void SetTitle(Telerik.Cms.ICmsPage page)
        {
            this.Title = "somevalue";
        }
    }


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  9. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    01 Jul 2011
    Link to this post
    Hey.

    Is there any way to override the description and keywords set by sitefinity per request basis without saving it to the db (as manager.SavePage(page) ) seems to do.The SetTitle works sure, but thats just for the Page.Title, not the cmspage.Title, .Description and Keywords.

    Basically I want to set title, desc and keywords based on parameters I supply via a module Im using on a page. Meaning, I got one CmsPage displaying various content based on another parameter (a productid in this case). 

    Maybe some easy way to make Sitefinity not add those tags to the header so I can add them myself?

    Thanks in advance.

    //Andreas
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    01 Jul 2011
    Link to this post
    Hi Andreas,

    Inside a custom class that inherits from InternalPage you can set some of the ICmsPage properties at runtime without saving them to the database. There is a virtual method AddHeaderControls that you can override , so you will have access to the header - Description, Keywords etc.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    01 Jul 2011
    Link to this post
    Thanks Ivan.

    Overriding AddHeaderControls in my custompage class still adds the regular description and keywords though. Adding another set of metatags works fine (always did), just not getting rid of the sitefinity added ones still.

    //Andreas
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    04 Jul 2011
    Link to this post
    Hello Andreas,

    If you override AddHeaderControls and you don't call the base there should not be keys added by the InternalPage.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  13. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    04 Jul 2011
    Link to this post
    No base called but title, desc and keywords are still getting added. My CustomBasePage is getting used, as well as the overridden method, so it's not that I haven't got the custompage hooked up correctly.

    Are you able to get it to work? Removing the keys in the header?

    //Andreas
  14. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    04 Jul 2011
    Link to this post
    Hi Andreas,

    Ok, I saw what the problem is. The desciption and keywords are set in CreateChildControls. Please take a look at the sample below that shows how to remove Title, Description and Keywords

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Cms.Web;
    using Telerik.Cms;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using Telerik.Caching;
    using System.IO;
    using System.Web.UI.HtmlControls;
    using Telerik.Cms.Security;
    using System.Web.Security;
     
    /// <summary>
    /// Summary description for InternalPageCustom
    /// </summary>
    public class InternalPageCustom : InternalPage
    {
     
     
        protected override void SetTitle(ICmsPage page)
        {
            this.Title = "somevalue";
        }
     
        protected override void CreateChildControls()
        {
            ICmsPage cmsPage = base.CmsPage;
            this.SetTitle(cmsPage);
            if (!String.IsNullOrEmpty(cmsPage.Description))
            {
                // add your custom description
            }
            if (!String.IsNullOrEmpty(cmsPage.Keywords))
            {
     
                // add custom keywords
            }
            if (cmsPage.Template == null)
            {
                this.Controls.Clear();
     
                ITemplate template;
                string path = this.ResolveUrl(UrlHelper.SitefinityRoot + "ControlTemplates/NoTemplateTemplate.ascx");
                if (File.Exists(this.MapPath(path)))
                    template = this.Page.LoadTemplate(path);
                else
                    template = new NoTemplateTemplate();
                template.InstantiateIn(this);
                return;
            }
     
            InitializeCache();
     
            this.AddHeaderControls(cmsPage);
            this.AddPageControls(
                cmsPage,
                FindContentPlaceHolders(this.Master));
        }
     
     
        internal static Dictionary<string, ContentPlaceHolder> FindContentPlaceHolders(MasterPage master)
        {
            return FindContentPlaceHolders(master, null);
        }
     
        internal static Dictionary<string, ContentPlaceHolder> FindContentPlaceHolders(MasterPage master, IList<string> controlIds)
        {
            lock (syncRoot)
            {
                Dictionary<string, ContentPlaceHolder> list = new Dictionary<string, ContentPlaceHolder>();
                while (master.Master != null)
                    master = master.Master;
                FindContentPlaceHolders(master.Controls, list, controlIds);
                return list;
            }
        }
     
        private static void FindContentPlaceHolders(ControlCollection controls, Dictionary<string, ContentPlaceHolder> list, IList<string> controlIds)
        {
            foreach (Control ctrl in controls)
            {
                if (ctrl is ContentPlaceHolder)
                {
                    ContentPlaceHolder holder = (ContentPlaceHolder)ctrl;
                    list.Add(holder.ID.ToLower(), holder);
                }
                if (controlIds != null && !String.IsNullOrEmpty(ctrl.ID))
                    controlIds.Add(ctrl.ID.ToLower());
                FindContentPlaceHolders(ctrl.Controls, list, controlIds);
            }
        }
     
        private static object syncRoot = new object();
     
    }


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  15. Andreas
    Andreas avatar
    49 posts
    Registered:
    29 Dec 2007
    04 Jul 2011
    Link to this post
    Once again you deliver! Thanks alot, works perfectly fine.

    //Andreas
  16. Francesco
    Francesco avatar
    1 posts
    Registered:
    17 Jan 2012
    19 Apr 2012
    Link to this post
    Dear Ivan,

    It was good to meet you very briefly at the Sitefinity Seminar a couple of days ago.  Most impressive vision you have for the product and a good presentation.

    Anyway, I can't seem to find the Telerik.Cms.dll and Telerik.Cms.Web.dll in my Sitefinity 4.4 or 5.0 installation or in the SDKs.  Where are they located?

    Regards,

    Raza Shah
    The Legatum Institute 
Register for webinar
16 posts, 0 answered