Chaining Workflow functions

Apr 20, 2011 at 4:42 PM

I'm rewriting a pice of my DVWP that initiates a workflow via a hyperlink.  It's a pain to maintain because if the workflow is updated, the template ID changes and the hyperlink breaks.  So I'd like to adapt my hyperlink if possible.  I'm looking over Jason's article as a reference.

Does this code look erroneous to you?

<script type="text/javascript">
function StartWorkflow(ItemURL, ItemID) {
var workflowGUID = null;
$().SPServices({
  operation: "GetTemplatesForItem",
  item: ItemURL,
  async: false,
  completefunc: function (xData, Status) {
    $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function(i,e) {
      // hard coded workflow name
      if ( $(this).attr("Name") == "Copy" ) {              
        var guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");        
        if ( guid != null ) {
          workflowGUID = "{" + guid + "}";
          }
        }
      });
  }
});

var loadingImage = '#Loader' + ItemID;
var workflowTD = '#WorkflowTD' + ItemID;
  $(loadingImage).show();
  $().SPServices({
    operation: "StartWorkflow",
    item: ItemURL,
    templateId: workflowGUID,
    workflowParameters: "<root />",
    completefunc: function() {
      $(workflowTD).html("Workflow Started");
    }
  });
}
</script>
eric
Apr 20, 2011 at 5:10 PM

Guess I should learn to trust myself more, it worked.

Apr 20, 2011 at 10:57 PM

Yep, that code works great!  I use it as a standard for any workflow firing I'm doing with SPServices.  The name of the workflow almost never changes and that's the only hard coded value, so if you can teach your users *NOT* to touch that dial, then your solution will stay intact.

Check out this post on how to pass in parameters:

http://spservices.codeplex.com/discussions/250122

Jun 25, 2013 at 8:11 PM
I am using the exact code to kick off workflow in SharePoint 2013 but somehow reponseXML returns nothing, I get success in Status but not in responseXML.
I'm using custom list , any help would be appreciated.
function StartWorkflow(ItemURL, ItemID) {
var workflowGUID = null;
$().SPServices({
  operation: "GetTemplatesForItem",
  item: ItemURL,
  async: false,
  completefunc: function (xData, Status) {
  alert(Status);
    $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function(i,e) {
    alert(xData.responseXML.xml);
      // hard coded workflow name
      if ( $(this).attr("Name") == "SendEmail" ) {              
        var guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");        
        if ( guid != null ) {
          workflowGUID = "{" + guid + "}";
          alert(workflowGUID );
          }
        }
      });
  }
});
Jun 26, 2013 at 4:09 AM
indraappala,
The code below does not prove that there is nothing in responseXML, only that it can't find "WorkflowTemplates > WorkflowTemplate"...

Place the following right after the alert for the Status (and prior to attempting to use jquery to find XML nodes):
alert(xData.responseXML);
This should show you the full XML response. First thing you want to do is check for any exceptions that SharePoint might have returned in the payload.

_____
Paul

Sent from mobile device.
Coordinator
Jun 26, 2013 at 5:28 AM
Indra is trying to use jQuery 1.9.0, which is probably the problem.

Indra: You only need to post in one place.

M.
Jun 26, 2013 at 2:10 PM
Roger that Mark.

Paul/mark,

Please find below code, the complete copy and responseXML returns nothing, am i missing something here?
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.js"></script>
<script language="javascript" type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2013.01/jquery.SPServices-2013.01.min.js"></script>

<script language="javascript" type="text/javascript">

function StartWorkflow(ItemURL, ItemID) {
var workflowGUID = null;
$().SPServices({
  operation: "GetTemplatesForItem",
  item: ItemURL,
  async: false,
  completefunc: function (xData, Status) {
  alert(Status);
  alert(xData.responseXML);
     $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function(i,e) {
      
      // hard coded workflow name
      if ( $(this).attr("Name") == "SendEmail" ) {              
        var guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");        
        if ( guid != null ) {
          workflowGUID = "{" + guid + "}";
          }
        }
      });
  }
});

var workflowTD = '#WorkflowTD' + ItemID;
 // $(loadingImage).show();
  $().SPServices({
    operation: "StartWorkflow",
    item: ItemURL,
    templateId: workflowGUID,
    workflowParameters: "<root />",
    completefunc: function() {
      $(workflowTD).html("Sending Email");
    }
  });
}
</script>

Jun 26, 2013 at 2:18 PM
After changing the Jquery as Mark mention I see the responseXML returns as [Object Document] but as you see in the code I dont see either alert("1") or alert on GUID. Any advise would be really appreciated.
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.js"></script>
<script language="javascript" type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2013.01/jquery.SPServices-2013.01.min.js"></script>

<script language="javascript" type="text/javascript">

function StartWorkflow(ItemURL, ItemID) {
var workflowGUID = null;
$().SPServices({
  operation: "GetTemplatesForItem",
  item: ItemURL,
  async: false,
  completefunc: function (xData, Status) {
  alert(Status);
  alert(xData.responseXML);
     $(xData.responseXML).find("WorkflowTemplates > WorkflowTemplate").each(function(i,e) {
      alert("1");
      // hard coded workflow name
      if ( $(this).attr("Name") == "SendEmail" ) {              
        var guid = $(this).find("WorkflowTemplateIdSet").attr("TemplateId");        
        if ( guid != null ) {
          workflowGUID = "{" + guid + "}";
          alert(workflowGUID);
          }
        }
      });
  }
});

var workflowTD = '#WorkflowTD' + ItemID;
 // $(loadingImage).show();
  $().SPServices({
    operation: "StartWorkflow",
    item: ItemURL,
    templateId: workflowGUID,
    workflowParameters: "<root />",
    completefunc: function() {
      $(workflowTD).html("Sending Email");
    }
  });
}
</script>

Coordinator
Jun 26, 2013 at 3:21 PM
As Paul pointed out, all that tells you is that .find("WorkflowTemplates > WorkflowTemplate") isn't returning anything. Take a look at the full response to see if you have an error or an issue.

M.
Jun 26, 2013 at 4:18 PM
Mark,

Please see full response below, any idea ? ( I purposely deleted the URL when copying the XML response here).
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetTemplatesForItemResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/workflow/"><GetTemplatesForItemResult><TemplateData><Web Title="AVM Message Center" Url="" /><List Title="Incident Management" Url="" /><WorkflowTemplates></WorkflowTemplates></TemplateData></GetTemplatesForItemResult></GetTemplatesForItemResponse></soap:Body></soap:Envelope>
Coordinator
Jun 26, 2013 at 4:24 PM
That would indicate that there are no workflow templates available for the item.

M.
Jun 26, 2013 at 4:31 PM
Mark,

I didn't get that, the workflow is there, I've used SharePoint designer 2013 to create custom workflow and published to the custom list.

The workflow works I've tested it on item added / edited.

Can you please shed some light and let me know what should be done, all I'm trying to is start workflow on click in a custom page.
Coordinator
Jun 26, 2013 at 4:36 PM
Is it a list-based workflow? The Web Services won't work with other types.

M.
Jun 26, 2013 at 4:39 PM
Yes it's a list based workflow.
Coordinator
Jun 27, 2013 at 5:41 PM
I'm not sure what else to tell you. You'll have to do some debugging.

M.