2016-02-12 32 views
2

我正在編寫一個由sdk消息步驟「任務分配」觸發並且異步運行的Dynamics CRM 2015插件。獲取當前運行系統作業的系統作業ID(異步操作實體)

Execute(IServiceProvider serviceProvider)的方法,我想尋找其他當前正在運行的系統作業,使用QueryExpression

QueryExpression systemjobq = new QueryExpression 
{ 
    EntityName = "asyncoperation", 
    ColumnSet = new ColumnSet("name", "createdon", "completedon", "regardingobjectid", "asyncoperationid", "startedon", "messagename", "statuscode"), 
    Criteria = new FilterExpression 
    { 
     Conditions = { 
         new ConditionExpression 
         { 
          AttributeName = "name", 
          Operator = ConditionOperator.Equal, 
          Values = { "My system jobs name" } 
         } 
        } 
    } 
}; 

因爲我不想找到「自己」 - 當前運行的系統的工作 - 我需要找出執行我的插件的當前正在運行的系統作業的asyncoperationid,這樣我就可以將它包含在我的搜索查詢表達式中。

如何找出當前正在運行的系統作業的asyncoperationid

回答

0

如果我理解正確,您希望查找異步插件當前正在執行的異步作業記錄。試試下面的擴展方法:

public static EntityCollection GetCurrentAsyncJob(this IOrganizationService service, ColumnSet cols, Guid EntityId, Guid OwnerId) { 

      QueryExpression systemjobq = new QueryExpression 
      { 
       EntityName = "asyncoperation", 
       ColumnSet = cols,//new ColumnSet("name", "createdon", "completedon", "regardingobjectid", "asyncoperationid", "startedon", "messagename", "statuscode"), 
       Criteria = new FilterExpression 
       { 
        Conditions = 
        { 
         new ConditionExpression 
         { 
          AttributeName = "regardingobjectid", 
          Operator = ConditionOperator.Equal, 
          Values = {EntityId} 
         }, 
         new ConditionExpression { 
         AttributeName = "statuscode", 
         Operator = ConditionOperator.In, 
         Values = {20} 
         }, 
         new ConditionExpression 
         { 
          AttributeName = "ownerid", 
          Operator = ConditionOperator.Equal, 
          Values = {OwnerId} 
         } 
        } 
       } 
      }; 
      systemjobq.Orders.Add(new OrderExpression { AttributeName = "createdon", OrderType = OrderType.Descending }); 
      return service.RetrieveMultiple(systemjobq); 
     } 

可以檢查你所得到的異步工作如下,其中service是在插件您IOrganization服務和taskId的ID是你的任務的ID,針對其插件正在運行,且OwnerId是插件的當前發起所有者:

 EntityCollection ents = service.GetCurrentAsyncJob(new ColumnSet(new []{"name", "createdon", "completedon", "regardingobjectid", "asyncoperationid", "startedon", "messagename", "statuscode"}), TaskId, OwnerId); 
      if (ents.Entities.Count > 0) 
       throw new InvalidPluginExecutionException(ents.Entities.FirstOrDefault().Id.ToString()); 
      else throw new InvalidPluginExecutionException("M? Really, No?"); 

這是你如何獲取當前用戶的插件,從你的插件上下文:

Guid userId = context.InitiatingUserId; 

該方法並不完美,因爲如果存在與此實體相關的作業,它將返回多個作業,並且同時觸發相同任務的作業。我不確定,也沒有測試過,但我認爲如果你的插件選擇在同一個用戶下運行,這隻會是一個問題。

請注意,擴展方法必須放置在靜態類中。

注2:等於20的狀態表示異步作業仍在運行,並且處於InProgress狀態。

請試試看看它是否適合你。

+1

嗨Bojan,thx爲您的答覆。實際上,這不完全是我要歸檔的內容。我的意圖是找到所有其他當前正在運行的系統作業,它們不是當前正在運行的系統作業,我正在運行我的代碼。我發現的是,您可以從上下文中獲取當前正在運行的「線程」的asyncobjectid, context.OperationId'。 –