In Dynamics CRM we use Email activity a lot to send all kind
of notification and approval. CRM also
provide Email Template that we can use to send customize email.
But some time we need to extend this capability.
Sample Case: – We
need to send email to customer based on case origin type with below conditions.
- We need to use embedded image. We
cannot use global image hosted on public domain.
- We need to use web resource images
of CRM.
- Need to use Email template.
- Image need to change based on case
origin value.
Solution: – After
lot of research and try different solution. We conclude that we need to perform
below task to achieve our requirement.
- Need base64 string with html image
tag to insert in email.
- Need to create an email to set
sender and receiver value.
- We need email template
information not to send just to get body and subject value.
- Need to create our custom
placeholder and replace these placeholder as per our requirement.
To achieve this we need some custom workflow activity for:
- Convert web resource to base64 image string
- Get email template subject and description at run time and
set these values in our email
- To replace our placeholders
- Send Email
Steps: –
- Create PNG Web resource to store
images.
2. Create an action to get web resource
to base64 image string based on condition. Use Getbase64Image custom workflow.
3. Create placeholder in your Email
template. I have use [url] as placeholder.
4. Create Workflow to complete our
requirement and send email
·
Call previously create action to get embedded image.
·
In next step create an email set to and from
value.
·
In next step call update email from email template custom workflow. This workflow
will use Instantiate Template Request because do not need to send this email
template. We need it’s Subject and description value.
InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
{
TemplateId =
EmailTemplate.Get(executionContext).Id,
ObjectId =
Case.Get(executionContext).Id,
ObjectType =
Case.Get(executionContext).LogicalName
};
InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);
Entity
emailT = (Entity)instTemplateResp.EntityCollection.Entities[0];
Entity email =
service.Retrieve(EmailSource.Get(executionContext).LogicalName,
EmailSource.Get(executionContext).Id, new ColumnSet(“description”, “subject”));
email.Attributes[“description”] = emailT.Attributes.Contains(“description”) ? (String)emailT.Attributes[“description”] : “”;
email.Attributes[“subject”] = emailT.Attributes.Contains(“subject”) ? (String)emailT.Attributes[“subject”] : “”;
service.Update(email);
5. Call Replace
Placeholder workflow to update placeholder with embedded image.
6. Finally send this email.
I have not considered email Template with attachment. For Email
Template with attachment you need to extend “update email from email template”
custom workflow.
Result: –
I am sharing custom workflow assembly. Hope this will help
other also.