Auto Filling Event Registration Form In Dynamics 365 Portal

To find the contact entity and lead matching the email address, set the reference to Event Registration Form in Dynamics 365 portal.

I have got a requirement from the client, which says “Event Registration Form” has a “Related Contact and Lead”, which should be automatically filled, if it has an email address matching “Event Registration Form – email address”. All these should happen when the user registers for an event from “Dynamics 365 – Customer Self Service Portal”.

Steps to achieve this are given below.

Step 1

Create a plugin to find out the email address, which has any matching contact or lead and set the reference to Related Contact and Lead.

Go to Visual Studio and create a Class Library project, as shown below.


Step 2

Now, use the code given below to create plugin class like “EmailLookupForContactNLead.cs”, as shown in the step 1. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.Xrm.Sdk;  
  7. using System.ServiceModel.Description;  
  8. using Microsoft.Xrm.Sdk.Query;  
  9. using System.Runtime.Serialization;  
  10.   
  11. namespace Dynamics_365_Portal.EmailLookupForContactsNLead  
  12. {  
  13.     public class EmailLookup   IPlugin  
  14.     {  
  15.   
  16.         public void Execute(IServiceProvider serviceProvider)  
  17.         {  
  18.             string eventRegEmail = "";  
  19.             //Extract the tracing service for use in debugging sandboxed plug-ins.  
  20.             ITracingService tracingService =  
  21.                 (ITracingService)serviceProvider.GetService(typeof(ITracingService));  
  22.   
  23.             // Obtain the execution context from the service provider.  
  24.             Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)  
  25.                 serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));  
  26.   
  27.             IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));  
  28.   
  29.             IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);  
  30.   
  31.             // The InputParameters collection contains all the data passed in the message request.  
  32.             if (context.InputParameters.Contains("Target") &&  
  33.                 context.InputParameters["Target"] is Entity)  
  34.             {  
  35.   
  36.                 // Obtain the target entity from the input parameters.  
  37.                 Entity entity = (Entity)context.InputParameters["Target"];  
  38.                 //</snippetAccountNumberPlugin2>  
  39.                 #region Core Operations  
  40.                  
  41.                 if (entity.LogicalName == "new_eventregistration")  
  42.                 {  
  43.   
  44.                      
  45.                     if (entity.Attributes.Contains("new_email"))  
  46.                     {  
  47.                         eventRegEmail = entity.Attributes["new_email"].ToString();  
  48.                         // To check the Contact for matching email address  
  49.                         #region contact lookup  
  50.                         EntityCollection ContactsEC = GetRelatedEntity(orgService, "contact""emailaddress1", eventRegEmail, new ColumnSet("fullname""contactid","emailaddress1"));  
  51.                         if (ContactsEC.Entities.Count >= 1)  
  52.                         {  
  53.                             entity.Attributes["new_relatedcontact"] = new EntityReference("contact", ContactsEC.Entities[0].Id) ;//refContact;                             
  54.                         }  
  55.                         #endregion contact lookup  
  56.  
  57.                         #region lead lookup  
  58.                         else   
  59.                         {  
  60.                             EntityCollection LeadsEC = GetRelatedEntity(orgService, "lead""emailaddress1", eventRegEmail, new ColumnSet("fullname""leadid""companyname"));  
  61.                             if (LeadsEC.Entities.Count >= 1)  
  62.                             {  
  63.                                 entity.Attributes["new_relatedlead"] = new EntityReference("lead", LeadsEC.Entities[0].Id);//refLead;                                
  64.                             }  
  65.                         }  
  66.                         #endregion lead lookup  
  67.                     }  
  68.   
  69.                 }  
  70.                 #endregion Core Operations  
  71.             }  
  72.   
  73.         }  
  74.  
  75.         #region lookup method  
  76.         // Generic method to query Contact and lead based on the parameters supplied  
  77.         private static EntityCollection GetRelatedEntity(IOrganizationService orgSvc, string entityName, string searchItem, string searchItemValue, ColumnSet colSet)  
  78.         {  
  79.             QueryExpression query = new QueryExpression  
  80.             {  
  81.                 EntityName = entityName,  
  82.                 ColumnSet = colSet,  
  83.                 Criteria = new FilterExpression  
  84.                 {  
  85.                     Conditions =  
  86.                     {  
  87.                      new ConditionExpression  
  88.                         {  
  89.                         AttributeName = searchItem,  
  90.                         Operator = ConditionOperator.Equal,  
  91.                         Values = { searchItemValue }  
  92.                         }  
  93.                     }  
  94.                 }  
  95.             };  
  96.   
  97.             return orgSvc.RetrieveMultiple(query);  
  98.   
  99.         }  
  100.         #endregion lookup method  
  101.   
  102.     }  
  103. }   

Step 3

Now, build the solution and get the .dll file from bin folder, as shown below.




Step 4

Deploy .dll file to Dynamics 365, using Plugin-Registration Tool.


Note- It is available in Dynamics 365 SDK folder in the path mentioned above.

Step 5

Double click on PluginRegistration.exe. Follow the steps given below to register the plugin DLL.


Provide the Live Id or Microsoft account in the sign given below in the form.


Once the credentials are validated, you will see the list of plugins registered already.


Now, you need to click on the menu Register and select Register New Assembly.


Select the options as highlighted in Yellow color.

Now, right click on the registered assembly “EmailLookup4ContactNLead” to create a new step.


Finally configure the step to complete the plugin deployment.


Click Register New Step button at the bottom of the page.


Now, go to Dynamics 365 portal and try to register for the event, using Event Registration form.