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 void CreateWorkflowAssociation(PSHWFAssociation WFAssociation)
        {
            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(WFAssociation.WFDefinitionId);
                clientContext.Load(workflowAssociations);
                clientContext.ExecuteQuery();

                if (workflowAssociations.AreItemsAvailable)
                {
                    foreach (WorkflowSubscription ws in workflowAssociations)
                    {
                        if (ws.Id == WFAssociation.WFAssoId)
                        {
                            ws.SetProperty("PrepareBriefValue", "06c07e59-cfce-e411-8e68-2c44fd94c786");

                            // create the association
                            workflowSubscriptionService.PublishSubscription(ws);
                            clientContext.ExecuteQuery();
                        }
                    }
                }
                else
                {
                    // create a new association / subscription
                    WorkflowSubscription newSubscription = new WorkflowSubscription(clientContext)
                    {
                        DefinitionId = WFAssociation.WFDefinitionId,
                        Enabled      = true,
                        Name         = WFAssociation.WFAssoName
                    };

                    //workflowHistoryListId = historyList.Id;
                    //workflowTaskListId = tasksList.Id;

                    //var startupOptions = new List<string>();
                    //// manual start
                    //startupOptions.Add("WorkflowStart");

                    // set the workflow start settings
                    newSubscription.EventTypes = WFAssociation.WFAssoEventTypes;// startupOptions;

                    // set the associated task and history lists
                    foreach (KeyValuePair <string, string> propertyDef in WFAssociation.WFAssoPropertyDefinitions)
                    {
                        newSubscription.SetProperty(propertyDef.Key, propertyDef.Value);
                    }
                    //newSubscription.SetProperty("HistoryListId",WFAssociation.WFAssoPropertyDefinitions["HistoryListId"]);// workflowHistoryListId.ToString());
                    //newSubscription.SetProperty("TaskListId", WFAssociation.WFAssoPropertyDefinitions["TaskListId"]);// workflowTaskListId.ToString());

                    //// OPTIONAL: add any association form values
                    //newSubscription.SetProperty("PrepareBriefValue", "06c07e59-cfce-e411-8e68-2c44fd94c786");

                    // create the association
                    workflowSubscriptionService.PublishSubscription(newSubscription);
                    clientContext.ExecuteQuery();
                }
            }
        }