Sunday, January 18, 2015

Maintaining Team Membership in Private Queues with a Plugin

The Spring 2014 release of Dynamics CRM introduced private queues prior to this all queues were public and visible to all users. Now you can control which users can see a private queue by adding them as members of that queue. When adding members to a queue you have the option to add a team in which case the team members are added as members of the queue.  The problem is that any subsequent changes in the teams membership are not reflected in the queues membership.

A way around this is to create a custom entity to link queues to teams and to write a plugin that runs whenever a team's memberships changes. The plugin needs to fire when the Associate and Disassociate events are raised and a user is added or removed from a team.  The Associate and Disassociate events are not linked to any specific object type as you can see on the registration screen so you need check in your code that the event relates to team membership.

Below is some sample plugin code that fires when a new user is added to a team and the associate event is triggered. The code checks if it's a team member relationship event and then it searches a custom entity called new_queueteams for the queues that the team is a member of.

  1. public void Execute(IServiceProvider serviceProvider)
  2. {
  3.     ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
  4.     IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext));
  5.     IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
  6.     IOrganizationService _service = serviceFactory.CreateOrganizationService(context.UserId);
  8.     if (context.MessageName == "Associate" && context.InputParameters.Contains("Relationship"))
  9.     {
  10.         Relationship relationship = (Relationship)context.InputParameters["Relationship"];
  12.         if (relationship.SchemaName != "teammembership_association")
  13.             return;
  15.         EntityReference target = (EntityReference)context.InputParameters["Target"];
  16.         EntityReferenceCollection coll = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
  17.         EntityReference member = coll[0];
  19.         QueryByAttribute qba = new QueryByAttribute("new_queueteams");
  20.         qba.ColumnSet = new ColumnSet("new_queueid");
  21.         qba.Attributes.AddRange("new_teamid");
  22.         qba.Values.AddRange(target.Id);
  23.         EntityCollection retrieved = _service.RetrieveMultiple(qba);
  25.         //Iterate the collection of queues and add the new team member
  27.     }
  29. }


No comments:

Post a Comment