Creating Forward Email through SDK
Here's the sample code to create Forward Email through SDK
private static Guid CreateForwardEmail(Guid existingEmailID)
{
Entity existingEmail = _service.Retrieve("email", existingEmailID, new ColumnSet("from", "createdon", "to", "cc", "subject", "description", "regardingobjectid"));
Entity oEmail = new Entity("email");
oEmail["regardingobjectid"] = existingEmail["regardingobjectid"];
oEmail["subject"] = "FW: " + existingEmail["subject"];
oEmail["description"] = "<FONT size=2 face='Tahoma, Verdana, Arial'><BR>------------------- Original Message -------------------<BR>";
oEmail["description"] += "<B>From:</B> " + ((EntityReference)((Microsoft.Xrm.Sdk.EntityCollection)existingEmail["from"]).Entities[0]["partyid"]).Name + "<BR>";
oEmail["description"] += "<B>Received:</B> " + ((DateTime)existingEmail["createdon"]).ToString("d/M/yyyy h:m tt") + "<BR>";
oEmail["description"] += "<B>To:</B> ";
EntityCollection toParties = (Microsoft.Xrm.Sdk.EntityCollection)existingEmail["to"];
bool firstRecord = true;
foreach (Entity to in toParties.Entities)
{
if (firstRecord)
firstRecord = false;
else
oEmail["description"] += "; ";
oEmail["description"] += ((EntityReference)to["partyid"]).Name;
}
oEmail["description"] += "<BR>";
oEmail["description"] += "<B>Cc:</B> ";
EntityCollection ccParties = (Microsoft.Xrm.Sdk.EntityCollection)existingEmail["cc"];
firstRecord = true;
foreach (Entity cc in ccParties.Entities)
{
if (firstRecord)
firstRecord = false;
else
oEmail["description"] += "; ";
oEmail["description"] += ((EntityReference)cc["partyid"]).Name;
}
oEmail["description"] += "<BR>";
oEmail["description"] += "<B>Subject:</B> " + existingEmail["subject"].ToString() + "</FONT><BR><BR>";
oEmail["description"] += "<FONT size=2 face='Tahoma, Verdana, Arial'>" + existingEmail["description"].ToString() + "</FONT>";
Guid newEmailID = _service.Create(oEmail);
QueryByAttribute attachmentQuery = new QueryByAttribute("activitymimeattachment");
attachmentQuery.ColumnSet = new ColumnSet("objecttypecode", "mimetype", "filename", "body");
attachmentQuery.AddAttributeValue("objectid", existingEmail.Id);
attachmentQuery.AddAttributeValue("objecttypecode", "email");
EntityCollection ecAttachments = _service.RetrieveMultiple(attachmentQuery);
foreach (Entity attachment in ecAttachments.Entities)
{
attachment.Id = Guid.Empty;
attachment.Attributes.Remove("activitymimeattachmentid");
attachment["objectid"] = new EntityReference("email", newEmailID);
_service.Create(attachment);
}
return newEmailID;
}
Comments
Post a Comment