Daily Archives: January 20, 2014

Sending Email with entity attachment from Notes

If you are looking for sending email with an entity record attachment from Notes then this article is going to help you to implement this requirement. We will explain here how to send email using a workflow with an attachment from Notes regarding an entity. Let’s consider a business scenario where we have implemented a customer approval process and as soon as the customer approves we need to send him notification with an attachment in notes, this attachment contains the details about his account.

We can’t access an attachment from Notes in an OOB workflow so we need to write a custom workflow where we need to get an entity record attachment from an annotation entity and need to create an activitymimeattachment record and need to associate it with the email created. While creating the workflow we need to use a create step instead of sending the email so that we can refer to the email record id.

So mainly we need to use the following procedure to implement our requirements:

  • Develop a custom workflow assembly to get an attachment.
  • Create a workflow and use a custom workflow assembly step to send the email.

Let’s create a custom workflow. If you are new to custom workflow assemblies then please refer to our previous article to create a custom workflow. Create a class library project and add the required assemblies to your project. Let’s rename our class file to SendEmailWithAttachement and use the following code:

namespace EmailAttachment


public class SendEmailWithAttachement : CodeActivity


//define output variable



public InArgument<EntityReference> SourceEmail { get; set; }

protected override void Execute(CodeActivityContext executionContext)


// Get workflow context

IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

//Create service factory

IOrganizationServiceFactory serviceFactory =          executionContext.GetExtension<IOrganizationServiceFactory>();

// Create Organization service

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// Get the target entity from the context

Entity  AccountID= (Entity)service.Retrieve(“account”, context.PrimaryEntityId, new ColumnSet(new string[] { “accountid” }));

AddAttachmentToEmailRecord(service, AccountID.Id, SourceEmail.Get<EntityReference>(executionContext));


private void AddAttachmentToEmailRecord(IOrganizationService service, Guid SourceAccountID, EntityReference SourceEmailID)


//create email object

Entity _ResultEntity = service.Retrieve(“email”, SourceEmailID.Id, new ColumnSet(true));

QueryExpression _QueryNotes = new QueryExpression(“annotation”);

_QueryNotes.ColumnSet = new ColumnSet(new string[] { “subject”, “mimetype”, “filename”, “documentbody” });

_QueryNotes.Criteria = new FilterExpression();

_QueryNotes.Criteria.FilterOperator = LogicalOperator.And;

_QueryNotes.Criteria.AddCondition(new ConditionExpression(“objectid”, ConditionOperator.Equal, SourceAccountID));

EntityCollection _MimeCollection = service.RetrieveMultiple(_QueryNotes);

if (_MimeCollection.Entities.Count > 0)

{  //we need to fetch first attachment

Entity _NotesAttachment = _MimeCollection.Entities.First();

//Create email attachment

Entity _EmailAttachment = new Entity(“activitymimeattachment”);

if (_NotesAttachment.Contains(“subject”))

_EmailAttachment[“subject”] = _NotesAttachment.GetAttributeValue<string>(“subject”);

_EmailAttachment[“objectid”] = new EntityReference(“email”, _ResultEntity.Id);

_EmailAttachment[“objecttypecode”] = “email”;

if (_NotesAttachment.Contains(“filename”))

_EmailAttachment[“filename”] = _NotesAttachment.GetAttributeValue<string>(“filename”);

if (_NotesAttachment.Contains(“documentbody”))

_EmailAttachment[“body”] = _NotesAttachment.GetAttributeValue<string>(“documentbody”);

if (_NotesAttachment.Contains(“mimetype”))

_EmailAttachment[“mimetype”] = _NotesAttachment.GetAttributeValue<string>(“mimetype”);



// Sending email

SendEmailRequest SendEmail = new SendEmailRequest();

SendEmail.EmailId = _ResultEntity.Id;

SendEmail.TrackingToken = “”;

SendEmail.IssueSend = true;

SendEmailResponse res = (SendEmailResponse)service.Execute(SendEmail);




Build and register your assembly using plug-in registration tool. Now we need to create our workflow to use our custom assembly step.

Navigate to Settings->Process to create new process. Select Account in entity drop down and workflow in category.

. Now follow below steps to configure workflow

  •  Add condition to check account approval.
  • Add a step to create email record that we can use in custom step
  • Add our custom workflow steps from Add Step.


Click on Set Properties button to set input variable like below

inputvariableAfter completion it should look like below


Save and Activate workflow.



Filed under MS CRM 2011