更改用户角色分配时触发 Dynamics CRM 2016 插件
Dynamics CRM 2016 Plugin triggered when user role assignments are changed
Dynamics CRM 2016 中是否有可能在任何用户的角色分配发生更改时触发插件?
如果是这样,我将在什么消息和实体上注册此插件?
您需要注册插件以将消息、主要和次要实体关联为 none。
在插件中,您需要检查 context.MessageName("Associate" 或 "Disassociate")和 context.InputParameters[“Relationship”](我们正在寻找 "systemuserroles_association")
检查条件的代码应该是这样的
//all usual plugin stuff here
if (context.InputParameters.Contains("Relationship")) {
relationshipName = context.InputParameters["Relationship"].ToString();
}
// Check the “Relationship Name” with your intended one
if (relationshipName != "systemuserroles_association") {
return;
}
if (context.MessageName == "Associate") {
//logic when role added
}
if (context.MessageName == "Disassociate") {
//logic when role removed
}
else {
//not interested
}
我还没有编译代码,但它应该让你知道如何继续。
您需要注册两个个插件:
1) 将在用户 关联 与安全角色时起作用。为此,您应该注册插件以关联消息。
2) 将在用户 解除 与安全角色的关联时起作用。为此,您应该注册插件以取消关联消息。
在这两个插件中,您应该检查 systemuserroles_association 关系名称,如下所示:
if (localContext.PluginExecutionContext.InputParameters.Contains("Relationship"))
relationshipName = ((Relationship)localContext.PluginExecutionContext.InputParameters["Relationship"]).SchemaName;
else return;
if (!relationshipName.Equals("systemuserroles_association", StringComparison.InvariantCultureIgnoreCase))
return;
之后您可以获得目标(用户)和相关实体(安全角色)并执行您的操作:
// Target
if (localContext.PluginExecutionContext.InputParameters.Contains("Target") && localContext.PluginExecutionContext.InputParameters["Target"] is EntityReference)
{
EntityReference targetEntity = (EntityReference)localContext.PluginExecutionContext.InputParameters["Target"];
userId = targetEntity.Id;
}
// RelatedEntities
if (localContext.PluginExecutionContext.InputParameters.Contains("RelatedEntities") && localContext.PluginExecutionContext.InputParameters["RelatedEntities"] is EntityReferenceCollection)
{
// Collection of SecurityRoles
EntityReferenceCollection relatedEntities = localContext.PluginExecutionContext.InputParameters["RelatedEntities"] as EntityReferenceCollection;
}
Dynamics CRM 2016 中是否有可能在任何用户的角色分配发生更改时触发插件?
如果是这样,我将在什么消息和实体上注册此插件?
您需要注册插件以将消息、主要和次要实体关联为 none。
在插件中,您需要检查 context.MessageName("Associate" 或 "Disassociate")和 context.InputParameters[“Relationship”](我们正在寻找 "systemuserroles_association")
检查条件的代码应该是这样的
//all usual plugin stuff here
if (context.InputParameters.Contains("Relationship")) {
relationshipName = context.InputParameters["Relationship"].ToString();
}
// Check the “Relationship Name” with your intended one
if (relationshipName != "systemuserroles_association") {
return;
}
if (context.MessageName == "Associate") {
//logic when role added
}
if (context.MessageName == "Disassociate") {
//logic when role removed
}
else {
//not interested
}
我还没有编译代码,但它应该让你知道如何继续。
您需要注册两个个插件:
1) 将在用户 关联 与安全角色时起作用。为此,您应该注册插件以关联消息。
2) 将在用户 解除 与安全角色的关联时起作用。为此,您应该注册插件以取消关联消息。
在这两个插件中,您应该检查 systemuserroles_association 关系名称,如下所示:
if (localContext.PluginExecutionContext.InputParameters.Contains("Relationship"))
relationshipName = ((Relationship)localContext.PluginExecutionContext.InputParameters["Relationship"]).SchemaName;
else return;
if (!relationshipName.Equals("systemuserroles_association", StringComparison.InvariantCultureIgnoreCase))
return;
之后您可以获得目标(用户)和相关实体(安全角色)并执行您的操作:
// Target
if (localContext.PluginExecutionContext.InputParameters.Contains("Target") && localContext.PluginExecutionContext.InputParameters["Target"] is EntityReference)
{
EntityReference targetEntity = (EntityReference)localContext.PluginExecutionContext.InputParameters["Target"];
userId = targetEntity.Id;
}
// RelatedEntities
if (localContext.PluginExecutionContext.InputParameters.Contains("RelatedEntities") && localContext.PluginExecutionContext.InputParameters["RelatedEntities"] is EntityReferenceCollection)
{
// Collection of SecurityRoles
EntityReferenceCollection relatedEntities = localContext.PluginExecutionContext.InputParameters["RelatedEntities"] as EntityReferenceCollection;
}