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

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.