Skip to content

Instantly share code, notes, and snippets.

@bkoopman
Last active December 17, 2015 19:39
Show Gist options
  • Save bkoopman/5662079 to your computer and use it in GitHub Desktop.
Save bkoopman/5662079 to your computer and use it in GitHub Desktop.
/// <summary>
/// Resolver to strip out items from parent publication
/// </summary>
public class OnlyChildPublicationsResolver : IResolver
{
/// <summary>
/// For publish and unpublish, remove all items from the parent publication from the list.
/// </summary>
/// <param name="item">Item to be resolved (e.g. a page, structure group, template)</param>
/// <param name="instruction">Resolve instruction</param>
/// <param name="context">Publish context</param>
/// <param name="resolvedItems">List of items that are currently to be rendered and published (added by previous resolvers in the chain)</param>
public void Resolve(IdentifiableObject item, ResolveInstruction instruction, PublishContext context, Tridion.Collections.ISet resolvedItems)
{
List itemsToRemove = new List();
StringBuilder infoMessage = new StringBuilder();
infoMessage.AppendLine(string.Format("Removed the following items from a {0} transaction to {1}:", instruction.Purpose, context.PublicationTarget.Title));
// parent publication id (should come from a configuration)
TcmUri parentPublicationUri = new TcmUri("tcm:0-2-1");
// check for items from parent publication (these do not need to be published or unpublished)
foreach (ResolvedItem resolvedItem in resolvedItems)
{
// mark all items from parent publication for removal
if (resolvedItem.Item.Id.PublicationId == parentPublicationUri.ItemId)
{
itemsToRemove.Add(resolvedItem);
}
}
// remove all items that we need to discard
foreach (ResolvedItem itemToRemove in itemsToRemove)
{
infoMessage.AppendLine(string.Format("{0}: {1} ({2})", itemToRemove.Item.Id.ItemType, itemToRemove.Item.Title, itemToRemove.Item.Id));
resolvedItems.Remove(itemToRemove);
}
// log info mesage about which items have been removed
if (itemsToRemove.Count > 0)
{
Logger.Write(infoMessage.ToString(), "OnlyChildPublicationsResolver", LoggingCategory.General, TraceEventType.Information);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment