Wednesday, July 11, 2012

Creating Email through Plugin / SDK in CRM 4 - line breaks don't work


You may have noticed when you create email using a sample code like below in a plugin or for that matter any other SDK based assembley for MS CRM. Your line breaks : \n or \r or System.Environment.NewLine will not work.


          email Email = new email();   
                     Email.to = new activityparty[] { toParty };
                     Email.from = new activityparty[] { fromParty };
                     Email.regardingobjectid = regardingObject;
                     Email.subject = "Hello!!";
                     Email.description = "Dear, line 1" +         
                     System.Environment.NewLine+  
                      "my message for your - line 2";
                     Email.directioncode = new CrmBoolean();
                     Email.directioncode.Value = true;
                     
                     Guid _emailId = _service.Create(Email);
                     
                     SendEmailRequest sendEmailRequest = new SendEmailRequest();
                     sendEmailRequest.EmailId = _emailId;
                     sendEmailRequest.TrackingToken = "";
                     sendEmailRequest.IssueSend = true;


I went into the business entity object that service will return when you query existing Emails in MSCRM, the .description field will have value like HTML rich text code :

.description = " < FONT size=2 face= \"Tahoma, Verdana, Arial \ " >your text < br / >"


So the key is to add "< br / >" as line feed instead of the common dot net and javascript carriage returns and you will be able to format your emails as you want from inside SDK plugins and more.

3 comments:

Ross said...

Can you show an example of where you put the
in your code above?

hero said...

Hi Ross,
Sorry not at the moment but if you have a large email body with many paragraphs you can see how line breaks work.

Microsoft CRM Solutions said...

This article was very helpful in answering alot of questions.Thanks for posting.