Skip to content
Microsoft Dynamics 365 CRM Tips and Tricks
By Inogic
  • Blogs
  • D365 Apps
  • Inogic
  • D365 / Power Platform Services
  • Maplytics Docs
  • New D365 Apps Docs
    • WhatsApp4Dynamics
    • Marketing4Dynamics
    • Business Process Checklist
  • D365 Apps Docs
    • Alerts4Dynamics
    • Attach2Dynamics
    • SharePoint Security Sync
    • Kanban Board
    • Lead Assignment and Distribution Automation
    • Map My Relationships
    • User Adoption Monitor
    • InoLink
    • Click2Clone
    • Click2Export
    • Undo2Restore
    • Subscription Recurring Billing Management
    • Gamifics365

Retrieve and Instantiate Global Email Template

By Inogic | June 26, 2015
0 Comment

Have you ever Retrieved or Instantiated an email template? If the answer to this question is YES, then you might be knowing the required parameters we need to have in place in order to do so.

For those of who aren`t aware of it then this blog would help you understand the tidbits involved.

We had a request in which we were supposed to use a Global Email Template for sending emails.

Well we had done the same for entity specific email template, but this was something we had never done until the request.

Let`s get started with the explanation of how we achieved it.

First, we`ll retrieve a Global Email template.

Retrieve:

Here we`ll retrieve the Global Email Template, on the basis of the Template Name. We just need the GUID for this demo, so we would just be retrieving the GUID of the template. The retrieved GUID would then be used to Instantiate the template.

Code Snippet

public void RetrieveTemplate()

{

string functionName = “RetrieveTemplate”;

Guid templateId = Guid.Empty;

try

{

string emailTemplate = “Welcome Email”;

FilterExpression filter = new FilterExpression();

ConditionExpression conditionTitle = new ConditionExpression();

ConditionExpression conditionType = new ConditionExpression();

QueryExpression query = new QueryExpression();

//title

conditionTitle.AttributeName = “title”;

conditionTitle.Operator = ConditionOperator.Equal;

conditionTitle.Values.Add(emailTemplate);

//entity type

conditionType.AttributeName = “templatetypecode”;

conditionType.Operator = ConditionOperator.Equal;

conditionType.Values.Add(“systemuser”);

//add conditions

filter.FilterOperator = LogicalOperator.And;

filter.Conditions.Add(conditionTitle);

filter.Conditions.Add(conditionType);

query.Criteria = filter;

query.ColumnSet = new ColumnSet();

query.EntityName = “template”;

query.ColumnSet.Columns.Add(“templateid”);

EntityCollection templateCollection = _service.RetrieveMultiple(query);

if (templateCollection.Entities.Count > 0)

{

templateId = templateCollection[0].Id;

InstantiateTemplate(templateId);

}

}

catch (FaultException<OrganizationServiceFault> ex)

{

throw new FaultException(functionName + “:” + ex.Message);

}

catch (Exception ex)

{

throw new Exception(functionName + “:” + ex.Message);

}

}

Explanation

  • Entire code is similar to what it should have been for retrieving Entity Specific Email template; the difference here is the part where we specify “templatetypecode”.
  • If the template is an entity specific template, then we would have used the respective entity`s logical name, but in this case we are retrieving a Global Email Template, then what could be “templatetypecode“? Here is a trick; we need to use “systemuser” as the “templatetypecode“. And this simple it is to retrieve Global Email Template.
  • Using the above code, we`ll get the GUID of the global template and that GUID will then be used in instantiating the Global Email Template.

Instantiate:

Here on the basis of the retrieved GUID, we`ll instantiate the Global Email Template, and that would be the last step of this procedure.

Code Snippet

private void InstantiateTemplate(Guid templateId)

{

string functionName = “InstantiateTemplate”;

EntityCollection templateCollection = new EntityCollection();

Entity template = new Entity();

try

{

InstantiateTemplateRequest request = new InstantiateTemplateRequest();

request.RequestName = “InstantiateTemplate”;

//Set Email template Id

request[“TemplateId”] = templateId;

//Set Regarding

request[“ObjectId”] = userId;

//Set regarding type

request[“ObjectType”] = “systemuser”;

//execute message

InstantiateTemplateResponse response = (InstantiateTemplateResponse)_service.Execute(request);

//Store response in collection

templateCollection = (EntityCollection)response[“EntityCollection”];

if (templateCollection.Entities.Count > 0)

{

template = templateCollection[0];

}

}

catch (FaultException<OrganizationServiceFault> ex)

{

throw new FaultException(functionName + “:” + ex.Message);

}

catch (Exception ex)

{

throw new Exception(functionName + “:” + ex.Message);

}

}

Explanation

  • In the above snippet there are 3 key things to look at.
    • Template Id :  It is the GUID of the template that we retrieved in starting.
    • Object Id : It is the GUID of the user from the organization. If this would have been an Entity Specific template then we would have used the GUID of any of the record from that entity.
    • Object Type : It is the systemuser in this case, since we are instantiating a Global Email Template. If it would have been an Entity Specific template then we would have used that respective entity`s logical name.
  • The noticing part was the above 3 points, rest is just executing the InstantiateTemplateRequest and getting the response in InstantiateTemplateResponse which is further used to get the Entity from it.

Hope this helps!

Its just not this, before moving further, check out our new utility tool User Adoption Tracker. Monitor what your teams are actually doing in Dynamics CRM. Email us on crm@inogic.com for a trial or if you would like to see a live demo.

Category: Development Dynamics CRM Dynamics CRM 2013 Dynamics CRM 2015 Dynamics CRM 2016 Tags: Dynamics CRM, email template, Gobal Email Template
Post navigation
← Handling Date/Time fields in Microsoft Dynamics CRM 2015 Online Update 1 Dynamics CRM 2015 Online Update 1 – API support for Transaction →

Follow Us!

Follow Us on FacebookFollow Us on TwitterFollow Us on LinkedInFollow Us on YouTubeFollow Us on InstagramFollow Us on E-mailFollow Us on Docs

Blog Series

  • Power Apps – Model Driven Apps
  • Power Apps – Canvas App
  • Power Pages
  • Power Automate
  • Power BI
  • AI Builder
  • Copilot
  • Microsoft Teams
  • Dynamics 365 for Field Service

Recent Innovations

WhatsApp4Dynamics – WhatsApp Integration with Microsoft Dynamics 365 CRM

DeDupeD – Data Hygiene App for Dynamics 365 CRM

Recent Posts

  • Manage Dynamics CRM records along with their associated activities on the same page using Focused view – Part 2
  • Copilot and Power Platform: Highlights from Microsoft Ignite 2023 and What They Mean for You!
  • Improved Work order experience in Dynamics 365 Field Service
  • Prevent and Remove Duplicates in Dynamics 365 CRM: 5 Benefits of Data Hygiene App DeDupeD
  • How to convert Power Page Portal into a Progressive Web App (PWA) & App Package

Archives

SiteLock
Copyright © 2023 Inogic. All Rights Reserved
Iconic One Theme | Powered by Wordpress