更改用户角色分配时触发Dynamics CRM 2016插件

时间:2016-11-10 12:06:14

标签: c# dynamics-crm dynamics-crm-2016

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

如果是这样,我会在哪个消息和实体上注册此插件?

2 个答案:

答案 0 :(得分:3)

您需要将插件注册为Associate message,primary和secondary entity为none。

在插件中,您需要检查context.MessageName(" Associate"或" Disassociate")和context.InputParameters [“Relationship”](我们正在寻找&#34 ; systemuserroles_association&#34)

检查条件的代码就是那样

//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 :(得分:0)

您需要注册两个插件:

1)当用户关联与安全角色时,将会有效。为此,您应该将插件注册到Associate message。

2)当用户取消关联与安全角色时,将会有效。为此,您应该将插件注册为Disassociate消息。

在这两个插件中,您应该检查 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(用户)和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;
}