Prevent editing of a list item if the workflow has failed

11.01.2010: A follow-up on this article is posted here: Check workflow status and refresh page when status equals “Completed”


Have you ever wanted to prevent editing of a list item if the workflow has failed? Here is the answer.

This code queries the list trough the web-service lists.asmx to get the status of the workflow. If the status i “3” – the workflow has failed and we want to prevent further editing of the element until the issue is resolved (the workflow is aborted and the cause is identified).

As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):
IMG

The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.

The scripts “interaction.js” and stringBuffer.js” is created by Erucy and published on codeplex – you can find them here.

Find the “FieldInternalName” of your workflow:
IMG

IMG

Find your list’s GUID and edit the script below and change the listGuid (line 18) and the “FieldInternalName” of your “Workflow-column” (line 18 and 20).

Add a CEWP below your EditForm like this:
IMG

With this code: (change listGuid and Wf’s “FieldInternalName”)
[javascript]
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/interaction.js"></script>
<script type="text/javascript" src="/test/English/Javascript/stringBuffer.js"></script>
<script type="text/javascript">
var SiteTitle = $.trim($(".ms-sitetitle a").text());
var ListName = $.trim($(".ms-pagetitle a").text());

var wfStatus = getWorkflowStatus();

if(wfStatus==3){ // 3 = "Error Occured"
$("#part1").hide(); // Hide the list form
$("#part1").before("<div>A workflow has failed. You cannot edit this item until this issue is resolved.</div><div><a title=’Click to send e-mail to an administrator’ href=’mailto:alexander.bautz@gmail.com?subject=Failed workflow on site: " + SiteTitle + ", list: " + ListName + ", itemId: " + getID() + "’>Click to send e-mail to an administrator</a></div>");
}

function getWorkflowStatus(){
var thisID = getID();
wsBaseUrl = L_Menu_BaseUrl + ‘/_vti_bin/’;
var item = getItemById(‘{d3d26e2b-93ca-4981-bf02-28dc73ad9287}’, thisID, [‘SampleWo’]); // Change ListGuid and Workflow "FieldInternalName"
if(item != null){
return item[‘SampleWo’];
}
}

function getID() {
var ID = ”;
var end = window.location.search.indexOf(‘&’);
if(window.location.search.indexOf(‘&’)<0){
ID = window.location.search.substring(4);
}else{
ID = window.location.search.substring(4,end);
}
return ID;
}
</script>
[/javascript]

If your workflow status is “3”, your EditForm looks like this:

IMG

A click on the link opens the default e-mail program and the subject is prefilled like this:
IMG

Please ask if something is unclear.

Alexander

One thought on “Prevent editing of a list item if the workflow has failed”

Leave a Reply