private Entity CreateTemplates(string attachmentName)
        {
            //Template 1
            string title1    = rowEmailSubject1 + DateTime.Now;
            string body1     = rowEmailBody1 + DateTime.Now;
            Entity template1 = new Entity("template");

            template1["title"]   = title1;
            template1["subject"] = TemplatesHelper.GetSubjectXML(title1);
            template1["subjectpresentationxml"] = TemplatesHelper.GetSubjectPresentationXML(title1);
            template1["body"]             = TemplatesHelper.GetBodyXML(body1);
            template1["presentationxml"]  = TemplatesHelper.GetBodyPresentationXML(body1);
            template1["templatetypecode"] = 8;//Global template
            template1["ispersonal"]       = false;
            template1["languagecode"]     = 1033;
            template1.Id = OrganizationService.Create(template1);

            //Template 2
            string title2    = rowEmailSubject2 + DateTime.Now;
            string body2     = rowEmailBody2 + DateTime.Now;
            Entity template2 = new Entity("template");

            template2["title"]   = title2;
            template2["subject"] = TemplatesHelper.GetSubjectXML(title2);
            template2["subjectpresentationxml"] = TemplatesHelper.GetSubjectPresentationXML(title2);
            template2["body"]             = TemplatesHelper.GetBodyXML(body2);
            template2["presentationxml"]  = TemplatesHelper.GetBodyPresentationXML(body2);
            template2["templatetypecode"] = 8;//Global template
            template2["ispersonal"]       = false;
            template2["languagecode"]     = 1033;
            template2.Id = OrganizationService.Create(template2);

            //Add attachment to template 1
            Entity activityMimeAttachment1 = new Entity("activitymimeattachment");

            activityMimeAttachment1["objectid"]       = template1.ToEntityReference();
            activityMimeAttachment1["objecttypecode"] = "template";
            activityMimeAttachment1["filename"]       = attachmentName;
            activityMimeAttachment1["body"]           = Convert.ToBase64String(new UnicodeEncoding().GetBytes("Sample Annotation Text 1"));
            activityMimeAttachment1["mimetype"]       = "text/plain";
            OrganizationService.Create(activityMimeAttachment1);

            //Add attachment to template 2
            Entity activityMimeAttachment2 = new Entity("activitymimeattachment");

            activityMimeAttachment2["objectid"]       = template2.ToEntityReference();
            activityMimeAttachment2["objecttypecode"] = "template";
            activityMimeAttachment2["filename"]       = attachmentName;
            activityMimeAttachment2["body"]           = Convert.ToBase64String(new UnicodeEncoding().GetBytes("Sample Annotation Text 2"));
            activityMimeAttachment2["mimetype"]       = "text/plain";
            OrganizationService.Create(activityMimeAttachment2);

            return(template1);
        }
        private Entity CreateTemplate()
        {
            Entity template = new Entity("template");

            template["title"]            = "DXTools - Integration Test - SendCustomEmailTest_WithTypeFormatters - " + DateTime.Now;
            template["templatetypecode"] = 8;//Global template
            template["ispersonal"]       = false;
            template["languagecode"]     = 1033;

            rowEmailSubject     = "Integration Test " + DateTime.Now;
            rowEmailBody        = "<p><strong> This is an integration test {CurrentVAT} </strong></p>";
            template["subject"] = TemplatesHelper.GetSubjectXML(rowEmailSubject);
            template["subjectpresentationxml"] = TemplatesHelper.GetSubjectPresentationXML(rowEmailSubject);
            template["body"]            = TemplatesHelper.GetBodyXML(rowEmailBody);
            template["presentationxml"] = TemplatesHelper.GetBodyPresentationXML(rowEmailBody);

            template.Id = OrganizationService.Create(template);

            return(template);
        }
        private Entity BuildTemplate(string templateTitle, string subject, string body)
        {
            Entity newTemplate = new Entity("template");

            newTemplate["title"]            = templateTitle;
            newTemplate["templatetypecode"] = 8;//Global template
            newTemplate["ispersonal"]       = false;
            newTemplate["languagecode"]     = 1033;

            //IMPORTANT NOTE:
            //Both fields are mandatory to set 'subject' properly
            newTemplate["subject"] = TemplatesHelper.GetSubjectXML(subject);;
            newTemplate["subjectpresentationxml"] = TemplatesHelper.GetSubjectPresentationXML(subject);

            //IMPORTANT NOTE:
            //Both fields are mandatory to set 'body' properly
            newTemplate["body"]            = TemplatesHelper.GetBodyXML(body);;
            newTemplate["presentationxml"] = TemplatesHelper.GetBodyPresentationXML(body);

            newTemplate.Id = this.CrmOrganisationService.Create(newTemplate);

            return(newTemplate);
        }