/// <summary>
        /// Gets the approval workflow settings.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="taskName">Name of the task.</param>
        /// <returns>
        /// The dictionary of workflow settings
        /// </returns>
        /// <exception cref="Microsoft.SharePoint.SPException"></exception>
        private static Dictionary <string, string> GetApprovalWorkflowSettings(SPWeb web, string taskName)
        {
            Dictionary <string, string> payLoad = new Dictionary <string, string>();

            string value = string.Empty;

            try
            {
                value = GlobalSettingsHelper.GetValue(ListConstants.ApprovalWorkflowApproversSharePointGroup);

                string approvers = ParseContentApproverGroups(web, value);

                if (approvers.Length == 0)
                {
                    throw new SPException("No SharePoint Approver Groups found");
                }

                payLoad.Add("Approvers", approvers);
            }
            catch (Exception ex)
            {
                throw new SPException(string.Format("Error in SharePoint Approver Group resolution{0}{1}", Environment.NewLine, ex.Message), ex);
            }

            try
            {
                value = GlobalSettingsHelper.GetValue(ListConstants.ApprovalWorkflowDefaultDurationDays);
                if (!string.IsNullOrEmpty(value))
                {
                    payLoad.Add("Duration", value);
                }
            }
            catch
            {
            }

            value = ResourcesHelper.GetLocalizedString("workflow_ApprovalTaskDefaultTitle");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskTitle", string.Format(value, taskName));
            }

            value = ResourcesHelper.GetLocalizedString("workflow_ApprovalTaskAssignedEmailSubject");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskAssignmentEmailSubject", string.Format(value, taskName));
            }

            value = ResourcesHelper.GetLocalizedString("workflow_ApprovalTaskAssignedEmailBodyHtml");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskAssignmentEmailBodyHtml", value);
            }

            value = ResourcesHelper.GetLocalizedString("workflow_ApprovalTaskCancelledEmailSubject");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskCancellationEmailSubject", string.Format(value, taskName));
            }

            value = ResourcesHelper.GetLocalizedString("workflow_ApprovalTaskCancelledEmailBodyHtml");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskCancellationEmailBodyHtml", value);
            }

            value = ResourcesHelper.GetLocalizedString("workflow_ApprovalTaskOverdueEmailSubject");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskOverdueEmailSubject", string.Format(value, taskName));
            }

            value = ResourcesHelper.GetLocalizedString("workflow_ApprovalTaskOverdueEmailBodyHtml");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskOverdueEmailBodyHtml", value);
            }

            value = ResourcesHelper.GetLocalizedString("workflow_approvaltaskstatus_completed");
            if (!string.IsNullOrEmpty(value))
            {
                payLoad.Add("TaskCompletionStatus", value);
            }

            payLoad.Add("EndonFirstApproval", "true");
            payLoad.Add("EnableContentApproval", "true");

            return(payLoad);
        }
        /// <summary>
        /// Ensures the approval workflow on list.
        /// </summary>
        /// <param name="web">The web.</param>
        /// <param name="list">The list.</param>
        /// <param name="events">The events.</param>
        /// <param name="useSamlWorkflow">Whether to use the SAML Approval workflow</param>
        /// <returns>
        /// The workflow subscription id
        /// </returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static Guid EnsureApprovalWorkflowOnList(SPWeb web, SPList list, List <WorkflowStartEventType> events, bool useSamlWorkflow)
        {
            Guid subscriptionId = Guid.Empty;

            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            // Remove existing 2010 workflow associations
            SPWorkflowAssociationCollection existingWorkflows = list.WorkflowAssociations;

            if (existingWorkflows != null)
            {
                for (int i = 0; i < existingWorkflows.Count; i++)
                {
                    list.WorkflowAssociations.Remove(existingWorkflows[i]);
                }
            }

            //SPFeature customWorkflowActivitiesFeature = web.Features[customWorkflowActivitiesFeatureId];

            //if (customWorkflowActivitiesFeature == null)
            //{
            //    customWorkflowActivitiesFeature = web.Features.Add(customWorkflowActivitiesFeatureId);
            //}

            SPFeature customWorkflowFeature = web.Features[customWorkflowFeatureId];

            if (customWorkflowFeature == null)
            {
                customWorkflowFeature = web.Features.Add(customWorkflowFeatureId);
            }

            if (customWorkflowFeature != null)
            {
                if (!list.EnableModeration || !list.ForceCheckout)
                {
                    list.EnableModeration = true;
                    list.ForceCheckout    = true;
                    list.EnableVersioning = true;
                    list.Update();
                }

                Dictionary <string, string> workflowPropertyData = GetApprovalWorkflowSettings(web, list.Title);

                SPList taskList = EnsureWorkflowTaskList(web, "/lists/workflowtasks", string.Empty);

                SPList historyList = EnsureWorkflowHistoryList(web, "/workflowhistory", string.Empty);

                if (taskList != null && historyList != null)
                {
                    bool workflowTaskContentTypeAssociated = false;
                    foreach (SPContentType contentType in taskList.ContentTypes)
                    {
                        if (contentType.Parent.Id.ToString().Equals(ContentTypeWorkflowTaskSharePoint2013, StringComparison.OrdinalIgnoreCase))
                        {
                            workflowTaskContentTypeAssociated = true;
                            break;
                        }
                    }

                    if (!workflowTaskContentTypeAssociated)
                    {
                        SPContentType           wftaskContentType = default(SPContentType);
                        SPContentTypeCollection contentTypes      = web.ContentTypes.Count == 0 ? web.Site.RootWeb.ContentTypes : web.ContentTypes;

                        wftaskContentType = contentTypes.Cast <SPContentType>().FirstOrDefault <SPContentType>(c => c.Id.ToString().Equals(ContentTypeWorkflowTaskSharePoint2013, StringComparison.OrdinalIgnoreCase));

                        if (wftaskContentType != null)
                        {
                            taskList.ContentTypes.Add(wftaskContentType);
                        }
                    }

                    string displayName = ResourcesHelper.GetLocalizedString("workflow_approval_instancename");

                    Guid workflowDefId = useSamlWorkflow ? approvalSamlWorkflowDefinitionId : approvalWorkflowDefinitionId;

                    subscriptionId = EnsureWorkflowOnList(web, list, workflowDefId, displayName, events, taskList, historyList, workflowPropertyData, false);

                    EnableWorkflowsRunAsAnApp(web);

                    SubscribeToEventReceivers(list, false);
                }
            }

            return(subscriptionId);
        }