Background:

To start a workflow from a SharePoint ribbon a URL needs to be build up in the following format:

SPUserCustomAction action = web.Lists[listname].UserCustomActions.Add();

string workflowTemplateId = workflowId.ToString();

action.Title = title;

action.ImageUrl = “/_layouts/15/images/wfComment.png”;

action.Url = “{SiteUrl}/wfsvc/06f8de2413bc41fe869414fc061435f9/WFInitForm.aspx?List={ListId}&ID={SelectedItemId}&TemplateID={“+ workflowTemplateId +“}&WF4=1&Source={Source}”;

action.Location = “CommandUI.Ribbon.ListView”;

action.Sequence = 0;

action.CommandUIExtension =

“<CommandUIExtension xmlns=’http://schemas.microsoft.com/sharepoint/’>&#8221;+

“<CommandUIDefinitions>”+

” <CommandUIDefinition Location=’Ribbon.Documents.Manage.Controls._children’>” +

” <Button Id=’Calor.WF.AddComment’ “ +

” Command=’WF.AddComment’ “ +

” Image32by32=’/_layouts/15/images/wfComment48x48.png’ “ +

” Image16by16=’/_layouts/15/images/wfComment32x32.png’ “ +

” Sequence=’0′ “ +

” LabelText=’Add Comment’ “ +

” Description=’Add Comment Button’ “ +

” TemplateAlias=’o1′ />” +

” </CommandUIDefinition>” +

” </CommandUIDefinitions>” +

” <CommandUIHandlers>” +

” <CommandUIHandler Command=’WF.AddComment’ “ +

” CommandAction=’~site/wfsvc/06f8de2413bc41fe869414fc061435f9/WFInitForm.aspx?List={ListId}&amp;ID={SelectedItemId}&amp;TemplateID={“ + workflowTemplateId + “}&amp;WF4=1&amp;Source={Source}’ />” +

” </CommandUIHandlers> “ +

“</CommandUIExtension>”;

action.Update();

The problem:

The problem however is how do we get the TemplateID all the other bits of the URL can be resolved with variables that are filled in dynamically but the Template ID is a bit trickier.

‘~site/wfsvc/06f8de2413bc41fe869414fc061435f9/WFInitForm.aspx?List={ListId}&amp;ID={SelectedItemId}&amp;TemplateID={“ + workflowTemplateId + “}&amp;WF4=1&amp;Source={Source}’

The Solution:

var clientContext = new ClientContext(web.Url);

var workflowServiceManager = new WorkflowServicesManager(clientContext, clientContext.Web);

WorkflowSubscriptionService workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();

var workflowSubscriptions = workflowSubscriptionService.EnumerateSubscriptions();

clientContext.Load(workflowSubscriptions);

clientContext.ExecuteQuery();

foreach(var workflowSubscription inworkflowSubscriptions)

{

   workflowTemplateId = workflowSubScription.Id

}

 

Advertisements