Monday, January 11, 2016

Custom Workflows in Sitecore

Purpose
Business workflow
  • Content Author- Submit
  • Content Reviewer-Approve/Reject 
  • Final State -Approved.

Technical Workflow 
  • State
  • Command 
  • Action and Validation Action.

Workflow Snapshot


How to create it?
  1. Open Sitecore in desktop mode and go to content Editor Screen. Go to Workflows and create Workflow with State, Command and Action Template.
  2. As per above workflow, create Draft State with Submit action. On Submit action select Data-> Next State Awaiting Approval.
  3. Create Awaiting Approval State with Two command Approve and Reject. Under Approve create three action - Auto publish(Type String : Sitecore.Workflows.Simple.PublishAction, Sitecore.Kernel, Param: Deep=1). SyncCustomDb Action(Reference .net bin dll) and Validation Action to report error , warnings to business users.[Data->Type->Sitecore.Workflows.Simple.ValidatorsAction, Sitecore.Kernel]
  4. What next, you tick mark.suppress comment if you want to do so.
  5. Create final Approved State within Awaiting Approval state. For Final Approved state ensure to mark Final checkbox .
  6. For Reject State select Next state to Draft state.
Template used
  • State
  • Action
  • Command
  • Validation Action
Important Library

  • Sitecore.Workflows.Simple.ValidatorsAction, Sitecore.Kernel
  • Sitecore.Workflows.Simple.PublishAction, Sitecore.Kernel

Publish Parameters
Publish action accepts 6 parameters:
  1. "deep" - controls whether children of the current item will be published. Possible values: "1" - children of the current item will be published; all other values - children of the current item will not be published.
  2. "related" - controls whether related items of the current item will be published. Possible values: "1" - related items of the current item will be published; all other values - related items of the current item will not be published.
  3. "targets" - comma (,) separated list of database names that item will be published to. Note, that this parameter does not expect a list of publishing target names, it expects list of database names.
  4. "alllanguages" - controls whether current item will be published in all languages that exist in source database. Possible values: "1" - current item will be published in all languages that exist in source database; all other values - code uses values of other parameters to determine languages in which current item will be published.
  5. "languages" - comma (,) separated list of languages in which current item will be published.
  6. "itemlanguage" - controls whether current item will be published in its current language. Possible values: "1" - current item will be published in its current language; "0" - current item will not be published in its current language; all other values - current item will published in its current language. Note that even if value of this parameter is "0", current item will still be published in its current language if current language of the item is in "languages" list.
Querystring parameter
deep=1&related=1&alllanguages=1

Auto Publish


Custom Workflow Action: SyncCustomDB
Suppose you want to sync your sitecore CMS data with custom database for Ecommerce database products or promotions if your enterprise solution doesn't provide that. The approach can vary with respect to problem and business requirement.

Here is the quick implementation.
Create Library with Workflow Pipeline processor and create new process .This process requires workflowPipelineArgs
Assign this workflow to the page item and then when business user approves the sitecore content for given item -template the action event will be performed to sync custom database. These can be anything, we can even replace this with any functionality such email notification or anything that needs to be done in case of approve content.


CustomWorkflowActionEvents

using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Workflows.Simple;
using Sitecore.Diagnostics;
using Sitecore.Data.Items;
using Sitecore.Configuration;
using Sitecore.Data;

namespace Demo.Sitecore.Practical.CustomWorkflowActionEvents
{
    public class CustomDatabaseSync
    {
        public void Process(WorkflowPipelineArgs args)
        {
            using (var db = new SomeCustomDBContext())
            {
                Item pageItem = args.DataItem;

                if (pageItem.TemplateName == "Promotion")
                {
                    var tbPromotion = db.Promotions.Where(x => x.PromotionID == workFlowItem.ID.Guid);

                    if (tbPromotion.Any())
                    {
                        var tb = tbPromotion.FirstOrDefault();
                        tb.PromotionName = workFlowItem.GetFieldValue("PromotionName");
                        tb.StartDate = workFlowItem.GetDateTime("StartDate");
                        tb.EndDate = workFlowItem.GetDateTime("EndDate");
                        tb.IsEnabled = true;
                        tb.UpdatedDate = DateTime.Now;
                        db.Entry(tb).State = System.Data.EntityState.Modified;
                    }
                    else
                    {
                        db.Promotions.Add(new Promotion
                        {
                            PromotionID = workFlowItem.ID.Guid,
                            PromotionName = workFlowItem.GetFieldValue("PromotionName"),
                            StartDate = workFlowItem.GetDateTime("StartDate"),
                            EndDate = workFlowItem.GetDateTime("EndDate"),
                            IsEnabled = true,
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now
                        });
                    }
                }               
                db.SaveChanges();
            }
        }
    }
}

Snapshot


References
http://www.sitecore.net/learn/blogs/technical-blogs/reinnovations/posts/2014/03/auto-publish-workflow-action-updates.aspx


No comments :