如果我理解正确,您希望查找异步插件当前正在执行的异步作业记录。试试下面的扩展方法:
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状态。
请试试看看它是否适合你。
嗨Bojan,thx为您的答复。实际上,这不完全是我要归档的内容。我的意图是找到所有其他当前正在运行的系统作业,它们不是当前正在运行的系统作业,我正在运行我的代码。我发现的是,您可以从上下文中获取当前正在运行的“线程”的asyncobjectid, context.OperationId'。 –