Skip to content

Instantly share code, notes, and snippets.

@vman
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vman/ab50f597983473cdfd19 to your computer and use it in GitHub Desktop.
Save vman/ab50f597983473cdfd19 to your computer and use it in GitHub Desktop.
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 Site Workflow
string workflowName = "SharePoint2013SiteWorkflow";
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.StartWorkflow(wfSubscription, initiationData);
clientContext.ExecuteQuery();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment