Skip to main content

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.

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.
This article was very helpful in answering alot of questions.Thanks for posting.
Anonymous said…
nicely written article about CRM India
Harsh Vardhan said…
This is a really authentic and informative blog. Share more posts like this.
Openspan Online Training
Openstack Online Training
Openstack Online Course

Popular posts from this blog

CRM 2011 Useful JavaScript tidbits

http://www.powerobjects.com/blog/2011/01/14/crm-2011-useful-javascript-tidbits/ Get the value from a CRM field var varMyValue = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getValue() ; Set the value of a CRM field Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’); Hide/Show a tab/section Xrm.Page.ui.tabs.get(5).SetVisible(false); Xrm.Page.ui.tabs.get(5).SetVisible(true); Call the onchange event of a field Xrm.Page.getAttribute(“CRMFieldSchemaName”).fireOnChange(); Get the selected value of picklist Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text; Set the requirement level Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“none”); Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“required”); Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“recommended”); Set the focus to a field Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true); Stop an on save event event.returnValue = false; Return array

Abort event through plugins

Suppose you are writing a pre -event plugin in which you compare to  old and new values and on certain condition would like to cancel the Save or  Update and stop the execution, it was simple in CRM 3 with an Abort() method while in CRM 4, you have to manually raise and throw a  Invalid-Plugin-Exception, while this gives a dirty error message on your MSCRM screen, you can make it better by entering you own Message explaining why the record could not be saved: throw new InvalidPluginExecutionException("Execution has been stopped due to this reason");

Using MS CRM Calendar in your custom ASP .NET pages *** Not fully Supported Customisations ***

Hi, Have you been creating custom pages in line with MS CRM for creating , viewing and updating entity records ? Have you placed controls such as Date picker Lookup on your custom page and have tried in vein to make those look like MS CRM ? I found a way to use the same MS CRM calendar control as it appears in the application... For getting this calendar on your asp .net page, follow the steps below: 1) Create a copy of the date.js javascript file located in your-server/crmsite-folder/_static/_controls/datetime folder, give the copy some name like date_myapp.js. 2) Open the following .js files from different sub folders in side the  your-server/crmsite-folder/_static      folder :       Global.js       encodedecode.js       xmlutil.js       util.js       remotecommand.js              Copy and whole content of each file one by one and keep pasting the same to the end of your date_myapp.js file. 3) Now open any CRM entity record page, for example a new Account crea