获取当前 运行 系统作业的系统作业 ID(AsyncOperation 实体)

Get systemjob id of current running system job (AsyncOperation Entity)

我正在编写一个 Dynamics CRM 2015 插件,它由 sdk 消息步骤 "assign of Task" 触发并异步运行。

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" }
                        }
                     }
    }
};

因为我不想找到"myself" - 当前运行系统作业 - 我需要找出当前运行系统作业的asyncoperationid执行我的插件,所以我可以将它包含在我的搜索查询表达式中。

如何找到当前运行系统作业的asyncoperationid

如果我没理解错的话,您想查找您的异步插件当前正在执行的异步作业的记录。尝试以下扩展方法:

   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);
        }

您可以检查您是否正在获取异步作业的 ID,如下所示,其中 service 是插件中的 IOrganization 服务,taskId 是您的任务的 ID,插件当前为 运行ning,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;

该方法并不完美,因为它会 return 多个与此实体相关的作业,并且同时为同一任务触发分配。我不确定也没有对此进行测试,但我认为只有在同一用户下将您的插件选择为 运行 才会出现问题。

注意扩展方法一定要放在静态的class.

注 2:状态等于 20 表示异步作业仍在 运行ning 和进行中。

请尝试看看是否适合您。