Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lowedown/637657320a9492939a5f0de721bba948 to your computer and use it in GitHub Desktop.
Save lowedown/637657320a9492939a5f0de721bba948 to your computer and use it in GitHub Desktop.
Will add child items of related items to publishing queue if they are contained in a specified "shared content" location
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<getItemReferences>
<!-- Returns children of linked references if they are contained in a location specified in the SharedContentLocations setting -->
<processor type="MyProject.AddItemLinkReferencesWithSharedContent, MyProject"/>
</getItemReferences>
</pipelines>
<settings>
<!-- Pipe separated list of shared content locations -->
<setting name="MyProject.SharedContentLocations" value="/sitecore/content/test/SharedContent" />
</settings>
</sitecore>
</configuration>
/// <summary>
/// Returns all link references from AddItemLinkReferences plus child items of links if the are contained in a specific shared content location
/// </summary>
public class AddItemLinkReferencesWithSharedContent : AddItemLinkReferences
{
protected override List<Item> GetItemReferences(PublishItemContext context)
{
Assert.ArgumentNotNull((object)context, "context");
List<Item> relatedItems = base.GetItemReferences(context);
List<Item> relatedItemChildren = new List<Item>();
var sharedContentLocations = Settings.GetSetting("MyProject.SharedContentLocations", string.Empty).Split('|');
// Add children of each referenced item in shared content
foreach (var item in relatedItems.Where(i => sharedContentLocations.Any(l => IsContainedInSharedContent(i, l))))
{
if (item.HasChildren)
{
relatedItemChildren.AddRange(item.Children);
}
}
return relatedItemChildren;
}
// Returns true if the specified item is a descendant of the specified path.
private bool IsContainedInSharedContent(Item item, string path)
{
return item.Paths.FullPath.StartsWith(path, StringComparison.InvariantCultureIgnoreCase) && !item.Paths.FullPath.Equals(path, StringComparison.CurrentCultureIgnoreCase);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment