Microsoft Office SharePoint Server comes with many built-in Web services. And the one we are going to use here is the Workflow web service "/_vti_bin/workflow.asmx"
- GetWorkflowDataForItem operation in particular
The following JavaScript code example is fairly easy to understand.
It takes two parameters.
siteUrl - URL to your site, currentItemFileFullUrl - absolute URL to the item
And it will return you the message about item workflow status.
function CheckRunningWorkflow(siteUrl,currentItemFileFullUrl)
{
var xh=new ActiveXObject("Microsoft.XMLHTTP");
if (xh==null)
return "Error: Cannot create XMLHTTP object";
xh.Open("POST", siteUrl+"/_vti_bin/workflow.asmx", false);
xh.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xh.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/workflow/GetWorkflowDataForItem");
var soapData='<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"'+
'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
'<soap:Body>'+
'<GetWorkflowDataForItem xmlns="http://schemas.microsoft.com/sharepoint/soap/workflow/"><item>'+
currentItemFileFullUrl+
'</item></GetWorkflowDataForItem></soap:Body></soap:Envelope>'
xh.Send(soapData);
if (xh.status==200)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
if (xmlDoc==null)
return false;
xmlDoc.async = "false";
if(!xmlDoc.loadXML(xh.ResponseText))
return false;
var xpath = "/soap:Envelope"+
"/soap:Body"+
"/GetWorkflowDataForItemResponse"+
"/GetWorkflowDataForItemResult"+
"/WorkflowData"+
"/ActiveWorkflowsData"+
"/Workflows"+
"/Workflow"
var activeWF=xmlDoc.selectSingleNode(xpath)
//Check if there is an active workflow for current item
if(activeWF != null)
{
//Check Workflow status code
if(activeWF.getAttribute("InternalState") == '2')
{
return "Running";
}
else if(activeWF.getAttribute("InternalState") == '4')
{
return "Completed";
}
}
else
{
reutrn "No active workflow for this item";
}
return "Workflow status for this item is not Running or Completed"
}
return "Failed to call web service";
}
Note: In above we only checked InternalState is 2 or 4. Which are SPWorkflowState enumeration values of "Running" and "Completed"
The complete set of status are:
|
None
|
0 |
|
Locked
|
1 |
|
Running
|
2 |
|
Completed
|
4 |
|
Cancelled
|
8 |
|
Expiring
|
16 |
|
Expired
|
32 |
|
Faulting
|
64 |
|
Terminated
|
128 |
|
Suspended
|
256 |
|
Orphaned
|
512 |
|
HasNewEvents
|
1024 |
|
NotStarted
|
2048 |
|
All
|
4095 |
I always found this code useful when client wants custom
Context menu, or when managed code is not an option.