Create word file from html and send it as attachment via plugin in CRM online

Here is one more requirement of sending a document as attachment in email via plugin or custom workflow.

The user requirement was to export the report in word file and send that via email. But as we all know in crm online we can’t access the report server so that option is not there. But yes, we can generate a dynamic html inside code and convert it to word file to send it via email.

So here is the code for that.

Get the record field from context to make the html

string fullname = lead.Attributes.Contains(“fullname”) == false ? “” : lead.Attributes[“fullname”].ToString();
string address1_line1 = lead.Attributes.Contains(“address1_line1”) == false ? “” : lead.Attributes[“address1_line1”].ToString();
string address1_line2 = lead.Attributes.Contains(“address1_line2”) == false ? “” : lead.Attributes[“address1_line2”].ToString();
string address1_city = lead.Attributes.Contains(“address1_city”) == false ? “” : lead.Attributes[“address1_city”].ToString();
string address1_stateorprovince = lead.Attributes.Contains(“address1_stateorprovince”) == false ? “” : lead.Attributes[“address1_stateorprovince”].ToString();
string address1_postalcode = lead.Attributes.Contains(“address1_postalcode”) == false ? “” : lead.Attributes[“address1_postalcode”].ToString();

string myString = “<html><head></head><body><p><strong>” + fullname + “</strong></p>”
+”<p>” + address1_line1 + ” ” + address1_line2 + ” </p>”
+”<p>” + address1_city + ” ” + address1_stateorprovince + ” ” + address1_postalcode + “</p></body></html>”;

Email email = new Email
{
To = new ActivityParty[] { toParty1 },
From = new ActivityParty[] { fromParty },
Subject = SubjectCont,
Description = “Hi, This is a address document to print.”,
DirectionCode = true,
ActivityId= Guid.NewGuid()
};
Guid _emailId = service.Create(email);

System.Text.Encoding encoding1 = System.Text.Encoding.UTF8;
byte[] bytes = encoding1.GetBytes(myString);
Entity attachment = new Entity(“activitymimeattachment”);
attachment[“subject”] = SubjectCont;
attachment[“filename”] = FullName + “_Address.doc”;
byte[] fileStream = bytes;
attachment[“body”] = Convert.ToBase64String(fileStream);
attachment[“mimetype”] = “application/msword”;
attachment[“attachmentnumber”] = 1;
attachment[“objectid”] = new EntityReference(“email”, _emailId);
attachment[“objecttypecode”] = “email”;
service.Create(attachment);

Namaste 🙂

Megh

Advertisements

2 thoughts on “Create word file from html and send it as attachment via plugin in CRM online

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s