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

Forums / Developing with Sitefinity / Set Custom Page Property in PagePropertiesEdit.ascx

Set Custom Page Property in PagePropertiesEdit.ascx

8 posts, 0 answered
  1. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    07 Sep 2010
    Link to this post
    Hi,
    I'm using the approach to setting a custom page property as outlined in Gabe's blog post:
    http://www.sitefinitywatch.com/blog/09-03-13/Hack_Adding_Properties_to_Sitefinity_Pages.aspx


    But instead of editing a page control, my client wants to edit the property in the PagePropertiesEdit.ascx page.  Ideally, we would like to edit the property when a page is created, too.

    I've added the user control directly to the master page header, and I can get it's property just fine.  But how do I find that control in the page when I'm editig the page properties?  I use this to get a reference to the page in the codebehind for PagePropertiesEdit.ascx:

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        Guid pageID = ((PageEditor)(this.Parent.Parent.Parent.Parent.Parent.Parent)).SelectedPageId;
        CmsManager cmsManager = new CmsManager();
        IPage pageFromDb = cmsManager.GetPage(pageID, true);
        Response.Write(pageFromDb.HeaderControls.Count);
    }

    but the HeaderControls collection doesn't appear to have anything in it.

    Regards,
    Mike
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    07 Sep 2010
    Link to this post
    Hi Mike,

    PagePropertiesEdit.ascx is just a template from where you can manage pages and this template is the same for all pages. PagePropertiesEdit.ascx is not the actual ICmsPage and this template does not have any HeaderControls in it. Furthermore this is a backend template used only for administration.

    You can take a look at


    All the best,
    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
  3. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    07 Sep 2010
    Link to this post
    Hi Ivan,

    Yes, I know the PagePropertiesEdit.ascx is just a template, and I've read both those posts.  The problem is that when I use those techniques to get a reference to the page, my user control doesn't seem to be accessible. 

    IPage pageFromDb = cmsManager.GetPage(pageID, true);

    The IPage object has a few properties that I thought I could use, for example:

    pageFromDb.HeaderControls

    But this collection is empty.  I think this is because the CmsManager only retrieves what it knows about the page instance from the database, and the entire controls collection isn't persisted.

    Then I thought I might simply add my control programmatically to the HeaderControl collection:

    Guid pageID = ((PageEditor)(this.Parent.Parent.Parent.Parent.Parent.Parent)).SelectedPageId;
    CmsManager cmsManager = new CmsManager();
    IPage pageFromDb = cmsManager.GetPage(pageID, true);
    Control myTitle = LoadControl("~/UserControls/PageInfo/MyControl.ascx");
    pageFromDb.AddHeaderControl(myTitle);
    cmsManager.SavePage(pageFromDb);

    But this throws "Header control type is not valid" presumably because my simple control doesn't have the right properties/interface for a header control.  This would work for me, if I could load my control, set a property on it, and add it to the header collection.

    So I figured I need to add the control to a placeholder on the page, but I don't see any way to do that from the page object you get from cmsManager.GetPage

    Regards,
    Mike
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    07 Sep 2010
    Link to this post
    Hi Mike,

    You can add HtmlControl control to the Header. If you are not using HtmlMeta control the error - "Header control type is not valid" is thrown.  We load the header controls when you request the page. When you use the default page properties for adding HeadTags, we clear the entire collection and add it again which may clear your data as well. You should better do this when the page is loaded and get the header control at runtime only during request. In the backend you can just save some data for the control that will be added and then retrieve this "property" inside custom InternalPage or PageLoad of an user/custom control. The better option is using InternlaPage.

    Best wishes,
    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
  5. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    08 Sep 2010
    Link to this post
    Thanks for clearing up the invalid control issue, and the heads-up on the fact that the Header Controls may be cleared out if a user sets meta tags. 

    A couple followup questions...

    I'm already extending InternalPage, so retrieving the "property" is fairly easy.  But where would I persist the data when the page is edited?  Another poster in this forum created a custom table that stored this in the DB, but I'd prefer to use a built-in persistence mechanism in Sitefinity, such as how control properties are stored.  Whatever it is, I need to be able to access this from the Admin side when editing the page properties.  Is a custom DB table my only choice?

    I guess the only thing I can think of at this point is to programmatically add my control to a placeholder on the page, after checking to see if it already exists.  Does that sound reasonable?

    Regards,
    Mike
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Sep 2010
    Link to this post
    Hi Mike,

    Control properties are also stored in the database. There is a data layer that persists page properties, but for pages it does not support dynamic fields - if you add a new field it will not be stored. By default these properties are stored insf_CmsPageProps 

    The case with controls added to dynamic pages - ICmsPage object is different - there we get the properties collection and save the values inside a database table - sf_CmsControlProperty

    Options

    1. Create a custom table where you store the pageID and header tags you want to add

    2. Create a custom/user control that you can drop on a page and using a selector you have set which tags you want to add.

    here is a sample

    ublic class InternalPageCustom : InternalPage
    {
        public InternalPageCustom()
        {
     
        }
     
        protected override void AddHeaderControls(Telerik.Cms.IPage page)
        {
            base.AddHeaderControls(page);
            // here you have access to the page - IPage and you can get
            // your custom property with a query and /add your control at runtime.
     
            HtmlMeta meta = new HtmlMeta();
            meta.Name = "keywords";
            meta.Content = "my key works are added";
            this.Header.Controls.Add(meta);
        }
    }



    Sincerely yours,
    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
  7. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    25 Oct 2010
    Link to this post

    I need a little help here...

    What I'm trying to do is programmatically add a control to the page controls from PagePropertiesEdit.ascx.  This is so that I can persist a control property that I will later extract when the page renders. 

    I've defined an interface, ISeoControl that has a single string property, SeoPageTitle.  I've also defined a simple user control that implements ISeoControl.

    In the PagePropertiesEdit.ascx.cs, I add the user control to the Page controls collection, like this:

    protected override void OnInit(EventArgs e)
    {
        Telerik.Cms.CmsManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(CmsManager_Executing);
        base.OnInit(e);
    }
    void CmsManager_Executing(object sender, Telerik.ExecutingEventArgs e)
    {
        if (e.CommandName.Equals("UpdatePage") && !this.WasPageUpdated)
        {
            this.WasPageUpdated = true;
            Guid pageID = ((PageEditor)(this.Parent.Parent.Parent.Parent.Parent.Parent)).SelectedPageId;
            UserControl uc = Page.LoadControl("~/UserControls/PageInfo/SeoPageTitle.ascx") as UserControl;
            CmsManager cmsManager = new CmsManager();
            IPage pageFromDb = cmsManager.GetPage(pageID, true);
            if (uc != null)
            {
                bool seoCtlExists = false;
                foreach (Telerik.Cms.ICmsWebControl webCtrl in pageFromDb.Controls)
                {
                    if (webCtrl.ControlType == typeof(CPi.Web.Public.ISeoControl))
                    {
                        seoCtlExists = true;
                    }
                }
                if (!seoCtlExists)
                {
                    CPi.Web.Public.ISeoControl seoCtl = uc as ISeoControl;
                    seoCtl.SeoPageTitle = "This was set programmatically";
                    pageFromDb = cmsManager.GetPage(pageID, true);
                    // This doesn't seem to add my seoCtl...
                    pageFromDb.Staged.AddControl("HiddenContent", typeof(CPi.Web.Public.ISeoControl), 0, uc.ID);
                }
            }
        }
    }

    But while there seems to be a control there, when I try to get it later, it's null.  It's not apparent to me how my instance of ISeoControl actually gets added, since this overload of AddControl doesn't seem to even have a reference to the instance I just created.

    So how do I add a control (based on ISeoControl) to the page so that I can later inspect one of the control's properties? 

    Btw: This is Sitefinity 3.2

  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    25 Oct 2010
    Link to this post
    Hello Mike,

    Could you try to call SavePage method of CmsManager after you add the control on a page.

    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
Register for webinar
8 posts, 0 answered