1

I have created a custom action for starting the workflow on list item. When user selects the item, an item from ribbon is enabled to start the workflow directly on item.

I have created on application page to make redirection to the workflow initiation form. But my problem is that I want to first determine that if the workflow is possible on the current selected item or not. i.e. SharePoint does not allow to start the workflow if the current workflow status is Error Occurred and etc.

I have found this link which describes the list of enumeration of status of workflow running on list item.

Among the list which values should I compare to allow the user to start workflow. If the workflow is not able to start on the item I would display the alert box to user.

My code is:

private void RedirectToInitiationForm(string itemId, string listId,string sourceUrl)
{
    try
    {
        // getting current web from context.
        SPWeb currentWeb = SPContext.Current.Web;
        SPList Documents = currentWeb.Lists[new Guid(listId)];
        SPWorkflowAssociation wfAssociation = qmsDocuments.WorkflowAssociations.Cast<SPWorkflowAssociation>().FirstOrDefault(wa => wa.BaseId == Constants.WorkflowAssociationID);
        if (wfAssociation != null)
        {
            // getting item GUID
            SPListItem DocItem = Documents.GetItemById(Convert.ToInt32(itemId));
            string url = SPUrlUtility.CombineUrl(currentWeb.Url,"/"+ wfAssociation.InstantiationUrl +"?List="+ new Guid(listId).ToString("B")+"&ID="+ itemId +"&ItemGuid="+ qmsDocItem.UniqueId.ToString("B")+"&TemplateID="+ wfAssociation.Id.ToString("B")+"&source="+ sourceUrl);                    
            Response.Redirect(url);
        }                
    }
    catch (Exception ex)
    { 

    }
    finally
    {

    }
}

Which are the statuses of the workflow should I consider to allow user to enable workflow? I will check the status of workflow prior to Response.Redirect() statement. But I don't which status should I consider for allowing user to start workflow and which status will not allow user to start workflow.

Please help.

1 Answer 1

0

Whenever we attach a workflow to a list it will add a new field which stores the status. The status values can be one of the enumeration value

  1. NotStarted
  2. FailedOnStart
  3. InProgress
  4. ErrorOccurred
  5. StoppedByUser
  6. Completed
  7. FailedOnStartRetrying
  8. ErrorOccurredRetrying
  9. ViewQueryOverflow
  10. Max

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflow.spworkflowstatus.aspx

You can use this code snippet

public string GetWorkflowStatus(SPListItem item, String workFlowName)
{
    SPWorkflowManager manager = item.Web.Site.WorkflowManager;
    foreach (SPWorkflow instance in manager.GetItemWorkflows(item))
    {
        if (instance.ParentAssociation.Name == workFlowName)
        {
            foreach (SPField field in instance.ParentList.Fields)
            {
                if (field is SPFieldWorkflowStatus)
                {
                    SPFieldWorkflowStatus statusField = (SPFieldWorkflowStatus)field;
                    if (statusField.Title == workFlowName)
                    {
                       int statusValue = int.Parse(item[statusField.StaticName].ToString());
                       return statusField.GetFieldValueAsHtml(statusValue);
                    }
                }
            }
        }
    }
    return String.Empty;
}
8
  • I got that sir, but how would I come to know by comparing above status, that workflow is able to run on the item and if the condition fails then workflow is not able to run on selected item?
    – users1100
    Oct 28, 2014 at 12:25
  • Actually I am not getting the point, which status should I compare to start workflow and if it fails then I should not start workflow on item.
    – users1100
    Oct 28, 2014 at 12:26
  • @users1100 before redirect call the method and pass DocItem and Workflow Name. This returns a string. Oct 28, 2014 at 12:27
  • If string is empty then I should start the workflow, if contains any value then I should not start the workflow right?
    – users1100
    Oct 28, 2014 at 12:29
  • if(status != "InProgress" && status != "max") then you can start the workflow Oct 28, 2014 at 12:31

Your Answer

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

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