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.