Microsoft Dynamics 365 CRM Tips and Tricks
By Inogic
Skip to content
  • Blogs
  • D365 Apps
  • Inogic
  • D365 / Power Platform Services
  • Maplytics Docs
  • New D365 Apps Docs
    • Subscription Recurring Billing Management
    • Marketing4Dynamics
    • Gamifics365
    • 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
    • Auto Tax Calculator

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 Apps Portal
  • Power Automate
  • Power BI
  • AI Builder
  • Dynamics 365 for Field Service
  • Dynamics 365 for Project Service Automation
  • Dynamics 365 for Mobiles and Tablets

Recent Updates

New Release: Business Process Checklist app for streamlining business processes in Microsoft Dynamics 365 CRM

Document Management Solution for your Dynamics 365 CRM – How to choose the better one?

Integrate Dynamics 365 Customer Engagement with SharePoint – The secure way!

Get valuable insights into CRM data with new-age smart Data Visualization apps – Kanban Board and Map My Relationships!

Visualize intricate details in Dynamics 365 CRM with finesse using latest Map My Relationships feature

Top 5 Reasons why Subscription and Recurring Billing Management is must for easy subscription management within Dynamics 365 CRM!

QuickBooks Online + Dynamics 365 CRM – Get Accounting & Sales Data on one platform with InoLink!

From beginners to experts – Shorten the journey of adapting to Dynamics 365 CRM with our advanced User Adoption Monitor app!

New Age Automation App to Clone/Copy Records in Dynamics 365 CRM with just 1 Click

1 Click is all you need to Undo, Restore / Retrieve Deleted Data within Dynamics 365 CRM!

Bid goodbye to export woes with Dynamics 365 CRM 1 Click productivity app – Click2Export

Alerts4Dynamics – New age Dynamics 365 CRM alert management solution to bridge the information gap!

Automatically set user availability while assigning leads in Dynamics 365 CRM – New Feature of our Lead Assignment and Distribution Automation!

Mailchimp integration with Dynamics 365 CRM – Sync Audience, Tags, Campaigns and more!

Gamification in Microsoft Dynamics 365 CRM – Set goals, Make them fun, and Everyone wins!

Recent Posts

  • How to incorporate fluent UI framework in Canvas Apps
  • Exploring Bookmarks in the OOB Power BI service reports
  • Mind Map Visualization of complex family structures, entities, trust beneficiaries, et al. within Microsoft Dynamics 365 CRM – Try Now!
  • Maplytics Jan23 Release Webinar – Enhancements to Auto Scheduling, Mobile UI, Route Tracking, and more!
  • How to use optional parameters of Dataverse Search in Power Automate Flow?

Archives

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