Automating Web Role Assignments in Dynamics 365 with a Custom Plugin

Purpose of the Plugin

The plugin aims to streamline the user management process by automatically assigning a default web role to every new contact created in Dynamics 365. This is particularly useful in environments where new contacts need immediate and predefined access to certain parts of the system, ensuring they have the necessary permissions from the start.

Prerequisites

Before you begin, ensure you have:

  • Access to your Dynamics 365 instance.
  • Appropriate permissions to write and register plugins.
  • The Dynamics 365 SDK installed, as it contains tools and libraries needed for plugin development.

Plugin Development Guide

The plugin is developed in C#, using the Dynamics 365 SDK. Below is the step-by-step process and the code needed to create and deploy this plugin.

  1. Setup Your Development Environment

    • Ensure you have Visual Studio and the Dynamics 365 SDK properly configured.
    • Create a new Class Library project in Visual Studio.
  2. Write the Plugin Code

    • Here is the complete code for the AssignWebRole plugin:
      using System;
      using System.Linq;
      using Microsoft.Xrm.Sdk;
      using Microsoft.Xrm.Sdk.Query;

      namespace Contact.Plugins
      {
          public class AssignWebRole : IPlugin
          {
              public void Execute(IServiceProvider serviceProvider)
              {
                  IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                  IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                  try
                  {
                      if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                      {
                          Entity targetContact = (Entity)context.InputParameters["Target"];
                          if (targetContact.LogicalName != "contact" || context.MessageName.ToLower() != "create")
                              return; // Exit if not creating a contact.

                          // Query to find the Web Role named 'Authenticated Users'
                          QueryExpression roleQuery = new QueryExpression("mspp_webrole");
                          roleQuery.ColumnSet.AddColumns("mspp_webroleid", "mspp_name");
                          roleQuery.Criteria.AddCondition("mspp_name", ConditionOperator.Equal, "Authenticated Users");

                          EntityCollection roles = service.RetrieveMultiple(roleQuery);

                          if (roles.Entities.Count > 0)
                          {
                              Entity webRole = roles.Entities.FirstOrDefault();

                              // Use the Associate method to link the contact with the web role
                              Relationship relationship = new Relationship("powerpagecomponent_mspp_webrole_contact");
                              EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
                              relatedEntities.Add(new EntityReference("mspp_webrole", webRole.Id));

                              service.Associate("contact", targetContact.Id, relationship, relatedEntities);
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      throw new InvalidPluginExecutionException("An error occurred in the AssignWebRole plugin.", ex);
                  }
              }
          }
      }

  3. Register the Plugin

    • Use the Plugin Registration Tool from the Dynamics 365 SDK to register the AssignWebRole plugin.
    • Configure the plugin to trigger on the "Create" message of the "contact" entity.

No comments:

Post a Comment