I am working on Event System code and am trying to retrieve the PublicationTarget(s)
for the current item being published.
The PublishEventArgs has a property called Targets which you would think is a list of PublicationTarget(s)
but is a list of TargetTypes
.
I need the PublicationTarget(s)
for the current item so I can test and make sure the item isn’t already scheduled for that given target and the date as well.
Any assistance would be greatly appreciated.
I currently have the following block of code:
private void ProcessComponent(Component component, String processEvent, PublishEventArgs pubArgs, UnPublishEventArgs unPubArgs)
{
if (!IsEventSystemActive())
{
return;
}
if (IsBundle(component))
{
Session session = component.Session;
Publication publication = component.ContextRepository as Publication;
//Component configComponent = session.GetObject(publication.WebDavUrl + "/Building%20Blocks/Content/System/System%20Configuration.xml") as Component;
ItemFields cmpFields = new ItemFields(component.Content, component.Schema);
ComponentLinkField cmpLinkField = cmpFields["Items"] as ComponentLinkField;
DateField pubDate = cmpFields["PublishDate"] as DateField;
DateTime date = new DateTime();
date = pubDate.Value;
foreach (Component linkedComp in cmpLinkField.Values)
{
switch (processEvent)
{
case "Publish":
foreach (PublicationTarget pTarg in pubArgs.Targets)
{
if (!IsInPublishingQueue(session, linkedComp.Id, 0, 0, pubArgs.PublishInstruction.StartAt, pTarg))
{
Publish_UnPublish(linkedComp, unPubArgs, pubArgs, pTarg);
}
}
break;
case "UnPublish":
foreach (PublicationTarget pTarg in unPubArgs.Targets)
{
if (!IsInPublishingQueue(session, linkedComp.Id, 0, 1, unPubArgs.UnPublishInstruction.StartAt, pTarg))
{
Publish_UnPublish(linkedComp, unPubArgs, pubArgs, pTarg);
}
}
break;
}
}
}
else
{
Component bundleComponent = GetBundleComponent(component);
if (bundleComponent != null)
{
ActivityInstance activity = GetActivityInstance(bundleComponent);
if (activity != null)
{
string message = string.Format("Component is part of Bundle \"{0}\", which is in Workflow \"{1}\". Publishing the item individually not allowed.",
bundleComponent.Title, activity.ActivityDefinition.Title);
throw new Exception(message);
}
}
}
}
This code is giving me a cast error due to the fact that the PublishEventArgs.Targets contains TargetType(s)
and not PublicationTarget(s)
.