How to provide email notifications/workflows for clones

六眼飞鱼酱① 提交于 2019-12-06 02:21:32

Answering my own question The code here was provided by various posters on SDN in this thread: http://sdn.sitecore.net/forum//ShowPost.aspx?PostID=34012

If anyone that contributed to that thread wants to post up an answer then I'll happily give credit - and rep - where it's due.

Firstly: John West points out that there are a couple of interesting, though private methods:

private static IEnumerable<Item> GetClonesOfVersion(Item source)
{
    Assert.ArgumentNotNull(source, "source");
    return (from clone in GetAllClones(source)
        where clone.SourceUri == source.Uri
        select clone);
}

private static IEnumerable<Item> GetAllClones(Item source)
{
    Assert.ArgumentNotNull(source, "source");
    return (from link in Globals.LinkDatabase.GetReferrers(source)
        select link.GetSourceItem() into clone
        where ((clone != null) && (clone.Source != null)) && (clone.Source.ID == source.ID)
        select clone);
}

There's a support ticket asking for these to be made public, otherwise just copy them into your project.

That needs to be coupled with a custom workflow action, which should be compiled and added to a workflow for a source item.

This one below, provided by Derek Roberti/Lauren Hightower is to force accept of notifications in clones. To provide email notifications we'd need to reverse the logic - instead of performing an action if a clone has notifications we'd want to ensure an action was performed if the clone had no notifications - i.e. was directly inheriting the edited value from the source item.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Clones;
using Sitecore.Diagnostics;
using Sitecore.SecurityModel;
using Sitecore;
using Sitecore.Links;

namespace WorkFlowCustom
{
public class ForceCloneAccept
{
public void Process(Sitecore.Workflows.Simple.WorkflowPipelineArgs args)
{
Item workFlowItem = args.DataItem;
List itemList = GetItemClones(workFlowItem, true);

foreach (Item cloneItem in itemList)
{
List list = new List(workFlowItem.Database.NotificationProvider.GetNotifications(cloneItem));
foreach (Notification n in list)
{
if ((n != null) && (workFlowItem != null))
{
n.Accept(cloneItem);
}
}
}
}

protected virtual List GetItemClones(Item item, bool processChildren)
{
Assert.ArgumentNotNull(item, "item");
List list = new List();

using (new SecurityDisabler())
{
foreach (ItemLink link in Globals.LinkDatabase.GetReferrers(item))

{
if (!(link.SourceFieldID != FieldIDs.Source))
{
Item sourceItem = link.GetSourceItem();
if (sourceItem != null)
{
list.Add(sourceItem);
}
}
}
}
if (processChildren)
{
foreach (Item item4 in item.Children)
{
list.AddRange(this.GetItemClones(item4, true));
}

}
return list;
}

}
} 

And here's some general reading on custom workflows and invoking actions: http://sdn.sitecore.net/FAQ/API/Cause%20the%20workflow%20to%20invoke%20an%20action.aspx

Thanks to all that provided input!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!