title: CRM 2011, Stopping custom workflow programmatically
body: <p>i've been trying to stop a workflow programmatically.
  I've read both in various posts and in the msdn that this can be done by updating
  the Asyncoperation status via update request.
  However everytime i update the request. the workflow get stuck on a mid stage such as cancelling or pausing and dosen't reach a final state. </p>
  
  <p>any ideas?</p>
  
  <pre><code>protected void ExecutePostAccountUpdate(LocalPluginContext localContext)
      {
          if (localContext == null)
          {
              throw new ArgumentNullException("localContext");
          }
  
          string fetchXML = "&lt;fetch mapping='logical' count='50' version='1.0'&gt;" +
                            "&lt;entity name='asyncoperation'&gt;" +
                            "&lt;filter&gt;" +
                            "&lt;condition attribute='regardingobjectid' operator='eq' value='" +
                            localContext.PluginExecutionContext.PrimaryEntityId + "' /&gt;" +
                            "&lt;/filter&gt;" +
                            "&lt;/entity&gt;" +
                            "&lt;/fetch&gt;";
          EntityCollection col = localContext.OrganizationService.RetrieveMultiple(new FetchExpression(fetchXML));
          if (col.Entities.Count &gt; 0)
          {
              AsyncOperation a = (AsyncOperation)col[0];
              a.StateCode = AsyncOperationState.Completed;
              a.StatusCode = new OptionSetValue(32);
              localContext.OrganizationService.Update(a);
  
  
  
          }
  
      }
  </code></pre>