public int Create(string externalKey, string layoutHtml, Priority priority = Priority.Medium) { if (externalKey.Length > Guid.Empty.ToString().Length) { throw new ArgumentException( "externalKey too long, should be max length of " + Guid.Empty.ToString().Length, "externalKey"); } if (_triggeredSendDefinitionClient.DoesTriggeredSendDefinitionExist(externalKey)) { throw new Exception(string.Format("A TriggeredSendDefinition with external key {0} already exsits", externalKey)); } var dataExtensionExternalKey = ExternalKeyGenerator.GenerateExternalKey("data-extension-" + externalKey); if (!_dataExtensionClient.DoesDataExtensionExist(dataExtensionExternalKey)) { var dataExtensionTemplateObjectId = _dataExtensionClient.RetrieveTriggeredSendDataExtensionTemplateObjectId(); var dataExtensionFieldNames = new HashSet <string> { "Subject", "Body", "Head" }; var replacementFields = LayoutHtmlReplacementFieldNameParser.Parse(layoutHtml); foreach (var replacementField in replacementFields) { dataExtensionFieldNames.Add(replacementField); } _dataExtensionClient.CreateDataExtension(dataExtensionTemplateObjectId, dataExtensionExternalKey, "triggeredsend-" + externalKey, dataExtensionFieldNames); } var emailTempalteExternalKey = ExternalKeyGenerator.GenerateExternalKey("email-template" + externalKey); var emailTemplateId = _emailTemplateClient.RetrieveEmailTemplateId(emailTempalteExternalKey); if (emailTemplateId == 0) { layoutHtml += EmailContentHelper.GetOpenTrackingTag() + EmailContentHelper.GetCompanyPhysicalMailingAddressTags(); emailTemplateId = _emailTemplateClient.CreateEmailTemplate(emailTempalteExternalKey, "template-" + externalKey, layoutHtml); } var emailId = _emailRequestClient.CreateEmailFromTemplate(emailTemplateId, "email-" + externalKey, "%%Subject%%", new KeyValuePair <string, string>("dynamicArea", "%%Body%%")); var deliveryProfileExternalKey = ExternalKeyGenerator.GenerateExternalKey("blank-delivery-profile"); _deliveryProfileClient.TryCreateBlankDeliveryProfile(deliveryProfileExternalKey); var triggeredSendDefinition = _triggeredSendDefinitionClient.CreateTriggeredSendDefinition(externalKey, emailId, dataExtensionExternalKey, deliveryProfileExternalKey, externalKey, externalKey, priority.ToString()); return(triggeredSendDefinition); }
public int Create(string externalKey, string layoutHtml) { if (externalKey.Length > Guid.Empty.ToString().Length) { throw new ArgumentException("externalKey too long, should be max length of " + Guid.Empty.ToString().Length, "externalKey"); } if (triggeredSendDefinitionClient.DoesTriggeredSendDefinitionExist(externalKey)) { throw new Exception(string.Format("A TriggeredSendDefinition with external key {0} already exsits", externalKey)); } var dataExtensionExternalKey = ExternalKeyGenerator.GenerateExternalKey("data-extension-" + externalKey); if (!dataExtensionClient.DoesDataExtensionExist(dataExtensionExternalKey)) { var dataExtensionTemplateObjectId = dataExtensionClient.RetrieveTriggeredSendDataExtensionTemplateObjectId(); var regex = new Regex(@"(?<=%%)[^\s].*?[^\s]?(?=%%)"); var matches = regex.Matches(layoutHtml); var dataExtensionFieldNames = new HashSet <string> { "Subject", "Body", "Head" }; for (var i = 0; i < matches.Count; i++) { dataExtensionFieldNames.Add(matches[i].Value); } dataExtensionClient.CreateDataExtension(dataExtensionTemplateObjectId, dataExtensionExternalKey, "triggeredsend-" + externalKey, dataExtensionFieldNames); } var emailTempalteExternalKey = ExternalKeyGenerator.GenerateExternalKey("email-template" + externalKey); var emailTemplateId = emailTemplateClient.RetrieveEmailTemplateId(emailTempalteExternalKey); if (emailTemplateId == 0) { layoutHtml += EmailContentHelper.GetOpenTrackingTag() + EmailContentHelper.GetCompanyPhysicalMailingAddressTags(); emailTemplateId = emailTemplateClient.CreateEmailTemplate(emailTempalteExternalKey, "template-" + externalKey, layoutHtml); } var emailId = emailRequestClient.CreateEmailFromTemplate(emailTemplateId, "email-" + externalKey, "%%Subject%%", new KeyValuePair <string, string>("dynamicArea", "%%Body%%")); var deliveryProfileExternalKey = ExternalKeyGenerator.GenerateExternalKey("blank-delivery-profile"); deliveryProfileClient.TryCreateBlankDeliveryProfile(deliveryProfileExternalKey); var triggeredSendDefinition = triggeredSendDefinitionClient.CreateTriggeredSendDefinition(externalKey, emailId, dataExtensionExternalKey, deliveryProfileExternalKey, externalKey, externalKey, "High"); triggeredSendDefinitionClient.StartTriggeredSend(externalKey); return(triggeredSendDefinition); }
private int Create(string externalKey, string layoutHtml) { var isTemplated = !string.IsNullOrWhiteSpace(layoutHtml); if (externalKey.Length > Guid.Empty.ToString().Length) { throw new ArgumentException("externalKey too long, should be max length of " + Guid.Empty.ToString().Length, "externalKey"); } if (_triggeredSendDefinitionClient.DoesTriggeredSendDefinitionExist(externalKey)) { throw new Exception(string.Format("A TriggeredSendDefinition with external key {0} already exsits", externalKey)); } var dataExtensionExternalKey = ExternalKeyGenerator.GenerateExternalKey("data-extension-" + externalKey); if (!_dataExtensionClient.DoesDataExtensionExist(dataExtensionExternalKey)) { var dataExtensionTemplateObjectId = _dataExtensionClient.RetrieveTriggeredSendDataExtensionTemplateObjectId(); var dataExtensionFieldNames = new HashSet <string> { "Subject", "Body" }; if (!isTemplated) { dataExtensionFieldNames.Add("Head"); } _dataExtensionClient.CreateDataExtension(dataExtensionTemplateObjectId, dataExtensionExternalKey, "triggeredsend-" + externalKey, dataExtensionFieldNames); } int emailId; var emailName = "email-" + externalKey; var emailExternalKey = ExternalKeyGenerator.GenerateExternalKey("email-" + externalKey); if (isTemplated) { var emailTempalteExternalKey = ExternalKeyGenerator.GenerateExternalKey("email-template" + externalKey); var emailTemplateId = _emailTemplateClient.RetrieveEmailTemplateId(emailTempalteExternalKey); if (emailTemplateId == 0) { emailTemplateId = _emailTemplateClient.CreateEmailTemplate(emailTempalteExternalKey, "template-" + externalKey, layoutHtml); } emailId = _emailRequestClient.CreateEmailFromTemplate(emailTemplateId, emailName, "%%Subject%%", new KeyValuePair <string, string>("dynamicArea", "%%Body%%")); } else { emailId = _emailRequestClient.CreateEmail(emailExternalKey, emailName, "%%Subject%%", "<html>" + "<head>%%Head%%</head>" + "%%Body%%" + EmailContentHelper.GetOpenTrackingTag() + EmailContentHelper.GetCompanyPhysicalMailingAddressTags() + "</html>"); } var deliveryProfileExternalKey = ExternalKeyGenerator.GenerateExternalKey("blank-delivery-profile"); _deliveryProfileClient.TryCreateBlankDeliveryProfile(deliveryProfileExternalKey); return(_triggeredSendDefinitionClient.CreateTriggeredSendDefinition(externalKey, emailId, dataExtensionExternalKey, deliveryProfileExternalKey, externalKey, externalKey)); }