More in this section

Forums / Developing with Sitefinity / Page Workflow / Roles

Page Workflow / Roles

6 posts, 0 answered
  1. George
    George  avatar
    204 posts
    Registered:
    05 Sep 2006
    14 Jun 2010
    Link to this post
    Hi Team,

    I am trying to override the behavior of the page workflow as when the current user is an administrator, I want to suppress the workflow and automatically publish the page. For this purpose and following other posts on the forum I did the following:

    Global.asax:

    void Application_Start(object sender, EventArgs e)
    {
        Telerik.Cms.CmsManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(CmsManager_Executing);
    }
     
    void CmsManager_Executing(object sender, Telerik.ExecutingEventArgs e)
    {
        //Check whether the user is creating the page
        if (e.CommandName == "CreatePage")
        {
            Telerik.Cms.ICmsPage pageForEdit = e.CommandArguments as Telerik.Cms.ICmsPage;
            var cmsManager = new CmsManager();
             
            if (pageForEdit != null)
            {
                WorkflowInstance workflow = pageForEdit.GetWorkflow();
                PageWorkflow activity = (PageWorkflow)workflow.Activity;
                if (activity.ApprovalStatus != ApprovalStatus.Published &&
                    UserManager.IsCurrentUserInRole("Administrators"))
                {
                    //set this page as published skipping the approval process
                    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);
                }
                    
            }
        }
    }
     
    private void ExecuteActivity(Guid id, string activityName)
    {
        WorkflowInstance instance = WorkflowRuntime.Instance.GetWorkflow(id);
        if (instance != null)
        {
            var workflow = (PageWorkflow)instance.Activity;
            EventActivity activity = FindActivity(activityName, workflow.Activities);
            if (activity != null)
            {
                activity.Execute(WorkflowRuntime.Instance);
            }
        }
    }
     
    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;
    }


    the problem is at WorkflowInstance workflow = pageForEdit.GetWorkflow(); workflow is null because the pageEdit.WorkflowID is Guid.Empty

    I also tried to check if that's how to handle the update of a page and I noticed that it is not the correct event since it is called only when the CommandName is PublishPage ...

    I don't know whether i am following the best approach to implement this functionality or whether i am only missing something here ...

    Appreciate your support ...




  2. George
    George  avatar
    204 posts
    Registered:
    05 Sep 2006
    14 Jun 2010
    Link to this post
    Any insight on the above issue ?

    Appreciate it
  3. George
    George  avatar
    204 posts
    Registered:
    05 Sep 2006
    14 Jun 2010
    Link to this post
    Ok i figured it out ... i had to use a different approach and add this behavior to the PageViewer
  4. Maciej Skiba
    Maciej Skiba avatar
    15 posts
    Registered:
    28 Sep 2009
    16 Jul 2010
    Link to this post
    Hi,

    Could you post your final code for this issue please? I have got a similar problem - I want to modify Sitefinity's page work flow, because I only need to save changes without publishing and publish them later without whole approval process. I would really appreciate some sample code, because I cannot manage this issue myself.

    Kind regards,
    Maciej.
  5. George
    George  avatar
    204 posts
    Registered:
    05 Sep 2006
    16 Jul 2010
    Link to this post
    You can accomplish this using two approaches. The first one is to automatically send for approval and approve the page thus you will only need to publish it later. The second approach is to modify the page workflow and create your custom workflow. Both will work, but you can use the first one since it's simple and maybe later you will need to reuse the approval process for specific users.

    Here is the code that you can use for the first approach. You need to create a code behind for the Page Viewer located in Sitefinity/Admin/ControlTemplates/Pages/

    PageViewer.ascx.cs

    using System;
    using System.Collections.Generic;
    using Telerik.Cms;
    using Telerik.Security;
    using Telerik.Workflow;

    public partial class Sitefinity_Admin_ControlTemplates_Pages_PageViewer : System.Web.UI.UserControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            //Add any desired condition here. ex: check if the user belong to the admin group.
            
            if (Request.UrlReferrer != null && Request.UrlReferrer.Query.Contains("cmspagemode=edit"))
            {

                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;

                    if (currentPage.Status == PageStatus.New)
                    {
                        currentPage.Publish();
                        manager.SavePage(currentPage);
                    }

                    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();
                            currentPage.UpdateStatus(ApprovalStatus.Approved);
                            manager.SavePage(currentPage);
                            Response.Redirect(Request.RawUrl, false);
                            break;
                        case Telerik.Workflow.ApprovalStatus.ForApproval:
                            ExecuteActivity(currentPage.WorkflowID, "Approve");
                            Nolics.ORMapper.Base.DataConnection.InitWebRequest();
                            currentPage.UpdateStatus(ApprovalStatus.Approved);
                            manager.SavePage(currentPage);
                            Response.Redirect(Request.RawUrl, false);
                            break;
                        case Telerik.Workflow.ApprovalStatus.Approved:
                            break;
                        case Telerik.Workflow.ApprovalStatus.Published:
                            break;
                    }
                }
            }
        }

        private void ExecuteActivity(Guid id, string activityName)
        {
            var instance = WorkflowRuntime.Instance.GetWorkflow(id);
            if (instance != null)
            {
                var workflow = (PageWorkflow)instance.Activity;
                var activity = FindActivity(activityName, workflow.Activities);
                if (activity != null)
                {
                    activity.Execute(WorkflowRuntime.Instance);
                }

            }

        }

        private Telerik.Workflow.EventActivity FindActivity(string name, IList<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;
        }
    }

    PageViewer.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageViewer.ascx.cs" Inherits="Sitefinity_Admin_ControlTemplates_Pages_PageViewer" %>
    <%@Register TagPrefix="cc1" Namespace="Telerik.Cms.Web.UI" Assembly="Telerik.Cms" %>
    <div id="sf_tlsCnt" class="pageTools clearfix">
        <div class="workflow">
            <cc1:PageWorkflowMenu runat="server" id="workflow" Sufix="or"></cc1:PageWorkflowMenu>
            <asp:HyperLink runat="server" ID="editPage" CssClass="CmsButLeft editdark"><asp:Literal ID="editLinkText" runat="server" Text="<%$Resources:EditThisPage %>"></asp:Literal></asp:HyperLink>
            <asp:LinkButton runat="server" ID="deleteVersionButton" CssClass="delete" Text="<%$Resources:DeleteThisVersion %>"></asp:LinkButton>
            <asp:Label runat="server" id="InfoText" Visible="false"></asp:Label>
        </div>
        <div class="links"><asp:HyperLink runat="server" ID="newWindowLink" Text="<%$Resources:ViewLivePageInANewWindow %>" NavigateUrl="" rel="external"></asp:HyperLink></div>
    </div>
    <iframe runat="server" id="viewport" width="1" height="1" style="display:none" name="viewer" frameborder="0" scrolling="yes" />

    Give it a try and it should.

    Cheers,

    George Saadeh
  6. Maciej Skiba
    Maciej Skiba avatar
    15 posts
    Registered:
    28 Sep 2009
    19 Jul 2010
    Link to this post
    Thank you very much for your answer. I will try this solution as soon as I cope with my current tasks.

    Kind regards,
    Maciej.
Register for webinar
6 posts, 0 answered