How to capture adding or removing members to CRM teams? What is the plugin step to capture adding/removing members to a CRM team? AddingMembers/RemovingMembers steps or Associate/Disassociate steps can be used in the plugin?
Following messages are supported for teams in plugin registrations.
AddMembers/RemoveMembers
A plugin has been created and registered under the messages of AddMembers/RemoveMembers for the Team entity. Members have been added/removed from /to CRM teams, but no plugin was triggered. Then another plugin was registered with Associated/Disassociated steps under CRM teams.
Plugins registered under AddMembers/RemveMembers will only be fired when a member is added/removed through the SDK/programmatically.
using System;
using System.Xml;
using System.Data;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;namespace crmfortress.teamplugin
{
public class AddRemoveMembers : IPlugin
{
IOrganizationService organizationService;
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
organizationService = organizationServiceFactory.CreateOrganizationService(context.UserId);EntityReference target = (EntityReference)context.InputParameters[“Target”];
// Check the Message Name and the Target Entity
if (context.MessageName == “AddMembers” && (target.LogicalName == “team” || target.LogicalName == “user”))
{}
// Check the Message Name and the Target Entity
if (context.MessageName == “RemoveMembers” && (target.LogicalName == “team” || target.LogicalName == “user”))
{}
}
}
}
Associate/Disassociate
If we want to capture the event of adding/removing members from CRM front end, then the plugin should be registered under Associate/Disassociate steps.
using Microsoft.Xrm.Sdk;
using System;namespace crmfortress.teamplugin
{
public class AssociateDisassociateMembers : IPlugin
{
IOrganizationService organizationService;public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
organizationService = organizationServiceFactory.CreateOrganizationService(context.UserId);EntityReference target = (EntityReference)context.InputParameters[“Target”];
// Check the Message Name and the Target Entity
if (context.MessageName == “Associate” && (target.LogicalName == “team” || target.LogicalName == “user”))
{
// Get the ralationships in the context
Relationship relationShip = (Relationship)context.InputParameters[“Relationship”];// Get Related Entities in the context
EntityReferenceCollection relatedentities = (EntityReferenceCollection)context.InputParameters[“RelatedEntities”];foreach (EntityReference relatedEntity in relatedentities)
{
// Check Related Entity Logical & Schema Name
if (relatedEntity.LogicalName == “systemuser” && relationShip.SchemaName == “teammembership_association”)
{
// Get user id
//relatedEntity.Id;
}
}
}// Check the Message Name and the Target Entity
if (context.MessageName == “Disassociate” && target.LogicalName == “teammembership_association”)
{
// Get the ralationships in the context
Relationship relationShip = (Relationship)context.InputParameters[“Relationship”];// Get Related Entities in the context
EntityReferenceCollection relatedentities = (EntityReferenceCollection)context.InputParameters[“RelatedEntities”];foreach (EntityReference relatedEntity in relatedentities)
{
// Check Related Entity Logical & Schema Name
if (relatedEntity.LogicalName == “systemuser” && relationShip.SchemaName == “teammembership_association”)
{
// Get user id
//relatedEntity.Id;
}
}}
}
}
}