public static Guid AddWorkflowDefinition(this Web web, WorkflowDefinition definition, bool publish = true)
        {
            var servicesManager   = new WorkflowServicesManager(web.Context, web);
            var deploymentService = servicesManager.GetWorkflowDeploymentService();

            WorkflowDefinition def = new WorkflowDefinition(web.Context);

            def.AssociationUrl = definition.AssociationUrl;
            def.Description    = definition.Description;
            def.DisplayName    = definition.DisplayName;
            def.DraftVersion   = definition.DraftVersion;
            def.FormField      = definition.FormField;
            def.Id             = definition.Id != Guid.Empty ? definition.Id : Guid.NewGuid();
            foreach (var prop in definition.Properties)
            {
                def.SetProperty(prop.Key, prop.Value);
            }
            def.RequiresAssociationForm = definition.RequiresAssociationForm;
            def.RequiresInitiationForm  = definition.RequiresInitiationForm;
            def.RestrictToScope         = definition.RestrictToScope;
            def.RestrictToType          = definition.RestrictToType;
            def.Xaml = definition.Xaml;

            var result = deploymentService.SaveDefinition(def);

            web.Context.ExecuteQueryRetry();

            if (publish)
            {
                deploymentService.PublishDefinition(result.Value);
                web.Context.ExecuteQueryRetry();
            }
            return(result.Value);
        }
        public static Guid AddWorkflowDefinition(this Web web, WorkflowDefinition definition, bool publish = true)
        {
            var servicesManager = new WorkflowServicesManager(web.Context, web);
            var deploymentService = servicesManager.GetWorkflowDeploymentService();

            WorkflowDefinition def = new WorkflowDefinition(web.Context);
            def.AssociationUrl = definition.AssociationUrl;
            def.Description = definition.Description;
            def.DisplayName = definition.DisplayName;
            def.DraftVersion = definition.DraftVersion;
            def.FormField = definition.FormField;
            def.Id = definition.Id != Guid.Empty ? definition.Id : Guid.NewGuid();
            foreach (var prop in definition.Properties)
            {
                def.SetProperty(prop.Key,prop.Value);
            }
            def.RequiresAssociationForm = definition.RequiresAssociationForm;
            def.RequiresInitiationForm = definition.RequiresInitiationForm;
            def.RestrictToScope = definition.RestrictToScope;
            def.RestrictToType = definition.RestrictToType;
            def.Xaml = definition.Xaml;

            var result = deploymentService.SaveDefinition(def);

            web.Context.ExecuteQueryRetry();

            if (publish)
            {
                deploymentService.PublishDefinition(result.Value);
                web.Context.ExecuteQueryRetry();
            }
            return result.Value;
        }
        protected virtual void MapProperties(WorkflowDefinition workflow, SP2013WorkflowDefinition definition)
        {
            if (!string.IsNullOrEmpty(definition.RestrictToType))
            {
                workflow.RestrictToType = definition.RestrictToType;
            }

            if (!string.IsNullOrEmpty(definition.RestrictToScope))
            {
                workflow.RestrictToScope = definition.RestrictToScope;
            }

            foreach (var prop in definition.Properties)
            {
                workflow.SetProperty(prop.Name, prop.Value);
            }
        }
        protected virtual void MapProperties(WorkflowDefinition workflow, SP2013WorkflowDefinition definition)
        {
            if (!string.IsNullOrEmpty(definition.RestrictToType))
                workflow.RestrictToType = definition.RestrictToType;

            if (!string.IsNullOrEmpty(definition.RestrictToScope))
                workflow.RestrictToScope = definition.RestrictToScope;

            foreach (var prop in definition.Properties)
                workflow.SetProperty(prop.Name, prop.Value);
        }