2

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.

1 Answer 1

2

I 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:

  1. Check if a given named email template exists, and create a simple empty one, if it doesn’t
  2. Retrieve the Content Managers Group
  3. Retrieve all usernames from the group
  4. Send notification email using the email template and retrieved usernames
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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.