Created
May 28, 2014 21:08
-
-
Save vman/58f745f8615d3956b753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.SharePoint.Client; | |
using Microsoft.SharePoint.Client.WorkflowServices; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security; | |
namespace CSOMWorkflow | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Replace the following values according to your configuration. | |
//Site details | |
string siteUrl = "https://mytenant.sharepoint.com/sites/test"; | |
string userName = "user@mytenant.onmicrosoft.com"; | |
string password = "password"; | |
//Name of the SharePoint2013 List Workflow | |
string workflowName = "SP2013ListWF"; | |
//GUID of list on which to create the subscription (association); | |
Guid targetListGuid = new Guid("fc50af29-8ae5-4303-bad1-213151818215"); | |
//Name of the new Subscription (association) | |
string newSubscriptionName = "CSOM Subscription"; | |
//Workflow Lists | |
string workflowHistoryListID = "5bc8227b-66a3-4e12-8055-846dcfb53cab"; | |
string taskListID = "a75e266c-5f20-4b83-9f95-10a42c629e84"; | |
using (ClientContext clientContext = new ClientContext(siteUrl)) | |
{ | |
SecureString securePassword = new SecureString(); | |
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c); | |
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword); | |
Web web = clientContext.Web; | |
//Workflow Services Manager which will handle all the workflow interaction. | |
WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(clientContext, web); | |
//Deployment Service which holds all the Workflow Definitions deployed to the site | |
WorkflowDeploymentService wfDeploymentService = wfServicesManager.GetWorkflowDeploymentService(); | |
//Get all the definitions from the Deployment Service, or get a specific definition using the GetDefinition method. | |
WorkflowDefinitionCollection wfDefinitions = wfDeploymentService.EnumerateDefinitions(false); | |
clientContext.Load(wfDefinitions, wfDefs => wfDefs.Where(wfd => wfd.DisplayName == workflowName)); | |
clientContext.ExecuteQuery(); | |
WorkflowDefinition wfDefinition = wfDefinitions.First(); | |
//The Subscription service is used to get all the Associations currently on the SPSite | |
WorkflowSubscriptionService wfSubscriptionService = wfServicesManager.GetWorkflowSubscriptionService(); | |
//The subscription (association) | |
WorkflowSubscription wfSubscription = new WorkflowSubscription(clientContext); | |
wfSubscription.DefinitionId = wfDefinition.Id; | |
wfSubscription.Enabled = true; | |
wfSubscription.Name = newSubscriptionName; | |
var startupOptions = new List<string>(); | |
// automatic start | |
startupOptions.Add("ItemAdded"); | |
startupOptions.Add("ItemUpdated"); | |
// manual start | |
startupOptions.Add("WorkflowStart"); | |
// set the workflow start settings | |
wfSubscription.EventTypes = startupOptions; | |
// set the associated task and history lists | |
wfSubscription.SetProperty("HistoryListId", workflowHistoryListID); | |
wfSubscription.SetProperty("TaskListId", taskListID); | |
//Create the Association | |
wfSubscriptionService.PublishSubscriptionForList(wfSubscription, targetListGuid); | |
clientContext.ExecuteQuery(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment