I want to be able to notify a user/ user group when an Asset is submitted for Approval.
Is there a way to do this without needing to create a script?
Thank you.
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityI don't think there is an OOTB way to send emails to notify users about approval.
The only way is by creating the script.
The following script goes through the following high-level steps:
using Stylelabs.M.Base.Querying;
using Stylelabs.M.Sdk.Contracts.Base;
using Stylelabs.M.Sdk.Contracts.Notifications;
using Stylelabs.M.Sdk.Models.Notifications;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
const string emailTemplatename = "Asset Acceptance Notification";
SendEmailAsync();
public async Task SendEmailAsync()
{
var rejectionTemplate = await MClient.Notifications.GetMailTemplateAsync(emailTemplatename);
if (rejectionTemplate == null)
{
await CreateEmailTemaplateAsync();
rejectionTemplate = await MClient.Notifications.GetMailTemplateAsync(emailTemplatename);
}
var groupId = MClient.Users.GetUserGroupAsync("Content Managers").Result.Id;
var query = Query.CreateQuery(entities =>
from e in entities
where e.DefinitionName == "User"
select e);
var users = MClient.Querying.QueryAsync(query, new EntityLoadConfiguration { RelationLoadOption = RelationLoadOption.All}).Result;
var groupUsers = new List<IEntity>();
foreach(var user in users.Items)
{
var groupRelation = user.GetRelation<IChildToManyParentsRelation>("UserGroupToUser");
if (groupRelation.Parents.Contains(group.Id ?? 0)) // nullable ID, provide fallback
{
groupUsers.Add(user);
}
}
var userNames = MClient.Users.GetUsernamesAsync(groupUsers.Select(i => i.Id ?? 0).ToList()).Result?.Select(i => i.Value).ToList();
var notificationRequest = new MailRequestByUsername
{
MailTemplateName = emailTemplatename,
Recipients = userNames
};
var entityModified = Context.Target as IEntity;
notificationRequest.Variables.Add("AssetUrl", "https://somedomain.stylelabsdemo.com/en-us/asset/"+entityModified.Id);
await MClient.Notifications.SendEmailNotificationAsync(notificationRequest);
}
public async Task CreateEmailTemaplateAsync()
{
var enUs = CultureInfo.GetCultureInfo("en-US");
var template = await MClient.EntityFactory.CreateAsync(Stylelabs.M.Sdk.Constants.MailTemplate.DefinitionName) as IMailTemplate;
template.Name = emailTemplatename;
template.SetSubject(enUs, "Asset Approved");
template.SetBody(enUs, "Hello, the following asset got approved: {{AssetUrl}}");
template.SetDescription(enUs, emailTemplatename);
template.SetTemplateVariables(new[] {
new TemplateVariable
{
Name = "AssetUrl",
VariableType = TemplateVariableType.String
}
});
await MClient.Entities.SaveAsync(template);
}
You can view below link for more detail - https://www.cmsbestpractices.com/how-to-send-emails-in-sitecore-content-hub/