Using JavaScript to check SharePoint list item workflow status - Via Web Service call.

by James 5/20/2008 6:57:00 PM

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.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Programming | SharePoint

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen Adopted by James Tsai

About the author

Name of author James Tsai
.NET / SharePoint Consultant
Columbus, OH

E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
View posts in large calendar

Certifications

MCPD
MCTS

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in