Skip to main content
replaced http://tridion.stackexchange.com/ with https://tridion.stackexchange.com/
Source Link

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension {
  public EventHandler() {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases) {
    // the TCMURI of the Publication from this item
    TcmUri contextRepository = subject.ContextRepository.Id;

    // the TCMURI of the Publication where this item was localized
    TcmUri owningRepository = subject.OwningRepository.Id;

    if (subject.IsLocalized) {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) {
          Direction = BluePrintChainDirection.Up
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = new TcmUri(subject.GetBluePrintChain(filter).Last().Id.ContextRepositoryId, ItemType.Publication);
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ItemId, subject.Id.ItemType, owningRepository.ItemId);
    
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?How to get the owning publication of a component via the Core Service?

update

@Josh was right, it needs to be direction Up and Last(), so updated my code accordingly.

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension {
  public EventHandler() {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases) {
    // the TCMURI of the Publication from this item
    TcmUri contextRepository = subject.ContextRepository.Id;

    // the TCMURI of the Publication where this item was localized
    TcmUri owningRepository = subject.OwningRepository.Id;

    if (subject.IsLocalized) {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) {
          Direction = BluePrintChainDirection.Up
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = new TcmUri(subject.GetBluePrintChain(filter).Last().Id.ContextRepositoryId, ItemType.Publication);
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ItemId, subject.Id.ItemType, owningRepository.ItemId);
    
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

update

@Josh was right, it needs to be direction Up and Last(), so updated my code accordingly.

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension {
  public EventHandler() {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases) {
    // the TCMURI of the Publication from this item
    TcmUri contextRepository = subject.ContextRepository.Id;

    // the TCMURI of the Publication where this item was localized
    TcmUri owningRepository = subject.OwningRepository.Id;

    if (subject.IsLocalized) {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) {
          Direction = BluePrintChainDirection.Up
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = new TcmUri(subject.GetBluePrintChain(filter).Last().Id.ContextRepositoryId, ItemType.Publication);
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ItemId, subject.Id.ItemType, owningRepository.ItemId);
    
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

update

@Josh was right, it needs to be direction Up and Last(), so updated my code accordingly.

updated code
Source Link
Bart Koopman
  • 29.6k
  • 3
  • 40
  • 90

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension {
  public EventHandler() {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases) {
    // the TCMURI of the Publication from this item
    TcmUri contextRepository = subject.ContextRepository.Id;

    // the TCMURI of the Publication where this item was localized
    TcmUri owningRepository = subject.OwningRepository.Id;

    if (subject.IsLocalized) {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) {
          Direction = BluePrintChainDirection.DownUp
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = new TcmUri(subject.GetBluePrintChain(filter).FirstOrDefaultLast().Id;Id.ContextRepositoryId, ItemType.Publication);
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ItemId, subject.Id.ItemType, owningRepository.ItemId);
    
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

update

@Josh was right, it needs to be direction Up and Last(), so updated my code accordingly.

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension {
  public EventHandler() {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases) {
    // the TCMURI of the Publication from this item
    TcmUri contextRepository = subject.ContextRepository.Id;

    // the TCMURI of the Publication where this item was localized
    TcmUri owningRepository = subject.OwningRepository.Id;

    if (subject.IsLocalized) {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) {
          Direction = BluePrintChainDirection.Down
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = subject.GetBluePrintChain(filter).FirstOrDefault().Id;
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ItemId, subject.Id.ItemType, owningRepository.ItemId);
    
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension {
  public EventHandler() {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases) {
    // the TCMURI of the Publication from this item
    TcmUri contextRepository = subject.ContextRepository.Id;

    // the TCMURI of the Publication where this item was localized
    TcmUri owningRepository = subject.OwningRepository.Id;

    if (subject.IsLocalized) {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) {
          Direction = BluePrintChainDirection.Up
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = new TcmUri(subject.GetBluePrintChain(filter).Last().Id.ContextRepositoryId, ItemType.Publication);
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ItemId, subject.Id.ItemType, owningRepository.ItemId);
    
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

update

@Josh was right, it needs to be direction Up and Last(), so updated my code accordingly.

added sample on how to actually get the parent Component
Source Link
Bart Koopman
  • 29.6k
  • 3
  • 40
  • 90

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension
  {
  public EventHandler()
  {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases)
  {
    // the TCMURI of the Publication from this item
    stringTcmUri contextRepository = subject.ContextRepository.Id.ToString();Id;
    
    // the TCMURI of the Publication where this item was localized
    stringTcmUri owningRepository = subject.OwningRepository.Id.ToString();Id;

    if (subject.IsLocalized)
    {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) 
        { 
          Direction = BluePrintChainDirection.Down 
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = subject.GetBluePrintChain(filter).FirstOrDefault().Id;
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ToString(ItemId, subject.Id.ItemType, owningRepository.ItemId);
    }
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension
 {
  public EventHandler()
  {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases)
  {
    // the TCMURI of the Publication from this item
    string contextRepository = subject.ContextRepository.Id.ToString();
    
    // the TCMURI of the Publication where this item was localized
    string owningRepository = subject.OwningRepository.Id.ToString();

    if (subject.IsLocalized)
    {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) 
        { 
          Direction = BluePrintChainDirection.Down 
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = subject.GetBluePrintChain(filter).FirstOrDefault().Id.ToString();
    }
  }
}

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

It would definitely help if you just read the API documentation, but here is some sample code that should give you an idea:

[TcmExtension("ExampleEventHandlerExtension")]
public class EventHandler : TcmExtension {
  public EventHandler() {
    EventSystem.Subscribe<Component, SaveEventArgs>(ComponentSaveAction, EventPhases.Processed);
  }

  public static void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phases) {
    // the TCMURI of the Publication from this item
    TcmUri contextRepository = subject.ContextRepository.Id;

    // the TCMURI of the Publication where this item was localized
    TcmUri owningRepository = subject.OwningRepository.Id;

    if (subject.IsLocalized) {
      BluePrintChainFilter filter = new BluePrintChainFilter(subject.Session) {
          Direction = BluePrintChainDirection.Down
        };

      // the TCMURI of the Publication where this item was created
      owningRepository = subject.GetBluePrintChain(filter).FirstOrDefault().Id;
    }

    // build TcmUri of parent Component
    // (note using ItemId of owningRepositiory as a PublicationId, 
    //  since that is a Publication TcmUri)
    TcmUri uri = new TcmUri(subject.Id.ItemId, subject.Id.ItemType, owningRepository.ItemId);
    
    // load parent Component
    Component parent = new Component(uri, subject.Session);
  }
}

Note I didn't test any of this, it is just an example to give you an idea of what you can use, please read the API documentation for every method shown here to see if it indeed is what you require.

If you need the same info via the Core Service see my answer to How to get the owning publication of a component via the Core Service?

completed
Source Link
Bart Koopman
  • 29.6k
  • 3
  • 40
  • 90
Loading
Source Link
Bart Koopman
  • 29.6k
  • 3
  • 40
  • 90
Loading