Creating Email Template In Content Hub Programmatically
Hello everyone, In this blog we will see, how we can create mail template programatically in Sitecore Content Hub.
So let's quickly try to see the code and we will understand it afterwwards.
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Stylelabs.M.Sdk;
using Stylelabs.M.Sdk.Models.Notifications;
using Stylelabs.M.Sdk.Contracts.Notifications;
using Stylelabs.M.Sdk.Contracts.Base;
using Stylelabs.M.Sdk.Contracts.Content;
using Stylelabs.M.Base.Querying;
using Stylelabs.M.Base.Querying.Filters;
CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
MClient.Logger.Info($"SS - Creating Email Template");
IMailTemplate template = await MClient.EntityFactory.CreateAsync(Constants.MailTemplate.DefinitionName) as IMailTemplate;
template.Name = "MailNotificationReject";
template.SetSubject(culture, "SS Mail Notification Reject");
template.SetBody(culture, "Email Body - <a href=\"{{assetLink}}\">Asset Link</a>");
var assetLink = new TemplateVariable
{
Name = "assetLink",
VariableType = TemplateVariableType.String
};
template.SetTemplateVariables(new[] { assetLink});
long entityId = await MClient.Entities.SaveAsync(template);
MClient.Logger.Info($"SS - Email Template created {entityId.ToString()}");
var entity = await MClient.Entities.GetAsync(entityId);
if (entity != null)
{
var label = await entity.GetPropertyAsync<ICultureSensitiveProperty>("M.Mailing.TemplateLabel");
label.SetValue(culture, "SS Mail Notification Reject");
var description = await
entity.GetPropertyAsync<ICultureSensitiveProperty>("M.Mailing.TemplateDescription");
description.SetValue(culture, "SS Mail Notification Reject");
await MClient.Entities.SaveAsync(entity);
}
First we will create a mail template using MClient.EntityFactory.CreateAsync() method whick takes Definition MailTemplate name.Then we are setting subject and body.Now for dynamic variable, we will be creating a instance of TemplateVariable and will pass the required parameters.Once it is done, we need to pass the template variable defined to SetTemplateVariables() method. This method takes array of templatevariable as parameter.Once it is completed, save the entity using SaveAsync() method.Once it is done, you can check the same in Email template section in Manage Page of Content Hub. Refer Below image for your reference.
Thanks for reading and hope this helps. Happy Learning!!!
You can check my other blogs too if interested. Blog Website
Comments
Post a Comment