We have a setup where component is dynamic and its also used on Page. But by-design in Tridion if you un-publish a Page which is using a dynamic component, it does not un-publish the component.
We have to fetch the component based on the metadata at various places and attached on page where component was also required on page for following requirements.
- Author driven Page name
- Author driven page Url
- Author driven page metadata.
so following event code was written to handle the scenario. in our scenario
[TcmExtension("UnpublishEventHandler")]
public class UnpublishEventHandler : TcmExtension
{
public UnpublishEventHandler()
{
EventSystem.Subscribe<Page, UnPublishEventArgs>(UnpublishComponent, EventPhases.TransactionCommitted);
}
public void UnpublishComponent(Page page, UnPublishEventArgs mUnPublishEventArgs, EventPhases mEventPhases)
{
var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string cts = appConfig.AppSettings.Settings["UnpublishPageComponentTemplates"].Value;
IList<ComponentPresentation> cpList = page.ComponentPresentations;
List<IdentifiableObject> items = new List<IdentifiableObject>();
if (cpList != null)
{
foreach (ComponentPresentation cp in cpList)
{
if (cts.ToLowerInvariant().Contains(cp.ComponentTemplate.Title.ToLowerInvariant()))
items.Add(cp.Component);
}
IEnumerable<IdentifiableObject> itemsenum = items;
List<TargetType> targets = new List<TargetType> { };
foreach (var pubTarget in mUnPublishEventArgs.Targets)
{
targets.Add((TargetType)pubTarget);
}
var transaction = PublishEngine.UnPublish(itemsenum, mUnPublishEventArgs.UnPublishInstruction, targets);
}
}
}