2016-02-12 47 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'。 –