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

Forums / Developing with Sitefinity / "Publishing" programatically created pages

"Publishing" programatically created pages

4 posts, 0 answered
  1. Andrew
    Andrew avatar
    6 posts
    Registered:
    26 Jul 2007
    14 Aug 2007
    Link to this post
    Hello

    We have workflow enabled on our newly developed site. One part of the site uses a number of automatically created pages. When initially created these pages are at the "Draft" workflow stage and have to be manually moved through the other stages to "Published"

    How do I automatically "Publish" the pages when I am creating them.

    I have tried
    ICmsPage somePage = (ICmsPage)manager.GetPage(somePageID, true); 
    somePage.Publish(); 

    and

    CmsWorkflowService.Instance.PublishPage(somePageID); 

    but neither seems to work.
  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    08 Dec 2016
    15 Aug 2007
    Link to this post
    Hi Andrew,

    As the page workflow is allowed you should pass programmatically through the entire workflow process before calling Publish method. After saving the changes to the page you should send it for approval. To complete the workflow process you need to approve and publish the page.

    I am sending you an example:

        private void CreateUserPage(string pageName, string menuName, out Guid pageId) 
      { 
            // creates user's page 
            CmsManager manager = new CmsManager(); 
            ICmsPage userPage = manager.CreatePage(pageName); 
            pageId = userPage.ID; 
            userPage.Navigable = true
            userPage.MenuName = menuName
     
            manager.SavePage(userPage); 
     
            userPage = (ICmsPage)manager.GetPage(userPage.ID, true); 
     
            Label lbl = new Label(); 
            lbl.ID = "Label1"
            lbl.Text = "My Label"
     
            IStagedPage stage = userPage.Staged.CheckOut(); 
     
            // adds Label control to the page in a container with ID Content 
            stage.AddControl("Content", lbl); 
     
            userPage = (ICmsPage)manager.GetPage(userPage.ID, true); 
     
            // gets template with a name "default" and after that sets it for the newly created page 
            IPageTemplate template = manager.GetTemplate("default"); 
            userPage.Staged.SetTemplate(template.ID, "Blue with right sidebar"); 
     
            userPage = (ICmsPage)manager.GetPage(userPage.ID, true); 
            userPage.Staged.CheckIn(); 
     
            ExecuteActivity(userPage.ID, "SendForApproval"); 
            ExecuteActivity(userPage.ID, "Approve"); 
     
            CmsWorkflowService.Instance.PublishPage(userPage.ID);  
        } 
     
        private EventActivity FindActivity(string name, IList<Activity> activities) 
        { 
            foreach (Activity act in activities) 
            { 
                if ((act is EventActivity) && (((EventActivity)act).CommandName == name)) 
                    return (EventActivity)act; 
                EventActivity ea = FindActivity(name, act.Activities); 
                if (ea != null) 
                    return ea; 
            } 
            return null; 
        } 
     
        private void ExecuteActivity(Guid pageId, string activityName) 
        { 
            WorkflowInstance instance = WorkflowRuntime.Instance.GetWorkflow(pageId); 
            if (instance != null) 
            { 
                PageWorkflow workflow = (PageWorkflow)instance.Activity; 
                EventActivity activity = FindActivity(activityName, workflow.Activities); 
                if (activity != null) 
                { 
                    activity.Execute(WorkflowRuntime.Instance); 
                } 
            } 
        } 

    The negative side of this solution is that the page functions as “Published” but in the grid it is displaying as “Draft”. We will investigate the reasons for this behavior and will contact you right way if we figure out some workaround.

    Hope this helps.

    Kind regards,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Marcel S
    Marcel S avatar
    15 posts
    Registered:
    19 Nov 2009
    20 Jul 2011
    Link to this post
    Very old thread, but anyway.

    I got this code:
    usedPage.Staged.AddControl("cplContent", "GenericContent1", ctlGeneric)
    manager.SavePage(usedPage)
    usedPage = manager.GetPage(userPage.ID, True)
    usedPage.Staged.AddControl("cplDList", "DownloadList1", ctlDList)
     
    userPage = manager.GetPage(userPage.ID, True)
    userPage.Publish()
     
    userPage = manager.GetPage(userPage.ID, True)
    userPage.Staged.CheckIn()
     
    ExecuteActivity(userPage.ID, "SendForApproval")
    ExecuteActivity(userPage.ID, "Approve")
     
    CmsWorkflowService.Instance.PublishPage(userPage.ID)

    ...but the page won't be published. In the pages section, the page is displayed as draft and needs to be approved.
    What's wrong? I just copied the essential part of your code.
    Maybe check in again?

    Edit: I recognized that the variable "instance" in ExecuteActivity is nothing, so nothing will be passed. Any solution for that?

    Regards,
    Marcel
  4. Marcel S
    Marcel S avatar
    15 posts
    Registered:
    19 Nov 2009
    20 Jul 2011
    Link to this post
    I got the final solution:

    Just give the WorkflowID to ExecuteActivity, not the page id.

    usedPage.Staged.AddControl("cplContent", "GenericContent1", ctlGeneric)
    manager.SavePage(usedPage)
    usedPage = manager.GetPage(userPage.ID, True)
    usedPage.Staged.AddControl("cplDList", "DownloadList1", ctlDList)
      
    userPage = manager.GetPage(userPage.ID, True)
    userPage.Publish()
      
    userPage = manager.GetPage(userPage.ID, True)
    userPage.Staged.CheckIn()
      
    ExecuteActivity(userPage.WorkflowID, "SendForApproval")
    ExecuteActivity(userPage.WorkflowID, "Approve")
      
    CmsWorkflowService.Instance.PublishPage(userPage.ID)

    Regards,
    Marcel
Register for webinar
4 posts, 0 answered