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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Xrm.Sdk;
- using System.ServiceModel.Description;
- using Microsoft.Xrm.Sdk.Query;
- using System.Runtime.Serialization;
-
- namespace Dynamics_365_Portal.EmailLookupForContactsNLead
- {
- public class EmailLookup IPlugin
- {
-
- public void Execute(IServiceProvider serviceProvider)
- {
- string eventRegEmail = "";
-
- ITracingService tracingService =
- (ITracingService)serviceProvider.GetService(typeof(ITracingService));
-
-
- Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
- serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
-
- IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
-
- IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);
-
-
- if (context.InputParameters.Contains("Target") &&
- context.InputParameters["Target"] is Entity)
- {
-
-
- Entity entity = (Entity)context.InputParameters["Target"];
-
- #region Core Operations
-
- if (entity.LogicalName == "new_eventregistration")
- {
-
-
- if (entity.Attributes.Contains("new_email"))
- {
- eventRegEmail = entity.Attributes["new_email"].ToString();
-
- #region contact lookup
- EntityCollection ContactsEC = GetRelatedEntity(orgService, "contact", "emailaddress1", eventRegEmail, new ColumnSet("fullname", "contactid","emailaddress1"));
- if (ContactsEC.Entities.Count >= 1)
- {
- entity.Attributes["new_relatedcontact"] = new EntityReference("contact", ContactsEC.Entities[0].Id) ;
- }
- #endregion contact lookup
-
- #region lead lookup
- else
- {
- EntityCollection LeadsEC = GetRelatedEntity(orgService, "lead", "emailaddress1", eventRegEmail, new ColumnSet("fullname", "leadid", "companyname"));
- if (LeadsEC.Entities.Count >= 1)
- {
- entity.Attributes["new_relatedlead"] = new EntityReference("lead", LeadsEC.Entities[0].Id);
- }
- }
- #endregion lead lookup
- }
-
- }
- #endregion Core Operations
- }
-
- }
-
- #region lookup method
-
- private static EntityCollection GetRelatedEntity(IOrganizationService orgSvc, string entityName, string searchItem, string searchItemValue, ColumnSet colSet)
- {
- QueryExpression query = new QueryExpression
- {
- EntityName = entityName,
- ColumnSet = colSet,
- Criteria = new FilterExpression
- {
- Conditions =
- {
- new ConditionExpression
- {
- AttributeName = searchItem,
- Operator = ConditionOperator.Equal,
- Values = { searchItemValue }
- }
- }
- }
- };
-
- return orgSvc.RetrieveMultiple(query);
-
- }
- #endregion lookup method
-
- }
- }
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.