Sitecore Content Hub : Sending emails to a UserGroup - Part 2 : Creating Actions and Scripts
In Sitecore Content Hub, Actions are components that perform a specific task. These actions can either be manually triggered by a user via the User Interface, or automatically run by a trigger or via the APIs.
Integrating custom scripts into your business logic within Sitecore Content Hub can be a powerful way to extend functionality and automate processes.
For our use case to send email to a UserGroup, we are going to create a Script in Content Hub. The primary purpose of the script is to :
1.Create the Email Template in Content Hub
2.Fetch the members of the UserGroup who will be notified about the asset submitted for review.
3.Lastly, send the email notification to the members
To create a script:
- Go the the Manage page, type 'scripts' in the search bar, and select Scripts:
- Click + Script to add the Script.
- Build the script for checking any errors and then Publish the script.
Below is the Script for our use case. Please feel free to update depending on your requirements.
using Stylelabs.M.Base.Querying;
using Stylelabs.M.Sdk.Contracts.Base;
using Stylelabs.M.Sdk.Contracts.Notifications;
using Stylelabs.M.Sdk.Models.Notifications;
using Stylelabs.M.Sdk.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
const string emailTemplatename = "Asset Activity Notification - submitted for review and approval";
await SendEmailAsync();
public async Task SendEmailAsync()
{
var emailTemplate = await MClient.Notifications.GetMailTemplateEntityAsync(emailTemplatename);
if(emailTemplate == null){
await CreateEmailTemaplateAsync();
emailTemplate = await MClient.Notifications.GetMailTemplateEntityAsync(emailTemplatename);
}
var userGrp = MClient.Users.GetUserGroupAsync("Activity Email Group").Result.Id;
var query = Query.CreateQuery(entities =>
from e in entities
where e.DefinitionName == "User"
select e);
var iterator = MClient.Querying.CreateEntityIterator(query, new EntityLoadConfiguration { RelationLoadOption = RelationLoadOption.All});
var groupUsers = new List();
var parents = "";
while (await iterator.MoveNextAsync().ConfigureAwait(false))
{
foreach (var users in iterator.Current.Items)
{
var user = await MClient.Entities.GetAsync((long)users.Id);
var groupRelation = user.GetRelation("UserGroupToUser");
foreach(var parent in groupRelation.Parents){
parents = parents + " , " + parent;
if (parent == userGrp)
{
groupUsers.Add(user);
}
}
}
}
var userNames = MClient.Users.GetUsernamesAsync(groupUsers.Select(i => i.Id ?? 0).ToList()).Result?.Select(i => i.Value).ToList();
var emailRecipients = new List();
var entityModified = Context.Target as IEntity;
foreach(var username in userNames){
if(!emailRecipients.Contains(username)){
emailRecipients.Add(username);
MClient.Logger.Info($"Username added {username} entity {entityModified.Id}");
}
else{
MClient.Logger.Info($"Username already added {username}");
}
}
var notificationRequest = new MailRequestByUsername
{
MailTemplateName = emailTemplatename,
Recipients = emailRecipients
};
notificationRequest.Variables.Add("AssetUrl", "https://ghd-p-001.sitecorecontenthub.cloud/en-us/asset/"+entityModified.Id);
await MClient.Notifications.SendEmailNotificationAsync(notificationRequest);
}
public async Task CreateEmailTemaplateAsync()
{
var enUs = CultureInfo.GetCultureInfo("en-US");
var entity = await MClient.EntityFactory.CreateAsync(Constants.MailTemplate.DefinitionName);
var template = MClient.TypedEntityFactory.FromEntity(entity);
template.Name = emailTemplatename;
template.Subject[enUs] = "Asset Activity Notification - Submitted for review and approval";
template.Description[enUs] = emailTemplatename;
template.Body[enUs] = "Hello {{Username}}, The following asset has been submitted for review and approval: {{AssetUrl}}";
template.SetPropertyValue("M.Mailing.TemplateLabel", enUs, emailTemplatename);
template.SetTemplateVariables(new []
{
new TemplateVariable
{
Name = "AssetUrl",
VariableType = TemplateVariableType.String
},
new TemplateVariable
{
Name = "Username",
VariableType = TemplateVariableType.String
}
});
await MClient.Entities.SaveAsync(template);
}
The next step is to create an Action and then link the Action to the script.
- On the menu bar, click Manage cog icon.
- On the Manage page, click Actions.
- On the Actions page, click New Action.
- In the New action dialog, select an Action type and provide other details as needed.
- Click Save.