Last active
August 29, 2015 14:01
-
-
Save vman/630fcac748cc9bb093ac 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.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"; | |
//ID of the Item on which to start the workflow. | |
int itemID = 1; | |
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); | |
//The Subscription service is used to get all the Associations currently on the SPSite | |
WorkflowSubscriptionService wfSubscriptionService = wfServicesManager.GetWorkflowSubscriptionService(); | |
//All the subscriptions (associations) | |
WorkflowSubscriptionCollection wfSubscriptions = wfSubscriptionService.EnumerateSubscriptions(); | |
//Load only the subscription (association) which we want. You can also get a subscription by definition id. | |
clientContext.Load(wfSubscriptions, wfSubs => wfSubs.Where(wfSub => wfSub.Name == workflowName)); | |
clientContext.ExecuteQuery(); | |
//Get the subscription. | |
WorkflowSubscription wfSubscription = wfSubscriptions.First(); | |
//The Instance Service is used to start workflows and create instances. | |
WorkflowInstanceService wfInstanceService = wfServicesManager.GetWorkflowInstanceService(); | |
//Any custom parameters you want to send to the workflow. | |
var initiationData = new Dictionary<string, object>(); | |
wfInstanceService.StartWorkflowOnListItem(wfSubscription, itemID, initiationData); | |
clientContext.ExecuteQuery(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment