I'm not sure how you are going trigger this plugin but here is the updated code to generate campaign response URL and adding them to dictinary. All you have to do create and send email for all the items from the dictionary
private static string UrlFormat = @"{0}/main.aspx?etn={1}&pagetype=entityrecord&id=%7B{2}%7D";
private static string HyperLinkFormat = @"<a href='{0}'>{1}</a>";
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.InitiatingUserId);
var fetchXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"> " +
" <entity name=\"campaignresponse\"> " +
" <attribute name=\"subject\" /> " +
" <attribute name=\"activityid\" /> " +
" <attribute name=\"ownerid\" /> " +
" <attribute name=\"regardingobjectid\" /> " +
" <attribute name=\"responsecode\" /> " +
" <attribute name=\"description\" /> " +
" <order attribute=\"subject\" descending=\"false\" /> " +
" <filter type=\"and\"> " +
" <condition attribute=\"createdon\" operator=\"last-x-days\" value=\"2\" /> " +
" </filter> " +
" </entity> " +
"</fetch> ";
var response = service.RetrieveMultiple(new FetchExpression(fetchXml));
var OwerWithCampaignResUrl = new Dictionary<Guid,StringBuilder>();
foreach (var entity in response.Entities)
{
string url = string.Format(CultureInfo.InvariantCulture, UrlFormat,
@"https://org.crm.dynamics.com"", // base url like https://org.crm.dynamics.com
entity.LogicalName, //lead
entity.Id); //leadid
string htmlHyperLink = string.Format(CultureInfo.InvariantCulture, HyperLinkFormat,
url,
"Campaign Link");
urlLinks.Append(htmlHyperLink).AppendLine();
if (OwerWithCampaignResUrl.ContainsKey(entity.Id))
{
OwerWithCampaignResUrl[entity.Id].Append(htmlHyperLink).AppendLine();
}
else
{
OwerWithCampaignResUrl.Add(entity.Id, new StringBuilder(htmlHyperLink));
OwerWithCampaignResUrl[entity.Id].AppendLine();
}
}
foreach (var item in OwerWithCampaignResUrl)
{
//Create email and send it to each owner
}
}