public List <PSHWFAssociation> GetWorkflowAssociations(PSHWorkflowDefinition WFDefinition)
        {
            List <PSHWFAssociation> lstWFAssociations = new List <PSHWFAssociation>();

            using (var clientContext = new ClientContext(_PWAUrl))
            {
                var workflowServicesManager = new WorkflowServicesManager(clientContext, clientContext.Web);
                //connect to the subscription service
                var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService();

                var workflowAssociations = workflowSubscriptionService.EnumerateSubscriptionsByDefinition(WFDefinition.WFDefinitionId);
                clientContext.Load(workflowAssociations);
                clientContext.ExecuteQuery();

                if (workflowAssociations.AreItemsAvailable)
                {
                    foreach (WorkflowSubscription ws in workflowAssociations)
                    {
                        PSHWFAssociation objWFDefinition = new PSHWFAssociation()
                        {
                            WFDefinitionId            = ws.DefinitionId,
                            WFAssoId                  = ws.Id,
                            WFAssoName                = ws.Name,
                            WFAssoEventTypes          = ws.EventTypes,
                            WFAssoPropertyDefinitions = ws.PropertyDefinitions
                        };

                        lstWFAssociations.Add(objWFDefinition);
                    }
                }
            }
            return(lstWFAssociations);
        }
        public List <PSHWorkflowDefinition> GetWorkflowDefinitions()
        {
            List <PSHWorkflowDefinition> lstWFDefinitions = new List <PSHWorkflowDefinition>();

            using (var clientContext = new ClientContext(_PWAUrl))
            {
                var workflowServicesManager = new WorkflowServicesManager(clientContext, clientContext.Web);

                // connect to the deployment service
                var workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();
                Web oWeb = clientContext.Web;

                // get all installed workflows
                var publishedWorkflowDefinitions = workflowDeploymentService.EnumerateDefinitions(true);
                clientContext.Load(publishedWorkflowDefinitions);
                clientContext.ExecuteQuery();
                foreach (var pubwfDef in publishedWorkflowDefinitions)
                {
                    Console.WriteLine("Name:{0} \nID: {1}", pubwfDef.DisplayName, pubwfDef.Id.ToString());
                    Console.WriteLine("---------------------------------------");

                    PSHWorkflowDefinition objWFDefinition = new PSHWorkflowDefinition()
                    {
                        WFDefinitionId   = pubwfDef.Id,
                        WFDefinitionXAML = pubwfDef.Xaml,
                        WFName           = pubwfDef.DisplayName
                    };

                    lstWFDefinitions.Add(objWFDefinition);
                }
            }
            return(lstWFDefinitions);
        }
        public void UpdateWorkflowDefinitionXAML(PSHWorkflowDefinition WFDefinition)
        {
            using (var clientContext = new ClientContext(_PWAUrl))
            {
                var workflowServicesManager = new WorkflowServicesManager(clientContext, clientContext.Web);

                // connect to the deployment service
                var workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();

                //Get all the definitions from the Deployment Service, or get a specific definition using the GetDefinition method.
                WorkflowDefinitionCollection wfDefinitions = workflowDeploymentService.EnumerateDefinitions(false);
                clientContext.Load(wfDefinitions, wfDefs => wfDefs.Where(wfd => wfd.Id == WFDefinition.WFDefinitionId));
                clientContext.ExecuteQuery();

                WorkflowDefinition wfDefinition = wfDefinitions.First();
                wfDefinition.DisplayName = WFDefinition.WFName;
                wfDefinition.Xaml        = WFDefinition.WFDefinitionXAML;

                ClientResult <Guid> wfDefId = workflowDeploymentService.SaveDefinition(wfDefinition);
                workflowDeploymentService.PublishDefinition(wfDefinition.Id);
                clientContext.ExecuteQuery();
            }
        }
        public PSHWorkflowDefinition GetWorkflowDefinition(string WFDefinitionName)
        {
            PSHWorkflowDefinition objWFDefinition = null;

            using (var clientContext = new ClientContext(_PWAUrl))
            {
                var workflowServicesManager = new WorkflowServicesManager(clientContext, clientContext.Web);

                // connect to the deployment service
                var workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();
                Web oWeb = clientContext.Web;

                //Get all the definitions from the Deployment Service, or get a specific definition using the GetDefinition method.
                WorkflowDefinitionCollection wfDefinitions = workflowDeploymentService.EnumerateDefinitions(false);
                clientContext.Load(wfDefinitions, wfDefs => wfDefs.Where(wfd => wfd.DisplayName == WFDefinitionName));
                clientContext.ExecuteQuery();

                WorkflowDefinition wfDefinition = wfDefinitions.First();

                ClientResult <Guid> wfDefId = workflowDeploymentService.SaveDefinition(wfDefinition);
                workflowDeploymentService.PublishDefinition(wfDefinition.Id);
                clientContext.ExecuteQuery();

                if (wfDefinition != null)
                {
                    objWFDefinition = new PSHWorkflowDefinition()
                    {
                        WFDefinitionId   = wfDefinition.Id,
                        WFDefinitionXAML = wfDefinition.Xaml,
                        WFName           = wfDefinition.DisplayName
                    };
                }
            }

            return(objWFDefinition);
        }