locked
How can I Resume,Cancel or Pause an workflow type systemjob (asyncoperation) programmatically RRS feed

  • Question

  • Hi,

    How can I Resume,Cancel or Pause an workflow type systemjob (asyncoperation) programmatically ?

    Thanks

    Tuesday, August 10, 2010 1:24 PM

Answers

  • Have a look at the TargetUpdateAsyncOperation class; there's an example of how to cancel a workflow here.
    --pogo (pat)
    Tuesday, August 10, 2010 10:07 PM
  • The most straightforward way to programatically "disable" the triggering of a workflow, would be to unpublish it:

    const int WorkflowStatusDraft = 1;
    const int WorkflowStatusPublished = 2;
    
    public void PublishWorkflow(Guid workflowId)
    {
      SetStateWorkflowRequest publishRequest = new SetStateWorkflowRequest();
      publishRequest.EntityId = workflowId;
      publishRequest.WorkflowState = WorkflowState.Published;
      publishRequest.WorkflowStatus = WorkflowStatusPublished;
    
      this.CrmService.Execute(publishRequest);
    }
    
    public void UnpublishWorkflow(Guid workflowId)
    {
      SetStateWorkflowRequest unpublishRequest = new SetStateWorkflowRequest();
      unpublishRequest.EntityId = workflowId;
      unpublishRequest.WorkflowState = WorkflowState.Draft;
      unpublishRequest.WorkflowStatus = WorkflowStatusDraft;
    
      this.CrmService.Execute(unpublishRequest);
    }
    
    

    --pogo (pat)
    Wednesday, August 11, 2010 10:26 PM

All replies

  • Have a look at the TargetUpdateAsyncOperation class; there's an example of how to cancel a workflow here.
    --pogo (pat)
    Tuesday, August 10, 2010 10:07 PM
  • Also is there a way to stop triggering a workflow ?
    Wednesday, August 11, 2010 12:14 PM
  • The most straightforward way to programatically "disable" the triggering of a workflow, would be to unpublish it:

    const int WorkflowStatusDraft = 1;
    const int WorkflowStatusPublished = 2;
    
    public void PublishWorkflow(Guid workflowId)
    {
      SetStateWorkflowRequest publishRequest = new SetStateWorkflowRequest();
      publishRequest.EntityId = workflowId;
      publishRequest.WorkflowState = WorkflowState.Published;
      publishRequest.WorkflowStatus = WorkflowStatusPublished;
    
      this.CrmService.Execute(publishRequest);
    }
    
    public void UnpublishWorkflow(Guid workflowId)
    {
      SetStateWorkflowRequest unpublishRequest = new SetStateWorkflowRequest();
      unpublishRequest.EntityId = workflowId;
      unpublishRequest.WorkflowState = WorkflowState.Draft;
      unpublishRequest.WorkflowStatus = WorkflowStatusDraft;
    
      this.CrmService.Execute(unpublishRequest);
    }
    
    

    --pogo (pat)
    Wednesday, August 11, 2010 10:26 PM
  • I need to unpublish all workflows prior to importing them using the sdk.  This is to allow them to be updated during our release process.  Do you know how I can retrieve a list of all published workflows in the sdk?  I'll use your unpublish/publish routines above to loop through them.

    Thanks,


    Tim
    Monday, June 6, 2011 11:33 PM
  • ConditionExpression condition = new ConditionExpression("statuscode", ConditionOperator.Equal, 2);  // 2 == Published, 1 == Draft
    
    FilterExpression filter = new FilterExpression();
    filter.AddCondition(condition);
    
    QueryExpression query = new QueryExpression(EntityName.workflow.ToString());
    query.Criteria = filter;
    query.ColumnSet = new ColumnSet(new string[] { "name" });
    
    RetrieveMultipleRequest req = new RetrieveMultipleRequest();
    req.Query = query;
    req.ReturnDynamicEntities = false;
    
    RetrieveMultipleResponse resp = (RetrieveMultipleResponse)crmService.Execute(req);
    
    foreach (workflow wf in resp.BusinessEntityCollection.BusinessEntities)
    {
    	System.Diagnostics.Debug.Print("{0}: {1}", wf.workflowid.Value, wf.name);
    }
    
    

    --pogo (pat) @ pogo69.wordpress.com
    • Proposed as answer by Tim Klooster Tuesday, June 7, 2011 12:19 AM
    Tuesday, June 7, 2011 12:12 AM
  • Pat,

    Perfect.  Thanks for sending this!

     


    Tim
    Tuesday, June 7, 2011 12:18 AM
  • Hi Pat

    If I want to cancel running instances of workflow, can I pass 31 as workflow status to cancel running instances of workflow ?

    Thursday, March 29, 2012 10:40 AM