Skip to content

Instantly share code, notes, and snippets.

@vman
Created May 29, 2014 12:38
Show Gist options
  • Save vman/7c46528e8cf726a2cee9 to your computer and use it in GitHub Desktop.
Save vman/7c46528e8cf726a2cee9 to your computer and use it in GitHub Desktop.
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Workflow;
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 SharePoint2010 List Workflow
string workflowName = "SP2010DocWF";
//Name of the List to which the Workflow is Associated
string targetListName = "Documents";
//Guid of the List to which the Workflow is Associated
Guid targetListGUID = new Guid("b89e266c-5f20-4b83-9f95-10a42c629e84");
//Guid of the ListItem on which to start the Workflow
Guid targetItemGUID = new Guid("6ab8227b-66a3-4e12-8055-846dcfb53cab");
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);
//Will return all Workflow Associations which are running on the SharePoint 2010 Engine
WorkflowAssociationCollection wfAssociations = web.Lists.GetByTitle(targetListName).WorkflowAssociations;
//Get the required Workflow Association
WorkflowAssociation wfAssociation = wfAssociations.GetByName(workflowName);
clientContext.Load(wfAssociation);
clientContext.ExecuteQuery();
//Get the instance of the Interop Service which will be used to create an instance of the Workflow
InteropService workflowInteropService = wfServicesManager.GetWorkflowInteropService();
var initiationData = new Dictionary<string, object>();
//Start the Workflow
ClientResult<Guid> resultGuid = workflowInteropService.StartWorkflow(wfAssociation.Name, new Guid(), targetListGUID , targetItemGUID, initiationData);
clientContext.ExecuteQuery();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment