如果我理解正確,您希望查找異步插件當前正在執行的異步作業記錄。試試下面的擴展方法:
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'。 –