When you subscribe to the following event handler:
EventSystem.Subscribe<Page, UnPublishEventArgs>(OnPageUnPublish, EventPhases.Processed);
You will have access to the Page that is going to be unpublished. But please note, it is a trigger on the action in the UI of unpublishing the Page, it is not unpublished (not even when you choose the Event Phase TransactionCommitted
, since it still has to go through the Content Delivery side to get unpublished). So you might want to choose instead to subscribe to the changing of the publish state, since then you know it is successfully unpublished.
EventSystem.Subscribe<Page, SetPublishStateEventArgs>(OnPageStateChange, EventPhases.TransactionCommitted);
Then in your OnPageStateChange
method you can check whether it was a publish or unpublish action:
private void OnPageStateChange(Page subject, SetPublishStateEventArgs args, EventPhases phase)
{
if (!args.IsPublished)
{
// Page was just unpublished on args.Target
}
}
update
Your added questions are a bit like: "please do my homework for me", it is a lot more beneficial if you think about a solution and ask for help where you get stuck, but I'll do my best to give you a nudge in the right direction.
Yes you can get to any item in the CM from inside an event handler. Look at my event handler example code and the subject
parameter is the Page (similar thing you would have in a Component event handler), so you can simply use the following:
Publication pub = (Publication)subject.ContextRepository;
StructureGroup root = pub.RootStructureGroup;
// get list of Pages in Structure Group
OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(subject.Session)
{
ItemTypes = new[] { ItemType.Page },
Recursive = false
};
IEnumerable<Page> items = root.GetItems(filter).Cast<Page>();
foreach (Page page in items)
{
// do whatever you need to do with these Pages
}
Again yes you can, but you will need to add a configuration to your event handler to store the Schema, or get it in another way. To find a Component based on a certain Schema is not that difficult.
// get the Schema TCM URI from somewhere (hardcoded in this example)
TcmUri tcmUri = new TcmUri("tcm:1-2-8");
Schema schema = new Schema(tcmUri, subject.Session)
// get list of Components based on given Schema
OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(subject.Session)
{
ItemTypes = new[] { ItemType.Component },
BasedOnSchemas = new[] { schema },
Recursive = false
};
Publication pub = (Publication)subject.ContextRepository;
IEnumerable<Component> items = pub.RootFolder.GetItems(filter).Cast<Component>();
foreach (Component comp in items)
{
// do whatever you need to do with these Components
// keep in mind, you might only have one Component based on this Schema,
// but there can always be multiple
// Maybe instead of getting a Component based on a Schema,
// you can store a link to the Component in the Publication Metadata
// and get it right away from there
}