Sample code to use Template’s content while sending mail using SendEmailReuquest in CRM 2011


At times we would like to send email using SendEmailRequest but still want to make use of template and replace the content in it.

For e.g. this is our sample template (Global template)

Here we would replace the [subject] and [url] placeholders.

Sample Code used

<p>private void SendMail(object sender, EventArgs e)<br />
 {<br />
 IOrganizationService orgService = GetOrganizationService();<br />
 Entity entity = GetGlobalTemplate("My Test Template", GetOrganizationService());<br />
 Entity email = new Entity();<br />
 email.LogicalName = "email";</p>
<p>// get the Subject content from template and replace it with "My New Subject"<br />
 email.Attributes["subject"] = GetDataFromXml(entity.Attributes["subject"].ToString(), "match");<br />
 email.Attributes["subject"] = email.Attributes["subject"].ToString().Replace("[subject]", "My New Subject");</p>
<p>// get the description from template and replace [url] placeholder with a bing's url<br />
 email.Attributes["description"] = GetDataFromXml(entity.Attributes["body"].ToString(), "match");<br />
 string urlToReplace = "&lt;a href=''&gt;Open Bing&lt;/a&gt;";<br />
 email.Attributes["description"] = email.Attributes["description"].ToString().Replace("[url]", urlToReplace);</p>
<p>List&lt;Entity&gt; fromtoEntities = new List&lt;Entity&gt;();<br />
 Entity activityParty = new Entity();<br />
 activityParty.LogicalName = "activityparty";<br />
 activityParty.Attributes["partyid"] = new EntityReference("systemuser", new Guid("FC480E73-77A9-E111-B151-00155D882D40"));<br />
 fromtoEntities.Add(activityParty);<br />
 email.Attributes["from"] = fromtoEntities.ToArray();<br />
 email.Attributes["to"] = fromtoEntities.ToArray();<br />
 email.Attributes["regardingobjectid"] = new EntityReference("contact", new Guid("2CCB57AA-A9B3-E111-B151-00155D882D40"));</p>
<p>Guid emailCreated = orgService.Create(email);<br />
 SendEmailRequest req = new SendEmailRequest();<br />
 req.EmailId = emailCreated;<br />
 req.TrackingToken = "";<br />
 req.IssueSend = true;<br />
 SendEmailResponse res = (SendEmailResponse)orgService.Execute(req);<br />
<p>private static string GetDataFromXml(string value, string attributeName)<br />
 {<br />
 if (string.IsNullOrEmpty(value))<br />
 {<br />
 return string.Empty;<br />
<p>XDocument document = XDocument.Parse(value);<br />
 // get the Element with the attribute name specified<br />
 XElement element = document.Descendants().Where(ele =&gt; ele.Attributes().Any(attr =&gt; attr.Name == attributeName)).FirstOrDefault();<br />
 return element == null ? string.Empty : element.Value;<br />
<p>public static Entity GetGlobalTemplate(string title, IOrganizationService orgService)<br />
 {<br />
 Entity emailTemplate = null;<br />
 try<br />
<p>QueryExpression query = new QueryExpression();</p>
<p>// Setup the query for the template entity<br />
 query.EntityName = "template";</p>
<p>// Return all columns<br />
 query.ColumnSet.AllColumns = true;<br />
 query.Criteria = new FilterExpression();<br />
 query.Criteria.FilterOperator = LogicalOperator.And;</p>
<p>// Create the title condition<br />
 ConditionExpression condition1 = new ConditionExpression();<br />
 condition1.AttributeName = "title";<br />
 condition1.Operator = ConditionOperator.Equal;<br />
<p>// Execute the query and return the result<br />
 EntityCollection entityColl = orgService.RetrieveMultiple(query);</p>
<p>if (entityColl.Entities.Count &gt; 0)<br />
 {<br />
 emailTemplate = entityColl.Entities[0];<br />
 }<br />
 }<br />
 catch<br />
 {<br />
 throw;<br />
 }<br />
 return emailTemplate;<br />

The email activity created :-



Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

4 thoughts on “Sample code to use Template’s content while sending mail using SendEmailReuquest in CRM 2011”

    1. Hi Madhu,
      List fromtoEntities = new List();

      Entity activityParty = new Entity();
      activityParty.LogicalName = “activityparty”;
      activityParty.Attributes[“partyid”] = new EntityReference(“systemuser”, new Guid(“FC480E73-77A9-E111-B151-00155D882D40”));


      Here in the above code create as many activityParty records and add them to fromtoEntities list as to whom you want to send the mail.


  1. How to get datafields value used in email template’s body and subject? Is there any way out there to do it?

    And, thank you for this article.


Please share your thoughts

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

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

My Trial

It is my experience timeline.


Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog & Website of Paul Andrew

Technical Leadership Centred Around the Microsoft Data Platform

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform


Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions


Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM


Mail to for queries and suggestions

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM


Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

%d bloggers like this: