Advertisements
Home > CRM 2011, Plug-Ins > Sending Email using Email Template in CRM 2011

Sending Email using Email Template in CRM 2011

Hi,

Here is the sample code to send an Email using Email template name.

Before start below are few keywords

  • Template Name   (i.e., Name of the Template)
  • Regarding Id        (i.e., GUID of the entity record which template associated with)
  • Regarding Type   (i.e., Logical name of the entity which template associated with)
  • ActivityParty[]     (i.e., Sender & Receivers Can be Users/Contacts/Accounts/Leads)
  • IOrganizationService crmService

 public void SendEmailUsingTemplate(IOrganizationService crmService,

ActivityParty[] fromParty,  ActivityParty[] toParty,

string templateName,

Guid regardingId, string regardingType)

{

try

{

// Create e-mail message.

var email = new Email

{

To = toParty,

From = fromParty,

DirectionCode = true

};

if (!string.IsNullOrEmpty(templateName))

{

Guid templateId = Guid.Empty;

// Get Template Id by Name

Entity template = GetTemplateByName(crmService, templateName);

if (template != null && template.Id != null)

{

var emailUsingTemplateReq = new SendEmailFromTemplateRequest

{

Target = email.ToEntity<Entity>(),

TemplateId = template.Id,

RegardingId = regardingId,

RegardingType = regardingType

};

var emailUsingTemplateResp = (SendEmailFromTemplateResponse)crmService.Execute(emailUsingTemplateReq);

}

else

{

// “****No email template exists with the given name ****”);

}

}

}

catch (Exception ex)

{

throw;

}

}

     private Entity GetTemplateByName(string title, IOrganizationService crmService)

{

var query = new QueryExpression();

query.EntityName = “template”;

var filter = new FilterExpression();

var condition1 = new ConditionExpression(“title”, ConditionOperator.Equal, new object[] { title });

filter.AddCondition(condition1);

query.Criteria = filter;

EntityCollection allTemplates = crmService.RetrieveMultiple(query);

Entity emailTemplate = null;

if (allTemplates.Entities.Count > 0)            {

emailTemplate = allTemplates.Entities[0];

}

return emailTemplate;

}

How Do I call this method

  • Prepare From and To Users/Contacts/Accounts
  • Pass Service,Template Name,Regarding details

// Prepare “From” activity parties

var from = new ActivityParty

{

PartyId = new EntityReference(SystemUser.EntityLogicalName, {GUID of User})

};

var fromParty = new[] { from };

// Prepare “To” activity parties

var to = new ActivityParty

{

PartyId = new EntityReference(SystemUser.EntityLogicalName, {GUID of User})

};

var toParty = new[] { to };

var orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);

IOrganizationService orgnaizationService = orgProxy;

Guid regardingntityId={GUID of record} // Ex – Guid of contact

string regardingEntityName = “contact” // Logical name ‘contact’

SendEmailUsingTemplate(orgnaizationService , fromParty, toParty, “templateName”, regardingntityId, regardingEntityName);

Hope it helps 🙂

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: