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

Forums / Developing with Sitefinity / Auto Publish page when Approved (Workflow Enabled)

Auto Publish page when Approved (Workflow Enabled)

7 posts, 0 answered
  1. Scott Harrington
    Scott Harrington avatar
    28 posts
    Registered:
    21 Sep 2009
    12 May 2010
    Link to this post
    Hello,

    We've setup our roles that say, all 'Editors' have Approval and Publishing rights.  With that said, is there a way to automatically publish the page when the Editor Approves a page?  So basically this would just skip the Publish screen when Approve is clicked.  But if the page is declined, we, obviously, don't want the page to be published.  I've tried the following code in the pageveiwer.ascx page where I have enabled workflow emails but I am still sent to the publish page after approval (Line 22-24 was the attempt at forcing a publish):

     

    01.void PageWorkflowMenu_Command(object sender, CommandEventArgs e) 
    02.{
    03.    WorkflowInstance workflow = ((WorkflowMenu)sender).GetWorkflow(); 
    04.    if (workflow != null) 
    05.    {         
    06.       List <EventActivity> commands = new List<EventActivity>(); 
    07.       this.LoadCommands(commands, workflow.Activity.Activities); 
    08.       int idx = int.Parse((string)e.CommandArgument); 
    09.   
    10.       if((commands.Count > idx) && !(commands[idx].CommandName.Equals("Publish"))) 
    11.       {    
    12.           Telerik.Cms.CmsManager manager = new Telerik.Cms.CmsManager(); 
    13.           Telerik.Cms.ICmsPage page = manager.Provider.GetPageByWorkflow(workflow.ID);
    14.               
    15.           switch(commands[idx].CommandName) 
    16.           {
    17.               case "SendForApproval":                 
    18.                   //...Do Something
    19.                   break; 
    20.               case "Approve":
    21.                  //Thought this would force a publish, 
                                  //still sent to the Publish page in workflow. 
    22.                  page.Staged.CheckIn(); 
    23.                  page.Publish();
    24.                  manager.SavePage(page);
    25.                  break; 
    26.               case "Decline":
    27.                  //...Do Something
    28.                  break;
    29.             }
    30.        }
    31.    
    32.

    Any suggestions would be great.  Thanks a lot for your help!

     

     

     

     

     

     

     

     

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 May 2010
    Link to this post
    Hello Scott Harrington,

    Below is a sample code that illustrates how to skip the workflow steps

    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 id, string activityName)
            {
                WorkflowInstance instance = WorkflowRuntime.Instance.GetWorkflow(id);
                if (instance != null)
                {
                    PageWorkflow workflow = (PageWorkflow)instance.Activity;
                    EventActivity activity = FindActivity(activityName, workflow.Activities);
                    if (activity != null)
                    {
                        activity.Execute(WorkflowRuntime.Instance);
                    }
      
                }
      
            
      
      
            void button1_Click(object sender, EventArgs e)
            {
                CmsManager cmsManager = new CmsManager();
                //WOKING WITH A SINGLE PAGE JUST FOR DEMONSTRATION
                ICmsPage pageForEdit = (ICmsPage)cmsManager.GetPage(new Guid("164C8A56-4A6B-9D9B-453E-52B4090AEABD"));
                WorkflowInstance workflow = pageForEdit.GetWorkflow(); 
                PageWorkflow activity = (PageWorkflow)workflow.Activity; 
                if (activity.ApprovalStatus != ApprovalStatus.Published) 
                {
                    // SEND THIS PAGE FOR APPROVAL - SKIPPING THE APPROVAL STATUSES
                    // FROM THE USER INTERFACE
                    ICmsPage stagedPage = (ICmsPage)cmsManager.GetPage(pageForEdit.ID, true);
                    ExecuteActivity(stagedPage.WorkflowID, "SendForApproval");
                    Nolics.ORMapper.Base.DataConnection.InitWebRequest();
                    ExecuteActivity(stagedPage.WorkflowID, "Approve");
                    Nolics.ORMapper.Base.DataConnection.InitWebRequest();
                    cmsManager.SavePage(stagedPage);
       
                
           }

    The code you use is supposed to catch the workflow steps, but it does not skip the steps.

    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. Scott Harrington
    Scott Harrington avatar
    28 posts
    Registered:
    21 Sep 2009
    12 May 2010
    Link to this post
    Great Ivan, thanks for the fast reply, this looks like a great solution!  In what page would this code snippit be placed?  PageViewer.ascx?  Thanks again!
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 May 2010
    Link to this post
    Hello Scott Harrington,

    PageViewer.ascx.cs should be fine.

    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.
  5. Scott Harrington
    Scott Harrington avatar
    28 posts
    Registered:
    21 Sep 2009
    12 May 2010
    Link to this post
    Great thank you!
  6. Scott Harrington
    Scott Harrington avatar
    28 posts
    Registered:
    21 Sep 2009
    12 May 2010
    Link to this post
    I put the code in place, but I am not sure how to attach a button click to the workflow:
    <cc1:pageworkflowmenu runat="server" id="workflow" sufix="or"></cc1:pageworkflowmenu>
    Really, all I can see is connecting to the command of the cc1:pagworkflowmenu, which I am already doing in the first code snippit I had attached earlier.  Any ideas? 

    Thanks!
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 May 2010
    Link to this post
    Hi Scott Harrington,

    Below is a sample code that illustrates how to send the current page from draft directly to published. If necessary you could move the code that we discussed in the previous post in another case of the switch block.

    private Telerik.Workflow.EventActivity FindActivity(string name, System.Collections.Generic.IList<Telerik.Workflow.Activity> activities)
       {
           foreach (Telerik.Workflow.Activity act in activities)
           {
               if ((act is Telerik.Workflow.EventActivity) && (((Telerik.Workflow.EventActivity)act).CommandName == name))
                   return (Telerik.Workflow.EventActivity)act;
               Telerik.Workflow.EventActivity ea = FindActivity(name, act.Activities);
               if (ea != null)
                   return ea;
           }
           return null;
       }
     
       private void ExecuteActivity(Guid id, string activityName)
       {
           Telerik.Workflow.WorkflowInstance instance = Telerik.Workflow.WorkflowRuntime.Instance.GetWorkflow(id);
           if (instance != null)
           {
               Telerik.Cms.PageWorkflow workflow = (Telerik.Cms.PageWorkflow)instance.Activity;
               Telerik.Workflow.EventActivity activity = FindActivity(activityName, workflow.Activities);
               if (activity != null)
               {
                   activity.Execute(Telerik.Workflow.WorkflowRuntime.Instance);
               }
     
           }
     
       
     
       protected override void OnPreRender(EventArgs e)
       {
           base.OnPreRender(e);
           var manager = new Telerik.Cms.CmsManager();
           Guid pageID = ((Telerik.Cms.Web.UI.PageEditor)(this.Parent.Parent.Parent.Parent.Parent.Parent)).SelectedPageId;
           var currentPage = (Telerik.Cms.ICmsPage)manager.GetPage(pageID, true);
           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;
               }
           }
       }


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