2

When extending the Tridion event system, using the following code:

EventSystem.Subscribe<Component, UnPublishEventArgs>(OnComponentUnPublishForInit, EventPhases.Initiated);
private static void OnComponentUnPublishForInit(Component component, UnPublishEventArgs unpublishEvent, EventPhases phase){...}

in the OnComponentUnPublishForInit method, how can I get information about if the Component which will be unpublished, is being used by others? And if it is used by others, how can I make this unpublish event fail, and give some information to the Tridion system?

An example, there is Component A, and component B. A contains B (A has a reference to B), A is published, B is published. When trying to unpublish B, A is already published and it has a reference to B, so B should not be unpublished. Can I get the information that A has a reference to B?

1

In UnPublishEventArgs, there's a property Items. it contains all of the items that are to be unpublished. You may think of some logic to decide who triggers what, but easier way will be to get ResolveInstruction from UnPublishEventArgs.UnPublishInstruction and resolve your component again. This will return you all of the items that will be touched if you unpublish component. From there on it's up to you to decide if it's a valid unpublish action or not. But keep in mind that it might be tricky to decide what item actually triggered unpublish.

Here are a couple of examples:

  • Component is in a static component presentation on a page. You unpublish page -> event is triggered with page in it

  • Bunch of components selected, and unpublished. Event is triggered for each of the components with Items property containing all of the components selected. Resolving components will return you nothing or some items dependant on this component. You can then compare it against Items to see if related items are unpublished as well.

So playing carefully with data from these 2 approaches you can get to something, but you should think it through carefully.

| improve this answer | |
  • How can I resolve the component? – Jacky Pei May 9 '14 at 9:17
  • If I'm not mistaken it's Tridion.ContentManager.Publishing.Resolving.ComponentResolver.Resolve – Andrey Marchuk May 9 '14 at 9:26
  • In the Tridion.ContentManager.Publishing.Resolving.ComponentResolver.Resolve Method there is a parameter 'PublishContext context', what is it and how can I get the right PublishContext to pas to it? – Jacky Pei May 12 '14 at 0:34
  • From API docs: PublishContext objects should not be created manually - instead, one of the ResolvePublishContext methods should be used in ResolveEngine. Those methods resolve publishing contexts necessary for particular resolving operation and return them as a result. If PublishContext is created manually and used in resolving, the collection of resolved items can be incorrect or incomplete. – Andrey Marchuk May 12 '14 at 7:18
1

Getting an event to fail should be as simple as throwing an exception.

Unfortunately for your case, Tridion doesn't have a concept of a component being "used by others" in this sense. You can do a where-used on it, but that won't tell you whether or not the component is in use by your web application. (Although you can tell whether it is published to other targets, but I think that's not what you mean.)

The usual way that component publishing is used is that the web application is designed to work with the component presentations that are currently published. If there are several or none that match the relevant criteria, it should be designed to cope, and respond in a correct way.

| improve this answer | |
  • 1
    An example, there is a component A, a component B, A contains B(A has a reference to B), A is published, B is published, now, Unpublish B, but A is published and it has reference to B, so B should not be unpublished, can I get the information that A has a reference to B? – Jacky Pei May 9 '14 at 6:53
  • And really thanks for helping me analyze the problem – Jacky Pei May 9 '14 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.