SitecoreワークフローのEメール通知ロールベースのカスタマイズ

投稿者:

Sitecoreには、すぐに使用できるワークフローの電子メール通知機能がありますが、ワークフロー実装の主要な部分の1つは、現在の状態とカスタムトークンを使用して適切なSitecoreロールに電子メールを送信することです。

ロールには、可用性に応じてコンテンツを承認できる複数の人がいる可能性があるため、1人ではなくsitecoreロールに電子メール通知を送信しています。

私たちが知っているように、承認者は、アイテム名、アイテムパス、送信者、ワークフローURLなどのワークフローにあるアイテムの詳細を知ることが重要です。 カスタムトークンの助けを借りて取得しているこれらすべての情報。

そこで、Sitecoreロールの新しいフィールドを追加しました。 外観は次のとおりです。-

workflow

アイテムがコンテンツ編集者によってチェックインされたら。 StoryReviewerの下のすべてのユーザーに電子メールがトリガーされます。

また、誰がアイテムを送信したかをレビュー担当者に知らせるためのカスタムトークンも作成しました。

これが同じコードです:-

public class WorkflowEmail
    {
        public void Process(WorkflowPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            ProcessorItem processorItem = args.ProcessorItem;
            if (processorItem != null)
            {
                Item item = processorItem.InnerItem;
                string fullPath = item.Paths.FullPath;
                string from = GetValue(item, "from", args);
                string to = !string.IsNullOrEmpty(getToEmailList(item)) ? getToEmailList(item) : GetValue(item, "to", args);
                string host = GetValue(item, "mail server", args);
                string subject = GetValue(item, "subject", args);
                string body = GetValue(item, "message", args);
                var message = new MailMessage();
                message.From = new MailAddress(from);
                message.To.Add(to);
                message.Subject = subject;
                message.Body = body;              
                message.IsBodyHtml = true;
                new SmtpClient(host).Send(message);
            }
        }

        private string GetValue(Item commandItem, string field, WorkflowPipelineArgs args)
        {
            string text = commandItem[field];
            if (text.Length <= 0) return String.Empty;

            return ReplaceString(text, args);
        }
        private string ReplaceString(string text, WorkflowPipelineArgs args)
        {
            Item workflowItem = args.DataItem;
            text = text.Replace("$itemName$", workflowItem.Name.ToString());
            text = text.Replace("$itemSubmittedBy$", GetItemSubmitter(args));
            text = text.Replace("$itemComments$", args.CommentFields["Comments"]);
            text = text.Replace("$itemPath$", workflowItem.Paths.FullPath);
            text = text.Replace("$itemLanguage$", workflowItem.Language.ToString());
            text = text.Replace("$itemVersion$", workflowItem.Version.ToString());
            text = text.Replace("$workboxUrl$", HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + "/sitecore/shell/Applications/Workbox.aspx?sc_bw=1");

            bool itemHasLayout = !String.IsNullOrEmpty(workflowItem.Fields[FieldIDs.LayoutField].Value);

            if (itemHasLayout)
                text = text.Replace("$itemUrl$", HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + (Sitecore.Links.LinkManager.GetItemUrl(workflowItem)));
            else
                text = text.Replace("$itemPreviewUrl$", "This item is not a page and cannot be previewed.");

            return text ;
        }
        private string GetItemSubmitter(WorkflowPipelineArgs args)
        {
            string result = String.Empty;

            Item contentItem = args.DataItem;
            IWorkflow contentWorkflow = contentItem.Database.WorkflowProvider.GetWorkflow(contentItem);
            WorkflowEvent[] contentHistory = contentWorkflow.GetHistory(contentItem);

            if (contentHistory.Length > 0)
            {
                string lastUser = contentHistory[contentHistory.Length - 1].User;
                User user = User.FromName(lastUser, false);
                UserProfile userProfile = user.Profile;

                result = userProfile.FullName;
            }

            return result;
        }

        public string getToEmailList(Item innerItem)
        {
            string role = innerItem[Templates.WorkflowRoles.Fields.Roles];
            if (!string.IsNullOrEmpty(role))
            {
                var users = System.Web.Security.Roles.GetUsersInRole(role);
                StringBuilder userlist = new StringBuilder();
                foreach (var user in users)
                {
                    var membershipUser = System.Web.Security.Membership.GetUser(user);
                    userlist.Append(membershipUser.Email);
                    userlist.Append(",");
                }
                return userlist.ToString().Remove(userlist.Length - 1);
            }
            return string.Empty;
        }
    }

これにより、すべてのトークンが置き換えられ、特定の役割のすべてのユーザーに電子メールが送信されます。

One comment

  1. How can I implement the code in a project in order to get email notification?

コメントを残す