Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

sitecore6 - How to programmatically approve a content item in Sitecore

Is there a way that I can programmatically approve a content item in Sitecore?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's what you might want to do. Setup a checkbox per site that will allow admins to chose whether they want to use Awaiting Approval or not. An admin will simply check a checkbox to Skip Approval (Draft to Approved) or leave unchecked to maintain workflow (Draft>AA(Approve or reject)>Approved):

  1. In your workflow, under draft state verify that you have a submit command with next state assigned to Awaiting Approval.
  2. Under submit command create a SkipOver action.
  3. Create Class library project in VS, the output will be your assembly.
  4. Create a SkipAction class in your assembly.
  5. Back to your SkipOver action, setup type string like this: Type string [shared]: YourAssembly.SkipAction,YourAssembly
  6. Goto sitecore>templates, create SkipApproval template
  7. Add template section to template called WorkflowSettings.
  8. Add template field to WorkflowSettings, select checkbox under type.
  9. Save and publish template item.
  10. In my case we add Skipapproval to a sectionsettings item which is then assigned to out standard template for new sites. So for each site that is created, we add sectionsettings template and the template will have the SkipApproval checkbox.
  11. Add this code to your SkipAction class:

    public class SkipAction
    {
        bool isSiteSettingsReached = false;
    
        /// <summary>
        /// This method facilitates grabbing of the skipapproval setting from setting>sectionsettings
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldId"></param>
        /// <returns></returns>
        protected bool GetSkipSetting(Item item, string fieldId)
        {
            bool result = false;
    
            if (item.ID.Equals(null) || item.ID.Equals(ItemIDs.ContentRoot) || item.ID.Equals(ItemIDs.RootID))
            {
                result=false;
            }
    
            if (isSkipApprovalChecked(item, fieldId))
            {
                result = true;
            }
    
            if (!isSkipApprovalChecked(item, fieldId))
            {
                result = false;
            }
    
            if (!isSiteSettingsReached)
            {
                result=GetSkipSetting(item.Parent, fieldId);
            }
    
            return result;
        }
    
        /// <summary>
        /// This method is initialized when skipaction is called 
        /// </summary>
        /// <param name="args"></param>
        public void Process(WorkflowPipelineArgs args)
        {
            var contentItem = args.DataItem;
            var actionItem = args.ProcessorItem.InnerItem;
    
            var parameters = WebUtil.ParseUrlParameters(actionItem["parameters"]);
            var nextStateId = parameters["nextstateid"];
            var skipFieldId = parameters["skipfieldid"];
    
            if(nextStateId.IsNullOrEmpty() ||
               skipFieldId.IsNullOrEmpty())
                return;
    
            bool skip = GetSkipSetting(contentItem, skipFieldId);
    
            Sitecore.Data.Database web = Sitecore.Configuration.Factory.GetDatabase("web");
            Sitecore.Data.Database production = Sitecore.Configuration.Factory.GetDatabase("production");
    
            if (skip)
            {
                contentItem.PerformTransition(nextStateId, "auto skip");
    
                using (new Sitecore.SecurityModel.SecurityDisabler())
                {                       
                    publishTo(web, contentItem);
                    publishTo(production, contentItem);                    
                }
    
                contentItem.Locking.Unlock();
                args.AbortPipeline();
            }          
        }
    
        /// <summary>
        /// this method is used to publish to environ by param 
        /// </summary>
        /// <param name="targetToPublish"></param>
        /// <param name="item"></param>
        private void publishTo(Sitecore.Data.Database targetToPublish, Item item)
        {
            Sitecore.Data.Database sourceDB = Sitecore.Configuration.Factory.GetDatabase("master");
    
            //// set publish options
            Sitecore.Publishing.PublishOptions myOptions = new Sitecore.Publishing.PublishOptions(
                sourceDB,
                targetToPublish,
                Sitecore.Publishing.PublishMode.Smart,
                item.Language,
                DateTime.Now);
    
            myOptions.RootItem = item;
            myOptions.Deep = false;
            Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(myOptions);
            Sitecore.Jobs.Job myPublishJob = publisher.PublishAsync();
            myPublishJob.Start();
    
        }  
        /// <summary>
        /// This method check the actual skip approval field in settings>sectionsettings
        /// </summary>
        /// <param name="item"></param>
        /// <param name="fieldId"></param>
        /// <returns></returns>
        public bool isSkipApprovalChecked(Sitecore.Data.Items.Item item, string fieldId)
        {           
            if (item.HasChildren) 
            {
                Sitecore.Data.Items.Item settingsItem = item.Axes.GetChild("Settings");
    
                if (settingsItem == null)
                {
                    return false;
                }
                else 
                {
                    isSiteSettingsReached = true;
                    Sitecore.Data.Items.Item sectionsettingsItem = settingsItem.Axes.GetChild("SectionSettings");
    
                    if (sectionsettingsItem.DisplayName == "SectionSettings" && sectionsettingsItem[fieldId] == "1")
                    {
                        return true;
                    }
                    else 
                    {
                        return false;                    
                    }   
                }             
            }
            return false;                
        }
    }
    

    }

  12. Go Back to your SkipOver action, add parameters like this (nextstateid = Approval State id, skipfieldid = item id of skipapproval checkbox in your template):

Parameters [shared]: nextstateid={D0F57FA8-F472-4332-89D9-E429CD111E50}&skipfieldid={XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

  1. Compile and deploy, fire up sitecore, as an admin goto site in question, check "Skip Approval", logout, login under user with rights to edit. Make a change, goto review tab> submit, verify you can still edit because you've been approved.
  2. Goto site in question, uncheck "Skip Approval", logout, login under user with rights to edit. Make a change, goto review tab> submit, verify that item is locked and Awaiting Approval.

Thanks, c


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...