当用户角色分配发生变化时触发Dynamics CRM 2016插件

本文关键字:Dynamics CRM 插件 2016 变化 用户 角色 分配 | 更新日期: 2023-09-27 18:17:20

是否有可能在Dynamics CRM 2016中有一个插件,当任何用户的角色分配发生变化时触发?

如果是,我该在什么消息和实体上注册这个插件?

当用户角色分配发生变化时触发Dynamics CRM 2016插件

您需要注册插件将消息,主实体和辅助实体关联为none。

在插件中,你需要检查上下文。MessageName("关联"或"解除关联")和上下文。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)当用户与安全角色关联时将工作。为此,您应该将插件注册为关联消息。

当用户与安全角色解除关联时,

将工作。为此,您应该注册插件来解除关联消息。

在这两个插件中,您应该检查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;

之后,你可以得到目标(用户)和RelatedEntities(安全角色),并做你的东西:

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