public Guid Subscribe(string name, Guid definitionId, Guid targetListId, WorkflowSubscritpionEventType eventTypes, Guid taskListId, Guid historyListId) { var eventTypesList = new List <string>(); foreach (WorkflowSubscritpionEventType type in Enum.GetValues(typeof(WorkflowSubscritpionEventType))) { if ((type & eventTypes) > 0) { eventTypesList.Add(type.ToString()); } } var subscription = new WorkflowSubscription(ClientContext) { Name = name, Enabled = true, DefinitionId = definitionId, EventSourceId = targetListId, EventTypes = eventTypesList.ToArray() }; subscription.SetProperty("TaskListId", taskListId.ToString()); subscription.SetProperty("HistoryListId", historyListId.ToString()); var subscriptionService = workflowServicesManager.GetWorkflowSubscriptionService(); var result = subscriptionService.PublishSubscriptionForList(subscription, targetListId); ClientContext.ExecuteQuery(); return(result.Value); }
public static void CreateAssociation(ref ClientContext clientContext, ref WorkflowServicesManager wfsm, Guid workflowDefinitionId, Guid listId, Guid historyListId, Guid taskListId) { WorkflowSubscriptionService subservice = wfsm.GetWorkflowSubscriptionService(); Console.WriteLine(); Console.WriteLine("Creating workflow association..."); WorkflowSubscription newSubscription = new WorkflowSubscription(clientContext) { DefinitionId = workflowDefinitionId, Enabled = true, Name = "Custom Association" + DateTime.Now }; // define startup options newSubscription.EventTypes = new List <string> { "ItemAdded", "ItemUpdated", "WorkflowStart" }; //define history & task list newSubscription.SetProperty("HistoryListId", historyListId.ToString()); newSubscription.SetProperty("TaskListId", taskListId.ToString()); //create association subservice.PublishSubscriptionForList(newSubscription, listId); clientContext.ExecuteQuery(); Console.WriteLine("Workflow association created!"); Console.ReadLine(); }
public static WorkflowSubscription MakeListId(this WorkflowSubscription workflowSubscription, Guid listId) { workflowSubscription.SetProperty("ListId", listId.ToString()); workflowSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", listId.ToString()); return(workflowSubscription); }
public Guid Subscribe(string name, Guid definitionId, Guid targetListId, WorkflowSubscritpionEventType eventTypes, Guid taskListId, Guid historyListId) { var eventTypesList = new List<string>(); foreach (WorkflowSubscritpionEventType type in Enum.GetValues(typeof(WorkflowSubscritpionEventType))) { if ((type & eventTypes) > 0) eventTypesList.Add(type.ToString()); } var subscription = new WorkflowSubscription(ClientContext) { Name = name, Enabled = true, DefinitionId = definitionId, EventSourceId = targetListId, EventTypes = eventTypesList.ToArray() }; subscription.SetProperty("TaskListId", taskListId.ToString()); subscription.SetProperty("HistoryListId", historyListId.ToString()); var subscriptionService = workflowServicesManager.GetWorkflowSubscriptionService(); var result = subscriptionService.PublishSubscriptionForList(subscription, targetListId); ClientContext.ExecuteQuery(); return result.Value; }
/// <summary> /// Adds a workflow subscription to a list /// </summary> /// <param name="list"></param> /// <param name="workflowDefinition">The workflow definition. <seealso> /// <cref>WorkflowExtensions.GetWorkflowDefinition</cref> /// </seealso> /// </param> /// <param name="subscriptionName">The name of the workflow subscription to create</param> /// <param name="startManually">if True the workflow can be started manually</param> /// <param name="startOnCreate">if True the workflow will be started on item creation</param> /// <param name="startOnChange">if True the workflow will be started on item change</param> /// <param name="historyListName">the name of the history list. If not available it will be created</param> /// <param name="taskListName">the name of the task list. If not available it will be created</param> /// <param name="associationValues"></param> /// <returns>Guid of the workflow subscription</returns> public static Guid AddWorkflowSubscription(this List list, WorkflowDefinition workflowDefinition, string subscriptionName, bool startManually, bool startOnCreate, bool startOnChange, string historyListName, string taskListName, Dictionary<string, string> associationValues = null) { // parameter validation subscriptionName.ValidateNotNullOrEmpty("subscriptionName"); historyListName.ValidateNotNullOrEmpty("historyListName"); taskListName.ValidateNotNullOrEmpty("taskListName"); var historyList = list.ParentWeb.GetListByTitle(historyListName); if (historyList == null) { historyList = list.ParentWeb.CreateList(ListTemplateType.WorkflowHistory, historyListName, false); } var taskList = list.ParentWeb.GetListByTitle(taskListName); if (taskList == null) { taskList = list.ParentWeb.CreateList(ListTemplateType.Tasks, taskListName, false); } var sub = new WorkflowSubscription(list.Context); sub.DefinitionId = workflowDefinition.Id; sub.Enabled = true; sub.Name = subscriptionName; var eventTypes = new List<string>(); if (startManually) eventTypes.Add("WorkflowStart"); if (startOnCreate) eventTypes.Add("ItemAdded"); if (startOnChange) eventTypes.Add("ItemUpdated"); sub.EventTypes = eventTypes; sub.SetProperty("HistoryListId", historyList.Id.ToString()); sub.SetProperty("TaskListId", taskList.Id.ToString()); if (associationValues != null) { foreach (var key in associationValues.Keys) { sub.SetProperty(key, associationValues[key]); } } var servicesManager = new WorkflowServicesManager(list.Context, list.ParentWeb); var subscriptionService = servicesManager.GetWorkflowSubscriptionService(); var subscriptionResult = subscriptionService.PublishSubscriptionForList(sub, list.Id); list.Context.ExecuteQueryRetry(); return subscriptionResult.Value; }
protected virtual void MapProperties(WorkflowSubscription workflow, SP2013WorkflowSubscriptionDefinition definition) { foreach (var prop in definition.Properties) { workflow.SetProperty(prop.Name, prop.Value); } }
/// <summary> /// Create a new workflow association (subscription). /// </summary> public static void CreateAssociation(ref ClientContext clientConext, ref WorkflowServicesManager wfServicesManager, Guid definitionId, Guid listId, Guid historyListId, Guid taskListId) { WorkflowSubscriptionService subService = wfServicesManager.GetWorkflowSubscriptionService(); Console.WriteLine(); Console.WriteLine("Creating workflow association..."); // create new association (aka: subscription) WorkflowSubscription newSubscription = new WorkflowSubscription(clientConext) { DefinitionId = definitionId, Enabled = true, Name = "Custom Association " + DateTime.Now }; // define startup options // automatic start options = ItemAdded & ItemUpdated // manual start = WorkflowStart newSubscription.EventTypes = new List <string> { "ItemAdded", "ItemUpdated", "WorkflowStart" }; // define the history & task associated lists newSubscription.SetProperty("HistoryListId", historyListId.ToString()); newSubscription.SetProperty("TaskListId", taskListId.ToString()); // OPTIONAL: if any values submitted by association form, add as properties here newSubscription.SetProperty("Prop1", "Value1"); newSubscription.SetProperty("Prop2", "Value2"); // create the association subService.PublishSubscriptionForList(newSubscription, listId); // creates association on list //subService.PublishSubscription(newSubscription); // creates association on current site clientConext.ExecuteQuery(); Console.WriteLine("Workflow association created!"); }
private void DeployWorkflowSubscriptionDefinition( object host, SPList list, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { var web = list.ParentWeb; var workflowServiceManager = new WorkflowServicesManager(list.ParentWeb); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) { throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); } var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(list.ID); InvokeOnModelEvent <SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(null, ModelEventType.OnUpdating); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var newSubscription = new WorkflowSubscription(); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = workflowSubscriptionModel.EventTypes.ToList(); newSubscription.EventSourceId = list.ID; // lookup task and history lists, probaly need to think ab otehr strategy var taskList = web.GetList(SPUrlUtility.CombineUrl(web.Url, workflowSubscriptionModel.TaskListUrl)); var historyList = web.GetList(SPUrlUtility.CombineUrl(web.Url, workflowSubscriptionModel.HistoryListUrl)); newSubscription.SetProperty("HistoryListId", historyList.ID.ToString()); newSubscription.SetProperty("TaskListId", taskList.ID.ToString()); newSubscription.SetProperty("ListId", list.ID.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", list.ID.ToString()); // to be able to change HistoryListId, TaskListId, ListId InvokeOnModelEvent <SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(newSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); } else { currentSubscription.EventTypes = workflowSubscriptionModel.EventTypes.ToList(); InvokeOnModelEvent <SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(currentSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); workflowSubscriptionService.PublishSubscription(currentSubscription); } }
private void DeployListWorkflowSubscriptionDefinition( object host, ClientContext hostclientContext, List list, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { // hostclientContext - it must be clientContext, not ClientRuntimeContext - won't work and would give some weirs error with wg publishing // use only clientContext instance for the workflow publishing, not ClientRuntimeContext var context = list.Context; var web = list.ParentWeb; //This WorkflowServiceManager object is created for current web from client context, //but actually it has to be created for parent web of current web. //Otherwise it uses wrong web for provisions with multiple webs //var workflowServiceManager = new WorkflowServicesManager(hostclientContext, hostclientContext.Web); context.Load(web); context.Load(list); context.ExecuteQueryWithTrace(); //This is creation of WorkflowServiceManager with right web var workflowServiceManager = new WorkflowServicesManager(hostclientContext, web); hostclientContext.Load(workflowServiceManager); hostclientContext.ExecuteQueryWithTrace(); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); hostclientContext.Load(workflowSubscriptionService); hostclientContext.Load(workflowDeploymentService); hostclientContext.Load(tgtwis); hostclientContext.ExecuteQueryWithTrace(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); hostclientContext.Load(publishedWorkflows); hostclientContext.ExecuteQueryWithTrace(); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) { throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); } var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByEventSource(list.Id); hostclientContext.Load(subscriptions); hostclientContext.ExecuteQueryWithTrace(); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var taskList = GetTaskList(web, workflowSubscriptionModel); var historyList = GetHistoryList(web, workflowSubscriptionModel); TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new SP2013 workflow subscription"); var newSubscription = new WorkflowSubscription(hostclientContext); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Setting subscription properties"); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = workflowSubscriptionModel.EventTypes; newSubscription.EventSourceId = list.Id; newSubscription.SetProperty("HistoryListId", historyList.Id.ToString()); newSubscription.SetProperty("TaskListId", taskList.Id.ToString()); newSubscription.SetProperty("ListId", list.Id.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", list.Id.ToString()); MapProperties(newSubscription, workflowSubscriptionModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); hostclientContext.ExecuteQueryWithTrace(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing SP2013 workflow subscription"); currentSubscription.EventTypes = workflowSubscriptionModel.EventTypes; MapProperties(currentSubscription, workflowSubscriptionModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); workflowSubscriptionService.PublishSubscription(currentSubscription); hostclientContext.ExecuteQueryWithTrace(); } }
/// <summary> /// Adds a workflow subscription to a list /// </summary> /// <param name="list"></param> /// <param name="workflowDefinition">The workflow definition. <seealso cref="WorkflowExtensions.GetWorkflowDefinition"/></param> /// <param name="subscriptionName">The name of the workflow subscription to create</param> /// <param name="startManually">if True the workflow can be started manually</param> /// <param name="startOnCreate">if True the workflow will be started on item creation</param> /// <param name="startOnChange">if True the workflow will be started on item change</param> /// <param name="historyListName">the name of the history list. If not available it will be created</param> /// <param name="taskListName">the name of the task list. If not available it will be created</param> /// <param name="associationValues"></param> /// <returns>Guid of the workflow subscription</returns> public static Guid AddWorkflowSubscription(this List list, WorkflowDefinition workflowDefinition, string subscriptionName, bool startManually, bool startOnCreate, bool startOnChange, string historyListName, string taskListName, Dictionary <string, string> associationValues = null) { // parameter validation subscriptionName.ValidateNotNullOrEmpty("subscriptionName"); historyListName.ValidateNotNullOrEmpty("historyListName"); taskListName.ValidateNotNullOrEmpty("taskListName"); var historyList = list.ParentWeb.GetListByTitle(historyListName); if (historyList == null) { historyList = list.ParentWeb.CreateList(ListTemplateType.WorkflowHistory, historyListName, false); } var taskList = list.ParentWeb.GetListByTitle(taskListName); if (taskList == null) { taskList = list.ParentWeb.CreateList(ListTemplateType.Tasks, taskListName, false); } var sub = new WorkflowSubscription(list.Context); sub.DefinitionId = workflowDefinition.Id; sub.Enabled = true; sub.Name = subscriptionName; var eventTypes = new List <string>(); if (startManually) { eventTypes.Add("WorkflowStart"); } if (startOnCreate) { eventTypes.Add("ItemAdded"); } if (startOnChange) { eventTypes.Add("ItemUpdated"); } sub.EventTypes = eventTypes; sub.SetProperty("HistoryListId", historyList.Id.ToString()); sub.SetProperty("TaskListId", taskList.Id.ToString()); if (associationValues != null) { foreach (var key in associationValues.Keys) { sub.SetProperty(key, associationValues[key]); } } var servicesManager = new WorkflowServicesManager(list.Context, list.ParentWeb); var subscriptionService = servicesManager.GetWorkflowSubscriptionService(); var subscriptionResult = subscriptionService.PublishSubscriptionForList(sub, list.Id); list.Context.ExecuteQuery(); return(subscriptionResult.Value); }
private void DeployListWorkflowSubscriptionDefinition( object host, ClientContext hostclientContext, List list, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { // hostclientContext - it must be clientContext, not ClientRuntimeContext - won't work and would give some weirs error with wg publishing // use only clientContext instance for the workflow publishing, not ClientRuntimeContext var context = list.Context; var web = list.ParentWeb; //This WorkflowServiceManager object is created for current web from client context, //but actually it has to be created for parent web of current web. //Otherwise it uses wrong web for provisions with multiple webs //var workflowServiceManager = new WorkflowServicesManager(hostclientContext, hostclientContext.Web); context.Load(web); context.Load(list); context.ExecuteQueryWithTrace(); //This is creation of WorkflowServiceManager with right web var workflowServiceManager = new WorkflowServicesManager(hostclientContext, web); hostclientContext.Load(workflowServiceManager); hostclientContext.ExecuteQueryWithTrace(); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); hostclientContext.Load(workflowSubscriptionService); hostclientContext.Load(workflowDeploymentService); hostclientContext.Load(tgtwis); hostclientContext.ExecuteQueryWithTrace(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); hostclientContext.Load(publishedWorkflows); hostclientContext.ExecuteQueryWithTrace(); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByEventSource(list.Id); hostclientContext.Load(subscriptions); hostclientContext.ExecuteQueryWithTrace(); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var taskList = GetTaskList(web, workflowSubscriptionModel); var historyList = GetHistoryList(web, workflowSubscriptionModel); TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new SP2013 workflow subscription"); var newSubscription = new WorkflowSubscription(hostclientContext); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Setting subscription properties"); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = workflowSubscriptionModel.EventTypes; newSubscription.EventSourceId = list.Id; newSubscription.SetProperty("HistoryListId", historyList.Id.ToString()); newSubscription.SetProperty("TaskListId", taskList.Id.ToString()); newSubscription.SetProperty("ListId", list.Id.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", list.Id.ToString()); MapProperties(currentSubscription, workflowSubscriptionModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); hostclientContext.ExecuteQueryWithTrace(); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing SP2013 workflow subscription"); currentSubscription.EventTypes = workflowSubscriptionModel.EventTypes; MapProperties(currentSubscription, workflowSubscriptionModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); workflowSubscriptionService.PublishSubscription(currentSubscription); hostclientContext.ExecuteQueryWithTrace(); } }
private void DeployWebWorkflowSubscriptionDefinition( object host, SPWeb web, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { var workflowServiceManager = new WorkflowServicesManager(web); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); // EnumerateSubscriptionsByEventSource() somehow throws an exception //var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByEventSource(web.ID); var subscriptions = workflowSubscriptionService.EnumerateSubscriptions().Where(s => s.EventSourceId == web.ID); InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(null, ModelEventType.OnUpdating); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var taskList = GetTaskList(web, workflowSubscriptionModel); var historyList = GetHistoryList(web, workflowSubscriptionModel); TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new SP2013 workflow subscription"); var newSubscription = new WorkflowSubscription(); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Setting subscription properties"); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = new List<string>(workflowSubscriptionModel.EventTypes); newSubscription.EventSourceId = web.ID; newSubscription.SetProperty("HistoryListId", historyList.ID.ToString()); newSubscription.SetProperty("TaskListId", taskList.ID.ToString()); newSubscription.SetProperty("WebId", web.ID.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.WebId", web.ID.ToString()); // to be able to change HistoryListId, TaskListId, ListId InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(newSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing SP2013 workflow subscription"); currentSubscription.EventTypes = new List<string>(workflowSubscriptionModel.EventTypes); InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(currentSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); workflowSubscriptionService.PublishSubscription(currentSubscription); } }
private void DeployWorkflowSubscriptionDefinition( object host, SPList list, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { var web = list.ParentWeb; var workflowServiceManager = new WorkflowServicesManager(list.ParentWeb); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(list.ID); InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(null, ModelEventType.OnUpdating); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var newSubscription = new WorkflowSubscription(); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = workflowSubscriptionModel.EventTypes.ToList(); newSubscription.EventSourceId = list.ID; // lookup task and history lists, probaly need to think ab otehr strategy var taskList = web.GetList(SPUrlUtility.CombineUrl(web.Url, workflowSubscriptionModel.TaskListUrl)); var historyList = web.GetList(SPUrlUtility.CombineUrl(web.Url, workflowSubscriptionModel.HistoryListUrl)); newSubscription.SetProperty("HistoryListId", historyList.ID.ToString()); newSubscription.SetProperty("TaskListId", taskList.ID.ToString()); newSubscription.SetProperty("ListId", list.ID.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", list.ID.ToString()); // to be able to change HistoryListId, TaskListId, ListId InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(newSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); } else { currentSubscription.EventTypes = workflowSubscriptionModel.EventTypes.ToList(); InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(currentSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); workflowSubscriptionService.PublishSubscription(currentSubscription); } }
private void DeployWorkflowSubscriptionDefinition( SP2013WorkflowSubscriptionModelHost host, ClientContext hostClientContext, List list, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { // hostClientContext - it must be ClientContext, not ClientRuntimeContext - won't work and would give some weirs error with wg publishing // use only ClientContext instance for the workflow pubnlishing, not ClientRuntimeContext var context = list.Context; var web = list.ParentWeb; var workflowServiceManager = new WorkflowServicesManager(hostClientContext, hostClientContext.Web); context.Load(web); context.Load(list); context.ExecuteQuery(); hostClientContext.Load(workflowServiceManager); hostClientContext.ExecuteQuery(); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); hostClientContext.Load(workflowSubscriptionService); hostClientContext.Load(workflowDeploymentService); hostClientContext.Load(tgtwis); hostClientContext.ExecuteQuery(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); hostClientContext.Load(publishedWorkflows); hostClientContext.ExecuteQuery(); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(list.Id); hostClientContext.Load(subscriptions); hostClientContext.ExecuteQuery(); InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(null, ModelEventType.OnUpdating); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var newSubscription = new WorkflowSubscription(hostClientContext); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = workflowSubscriptionModel.EventTypes; newSubscription.EventSourceId = list.Id; // lookup task and history lists, probaly need to think ab otehr strategy var taskList = WebExtensions.QueryAndGetListByUrl(web, workflowSubscriptionModel.TaskListUrl); var historyList = WebExtensions.QueryAndGetListByUrl(web, workflowSubscriptionModel.HistoryListUrl); newSubscription.SetProperty("HistoryListId", historyList.Id.ToString()); newSubscription.SetProperty("TaskListId", taskList.Id.ToString()); newSubscription.SetProperty("ListId", list.Id.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", list.Id.ToString()); // to be able to change HistoryListId, TaskListId, ListId InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(newSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); hostClientContext.ExecuteQuery(); } else { currentSubscription.EventTypes = workflowSubscriptionModel.EventTypes; InvokeOnModelEvent<SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(currentSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); workflowSubscriptionService.PublishSubscription(currentSubscription); hostClientContext.ExecuteQuery(); } }
private void DeployWebWorkflowSubscriptionDefinition( object host, SPWeb web, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { var workflowServiceManager = new WorkflowServicesManager(web); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) { throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); } // EnumerateSubscriptionsByEventSource() somehow throws an exception //var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByEventSource(web.ID); var subscriptions = workflowSubscriptionService.EnumerateSubscriptions().Where(s => s.EventSourceId == web.ID); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var taskList = GetTaskList(web, workflowSubscriptionModel); var historyList = GetHistoryList(web, workflowSubscriptionModel); TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new SP2013 workflow subscription"); var newSubscription = new WorkflowSubscription(); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Setting subscription properties"); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = new List <string>(workflowSubscriptionModel.EventTypes); newSubscription.EventSourceId = web.ID; newSubscription.SetProperty("HistoryListId", historyList.ID.ToString()); newSubscription.SetProperty("TaskListId", taskList.ID.ToString()); newSubscription.SetProperty("WebId", web.ID.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.WebId", web.ID.ToString()); MapProperties(newSubscription, workflowSubscriptionModel); // to be able to change HistoryListId, TaskListId, ListId InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing SP2013 workflow subscription"); currentSubscription.EventTypes = new List <string>(workflowSubscriptionModel.EventTypes); MapProperties(currentSubscription, workflowSubscriptionModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Calling PublishSubscription()"); workflowSubscriptionService.PublishSubscription(currentSubscription); } }
protected virtual void MapProperties(WorkflowSubscription workflow, SP2013WorkflowSubscriptionDefinition definition) { foreach (var prop in definition.Properties) workflow.SetProperty(prop.Name, prop.Value); }
public static WorkflowSubscription MakeHistoryListId(this WorkflowSubscription workflowSubscription, Guid historyListId) { workflowSubscription.SetProperty("HistoryListId", historyListId.ToString()); return(workflowSubscription); }
public static WorkflowSubscription MakeTaskListId(this WorkflowSubscription workflowSubscription, Guid taskListId) { workflowSubscription.SetProperty("TaskListId", taskListId.ToString()); return(workflowSubscription); }
private void DeployWorkflowSubscriptionDefinition( SP2013WorkflowSubscriptionModelHost host, ClientContext hostClientContext, List list, SP2013WorkflowSubscriptionDefinition workflowSubscriptionModel) { // hostClientContext - it must be ClientContext, not ClientRuntimeContext - won't work and would give some weirs error with wg publishing // use only ClientContext instance for the workflow pubnlishing, not ClientRuntimeContext var context = list.Context; var web = list.ParentWeb; var workflowServiceManager = new WorkflowServicesManager(hostClientContext, hostClientContext.Web); context.Load(web); context.Load(list); context.ExecuteQuery(); hostClientContext.Load(workflowServiceManager); hostClientContext.ExecuteQuery(); var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService(); var workflowDeploymentService = workflowServiceManager.GetWorkflowDeploymentService(); var tgtwis = workflowServiceManager.GetWorkflowInstanceService(); hostClientContext.Load(workflowSubscriptionService); hostClientContext.Load(workflowDeploymentService); hostClientContext.Load(tgtwis); hostClientContext.ExecuteQuery(); var publishedWorkflows = workflowDeploymentService.EnumerateDefinitions(true); hostClientContext.Load(publishedWorkflows); hostClientContext.ExecuteQuery(); var currentWorkflowDefinition = publishedWorkflows.FirstOrDefault(w => w.DisplayName == workflowSubscriptionModel.WorkflowDisplayName); if (currentWorkflowDefinition == null) { throw new Exception(string.Format("Cannot lookup workflow definition with display name: [{0}] on web:[{1}]", workflowSubscriptionModel.WorkflowDisplayName, web.Url)); } var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(list.Id); hostClientContext.Load(subscriptions); hostClientContext.ExecuteQuery(); InvokeOnModelEvent <SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(null, ModelEventType.OnUpdating); var currentSubscription = subscriptions.FirstOrDefault(s => s.Name == workflowSubscriptionModel.Name); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); if (currentSubscription == null) { var newSubscription = new WorkflowSubscription(hostClientContext); newSubscription.Name = workflowSubscriptionModel.Name; newSubscription.DefinitionId = currentWorkflowDefinition.Id; newSubscription.EventTypes = workflowSubscriptionModel.EventTypes; newSubscription.EventSourceId = list.Id; // lookup task and history lists, probaly need to think ab otehr strategy var taskList = WebExtensions.QueryAndGetListByUrl(web, workflowSubscriptionModel.TaskListUrl); var historyList = WebExtensions.QueryAndGetListByUrl(web, workflowSubscriptionModel.HistoryListUrl); newSubscription.SetProperty("HistoryListId", historyList.Id.ToString()); newSubscription.SetProperty("TaskListId", taskList.Id.ToString()); newSubscription.SetProperty("ListId", list.Id.ToString()); newSubscription.SetProperty("Microsoft.SharePoint.ActivationProperties.ListId", list.Id.ToString()); // to be able to change HistoryListId, TaskListId, ListId InvokeOnModelEvent <SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(newSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = newSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); var currentSubscriptionId = workflowSubscriptionService.PublishSubscription(newSubscription); hostClientContext.ExecuteQuery(); } else { currentSubscription.EventTypes = workflowSubscriptionModel.EventTypes; InvokeOnModelEvent <SP2013WorkflowSubscriptionDefinition, WorkflowSubscription>(currentSubscription, ModelEventType.OnUpdated); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentSubscription, ObjectType = typeof(WorkflowSubscription), ObjectDefinition = workflowSubscriptionModel, ModelHost = host }); workflowSubscriptionService.PublishSubscription(currentSubscription); hostClientContext.ExecuteQuery(); } }
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(); } } }