1

I was trying to follow the instructions in an excellent post "Open workflow initiation form in modal dialog" but did not know how to do this step:

I'm able to dynamically insert the GUID of the workflow as long as I know the name. That way, if I republish the workflow, this custom action will still run correctly."

However, there was no supporting javascript example.

It appears that step is required to make the sample code work when the workflow is republished.

Sample code without the workflow GUID:

javascript:OpenPopUpPageWithTitle(L_Menu_BaseUrl + "/_layouts/IniWrkflIP.aspx?List={11449cc0-e482-4971-9ec7-f12e812f4489}&ID=" + SP.ListOperation.Selection.getSelectedItems(SP.ClientContext.get_current())[0].id + "&TemplateID={c810bee8-8799-40ac-a8db-d564a12c668d}", RefreshOnDialogClose, 600, 400, 'Assign To')

sample URL that gets built (without the workflow GUID):

http://<site url>/_layouts/IniWrkflIP.aspx?List={11449cc0-e482-4971-9ec7-f12e812f4489}&ID=56&TemplateID={c810bee8-8799-40ac-a8db-d564a12c668d}

I don't know what the workflow GUID query parameter is, nor the JSOM to populate it.

Can anyone out there fill in that code gap?

*note: I am posting this as a new question because I apparently do not have permission to comment on that original post.

1
  • The answer to that other question does not say anything about getting the workflow ID dynamically, it says it was found by examining the workflow initiation URL, and the workflow ID is in the TemplateID query parameter. It looks to me like that is hard-coded into the custom action. Commented Mar 24, 2015 at 19:09

1 Answer 1

1

The SPServices library offers a way to dynamically get a workflow GUID based on the name of the workflow. If this will work for you, you can write up a quick script that passes the name to SPServices, gets the workflow GUID, then passes the GUID along in a variable.

I use something very similar to find & start a workflow on a specific item. The script below (StartWorkflow) is called from an icon or link beside each item, which then passes the item URL and ID to the script. In your case, you wouldn't need the latter part that starts the workflow, but the first section would return your GUID based on whatever name you pass. My workflow name is hard-coded in this example, but you could pass yours as a variable isntead.

function StartWorkflow(ItemURL, ItemID){
var workflowDiv = 'WorkflowDiv' + ItemID
var workflowGUID = null;

//Find Workflow TemplateID for current document library
$().SPServices({
  operation: "GetTemplatesForItem",
  item: ItemURL,
  debug: true,
  async: false,
  completefunc: function (xData, Status) {
    $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function(i,e) {
      // hard coded workflow name
      if ( $(this).attr("Name") == "My Workflow Name" ) {              
        var guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");        
        if ( guid != null ) {
          workflowGUID = "{" + guid + "}";
          }
        }
      });
  }
});

//Show loading image
document.getElementById('wfloader').style.visibility = 'visible';

//Call Workflow
$().SPServices({
  operation: "StartWorkflow",
  item: ItemURL,
  debug: true,
  templateId: workflowGUID,
  workflowParameters: "<root />",
  completefunc: function() {
    window.location.reload();
  }

});

}

If you run into any issues, I highly recommend the discussions list on the SPServices link I gave. The users there are really knowledgeable and pretty good about responding.

1
  • Used this approach several times Commented Mar 24, 2015 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.