More in this section

Forums / Developing with Sitefinity / Creating 'advanced' pages automatically

Creating 'advanced' pages automatically

11 posts, 0 answered
  1. Doug
    Doug avatar
    34 posts
    Registered:
    20 Apr 2010
    01 Jun 2010
    Link to this post
    By 'advanced'  I mean creating pages automatically that have a template applied to them and are populated with controls.  Now I know that I can create basic pages with the CmsManager but I am not seeing how to apply a template.  Further, assume that on this template there are three place holders for controls, how would I go about pragmatically placing a Generic Content control, for example, into Placeholder 1?  Further how would I go about populating the Genrice Control with some copy?

    Thanks.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Jun 2010
    Link to this post
    Hi Doug,

    Please take a look at How to create predefined template pages that contain controls placed on the content areas

    Kind regards,
    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. Doug
    Doug avatar
    34 posts
    Registered:
    20 Apr 2010
    01 Jun 2010
    Link to this post
    Hi Ivan,
       Thanks for that, it looks pretty straight forward!  Just an FYI there is an error in that code snippet: in the InitializePage method of the PageInitializer class IPageTemplate is defined as 'SavepageTemplate' but it is later referenced as 'pageTemplate'.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Jun 2010
    Link to this post
    Hello Doug,

    The FormatCodeBlock has appended  "Save" link to the object. This is not coming from the code. I will paste the snippet again.

    Kind regards,
    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. Doug
    Doug avatar
    34 posts
    Registered:
    20 Apr 2010
    02 Jun 2010
    Link to this post
    Hi Ivan,
       Thanks for that code block it got me on the right track although I actually had to use the ICmsPage interface since I need access to additional properties of the page, specifically URL's which has lead me to a problem.  All of the pages I am creating automatically are going underneath a parent named 'Archive' and my desired URL would be something like: /archive/May/05/Article/ however this does not work although I suspect it has to do with the absence of an .aspx extension in the query string.   I looked through the web.config and while I found Rewrite rules for News, Events, and things of that nature I could not find one that applied to pages. 

    Is it possible to do what I am looking for or am I stuck with needing an .aspx extension for the time being?  Thanks.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    02 Jun 2010
    Link to this post
    Hi Doug,

    The extenssionles url should not be a problem. Anyway, you may use URL Rewriting/Redirecting with pages. Please take a look at our UserManual, section URL Rewriting page 195

    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.
  7. Doug
    Doug avatar
    34 posts
    Registered:
    20 Apr 2010
    02 Jun 2010
    Link to this post
    Hi Ivan,
         Thanks for the link. I fear that I am still having a few problems though. Rather then try and explain it please take a look at the code snippet below:

     

    private void BuildPage()
    {
        CmsManager mgr = new CmsManager();
         
        ICmsPage page = mgr.CreatePage(RemoveWhiteSpaceFromString(this.textTitle.Text), new Guid("2555B702-611D-4575-9D93-9A4C0C5D2004"));
        IPageTemplate template = mgr.GetTemplate("ShowMaster");
        page.Staged.SetTemplate(template.ID, "ShowTemplate");
        page.AddUrl(CreatePageURL());
        page.MenuName = CreateMenuLabel();
        mgr.SavePage(page);
        AddControlsToPage(mgr, page);
        page.Publish();
         
    }
    private void AddControlsToPage(CmsManager Manager, ICmsPage Page)
    {
        Page = (ICmsPage)Manager.GetPage(Page.ID, true);
        Page.Staged.AddControl("News", "NewsControl", BuildGenericContentControl(this.radEditorShow.Content));
        Manager.SavePage(Page);
        Page.Publish();
        Page = (ICmsPage)Manager.GetPage(Page.ID, true);
        Page.Staged.AddControl("ShowSummary", "SummaryControl", BuildGenericContentControl(this.radEditorShowSummary.Content));
        Manager.SavePage(Page);
        Page.Publish();
        Page = (ICmsPage)Manager.GetPage(Page.ID, true);
        Page.Staged.AddControl("Video", "VideoControl", BuildGenericContentControl(this.textBrigthCoveEmbed.Text));
        Manager.SavePage(Page);
        Page.Publish();
    }

    Here is where I am having a problems:

    Once this code has complated execution the page that is created is in a 'Locked' state and I am unable to change that.   After reading this thread I assumed that since I needed to save the page each time I added a control to to it that I also needed to publish the page each time (Note: this theory is the only reason for the ICmsPage cast since IPage does not contain the Publish method otherwise the AddControls method would simply work with IPage.)  How do I go about pragmatically approving the page?

    Next I am still having an issue with URLs.  The AddUrl method is adding the correct URL to the page in the format of: ~/archive/MM/dd/yyyy/Title/ the problem is that even after I publish the page manually, while I can hit the actual page at ~/archive/Title.aspx the friendly URL above returns a 404.  I have tried making it the Default URL on the pages properties section but that didn't do anything.   Is there something I am missing? 

    Finally, is there a way to add a custom user control to a page, at runtime, similar to the methodology I am using above with the generic content control?

    Thanks for your time.

    P.S.  There is no voodoo in any of the method calls that I have not shown.  For example BuildGenericContentControl simply creates an instance of GenericContent class, populates the Content property, and returns it.  The other two methods simply return string values.
  8. Doug
    Doug avatar
    34 posts
    Registered:
    20 Apr 2010
    03 Jun 2010
    Link to this post
    Hi Ivan,
        After some more Google searching I found an article over on SitefinityWatch that helped me resolve most of these issues.  The only question I still have is about loading custom user controls at run time.  It appears that it is possible but only when following a "pluggable module" approach to development and not an Intra-Site module.  Is this correct?

    Thanks.
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    03 Jun 2010
    Link to this post
    Hello Doug,

    You have to save the control and then call Staged.CheckIn(); Afterwords you have to call Manager.SavePage(page);

    Manager.SaveControl(cntrl);
    ((ICmsPage)Manager.GetPage(page.ID, true)).Staged.CheckIn();
    Manager.SavePage(page);

    If  you are using worklow you have to go through the workflow steps and approve/publish a page.

    Telerik.Workflow.WorkflowInstance wInstance = currentPage.GetWorkflow();
    if (wInstance != null)
    {
        var activity = (Telerik.Cms.PageWorkflow)wInstance.Activity;
        switch (activity.ApprovalStatus)
        {
            case Telerik.Workflow.ApprovalStatus.Draft:
     
                ExecuteActivity(currentPage.WorkflowID, "SendForApproval");
                Nolics.ORMapper.Base.DataConnection.InitWebRequest();
                ExecuteActivity(currentPage.WorkflowID, "Approve");
                Nolics.ORMapper.Base.DataConnection.InitWebRequest();
                ExecuteActivity(currentPage.WorkflowID, "Publish");
                Nolics.ORMapper.Base.DataConnection.InitWebRequest();
                manager.SavePage(currentPage);
                break;
            case Telerik.Workflow.ApprovalStatus.ForApproval:
               
                break;
            case Telerik.Workflow.ApprovalStatus.Approved:
               
               break;
            case Telerik.Workflow.ApprovalStatus.Published:
                break;
        }
    }

    You may get 404 if there is no such path in your SiteMap stricture -/archive/MM/dd/yyyy/Title/

    To add a user control you can use again AddControl method

    staged.AddControl("Holder", "~/UserControl.ascx", 0, "UserControl1");


    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.
  10. Doug
    Doug avatar
    34 posts
    Registered:
    20 Apr 2010
    03 Jun 2010
    Link to this post
    Thanks Ivan.  One final question that I wasn't able to answer from the API documentation or from various google searches is related to URL's.  As shown earlier I am making a call to AddUrl() and, while it is unlikely that an additional URL will be duplicated, I ran into the exception several times throughout my testing due to a key conflict. Now I could simply write some data access code to go out and look at the sf_CmsUrl table but surely Sitefinity already provides this mechanisim.  Unfortunately upon examination of the CmsUrl class it doesn't appear that it exposes a method designed to do this. Does this exist in another location or do I need roll my own?

    Thanks.
    -Doug
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    03 Jun 2010
    Link to this post
    Hi Doug,

    There is a method that checks for existing urls but it cannot be used directly. You can use the code below

    protected void Button1_Click(object sender, EventArgs e)
        {
            var cmsManager = new CmsManager();
            ICmsPage page = (ICmsPage)cmsManager.GetPage(new Guid("bb212185-e5b4-4321-8bb7-09a475961aa4"), true);
            string insertUrl = "~/test";
            if (CheckList(insertUrl, page))
            {
                page.AddUrl(insertUrl);
                cmsManager.SavePage(page);
                Response.Write("the link was added");
            }
            else
            {
                Response.Write("the link exists");
            }
       }
        public bool CheckList(string candidate, ICmsPage page)
        {
            IList<ICmsUrl> urls = page.GetUrls();
            IList<ICmsUrl> dummyList = new List<ICmsUrl>();
            foreach (ICmsUrl url in urls)
            {
                if (url.Url.Equals(candidate, StringComparison.OrdinalIgnoreCase))
                {
                    dummyList.Add(url);
                }
            }
            if (dummyList.Count > 0)
                return false;
            return true;
        }


    Regards,
    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
11 posts, 0 answered