Tuesday, 1 May 2012

Create and attach attachment to Email Template in CRM 2011


In this article , I am going to explain how to Create and attach attachment to Email Template


For Better Understanding I divided this article in two parts
(i)      Create Email Template
(ii)     Attach attachments to email Template


Namespace need to include 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using System.Collections.Generic;

Create Email Template
Template emailTemplate = new Template
{
    Title = "An example email template",
    Subject = "This is an example email.",
    IsPersonal = false,
    TemplateTypeCode = "lead",

    //1033 is the code for US English - you may need to change this value
    //depending on your locale.
    LanguageCode = 1033

};
Guid _emailTemplateId = _serviceProxy.Create(emailTemplate);

Attach attachments to email Template
ActivityMimeAttachment attachment = new ActivityMimeAttachment
{
    Subject = String.Format("Attachment {0}", i),
    FileName = String.Format("ExampleAttachment{0}.txt", i),
    Body = "Some Text",
    ObjectId = new EntityReference(Template.EntityLogicalName, _emailTemplateId),
    ObjectTypeCode = Template.EntityLogicalName
};

_templateAttachmentIds.Add(_serviceProxy.Create(attachment));

Complete Source Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
                                                serverConfig.HomeRealmUri,
                                                serverConfig.Credentials,
                                                serverConfig.DeviceCredentials))
{
    // This statement is required to enable early-bound type support.
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

    // Create Email Template.
    Template emailTemplate = new Template
    {
        Title = "An example email template",
        Subject = "This is an example email.",
        IsPersonal = false,
        TemplateTypeCode = "lead",

        //1033 is the code for US English - you may need to change this value
        //depending on your locale.
        LanguageCode = 1033

    };
    Guid _emailTemplateId = _serviceProxy.Create(emailTemplate);

    //Attach attachments to email Template
    List<Guid> _templateAttachmentIds = new List<Guid>();
    for (int i = 0; i < 3; i++)
    {
        ActivityMimeAttachment attachment = new ActivityMimeAttachment
        {
            Subject = String.Format("Attachment {0}", i),
            FileName = String.Format("ExampleAttachment{0}.txt", i),
            Body = "Some Text",
            ObjectId = new EntityReference(Template.EntityLogicalName, _emailTemplateId),
            ObjectTypeCode = Template.EntityLogicalName
        };

        _templateAttachmentIds.Add(_serviceProxy.Create(attachment));
    }

}



1 comment:

Note: only a member of this blog may post a comment.