SharePoint 2013 – Get TemplateID for starting a workflow from the ribbon in SharePoint 2013 workflow

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 =

""+

""+

" " +

" +

" 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' />" +

" " +

" " +

" " +

" +

" CommandAction='~site/wfsvc/06f8de2413bc41fe869414fc061435f9/WFInitForm.aspx?List={ListId}&ID={SelectedItemId}&TemplateID={" + workflowTemplateId + "}&WF4=1&Source={Source}' />" +

" " +

"";

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}&ID={SelectedItemId}&TemplateID={" + workflowTemplateId + "}&WF4=1&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

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.