If you use a K2 SmartObject lookup for assigning email addresses dynamically in a K2 Email Event and the source returns duplicate addresses, these are displayed in the in the To/Cc fields of the email. In this blog I will show you how to remove these duplicates.
STEP 1: Open the workflow .kprx file in Visual Studio
Please note, this technique will only work for K2 processes built in K2 for Visual Studio.
The K2 process needs to be opened in Visual Studio in order to make changes to the code behind.
STEP 2: Edit the code
Right click on the MailEvent, hover over View Code and select Event Item.
STEP 3: Open the class file
Press F7 or right click and click View Code in the window below. A new tab will open the Mailevent.Xoml.cs file.
STEP 4: Add a new system reference
Add the refrence: using System.Linq;
STEP 5: Add the following code
Go to the method below and add the code as shown below in the yellow sections highlighted
Notice the Linq “Distinct()” extension method is used to remove duplicates
STEP 6: Save and deploy your K2 process
Save the .cs file and deploy the workflow process. You can now test the email by starting a new instance of the K2 process.
Notice you will receive an email with disticnt recipients.
This simple method will help avoid duplicate recipients in an outgoing email sent by a K2 Email Event.