3

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

1
  • Do you mean approving content in a workflow to move to the next state?
    – user623892
    Jan 19, 2012 at 14:08

2 Answers 2

3

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

0

Here is a blog that (quite descriptively) tells you how to change workflow state for items programmatically: http://www.cognifide.com/blogs/sitecore/changing-workflow-state-of-sitecore-items-programmatically/ Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.