Not able to Checkout Component or Page after adding Event Handler
-
12-12-2019 - |
Question
I am adding event handler for CheckOutEventArgs, and am trying to get the checkout user details. Below is my code.
public void Subscribe()
{
EventSystem.Subscribe<Page, CheckOutEventArgs>(PageCheckOutWarning,
EventPhases.Initiated);
EventSystem.Subscribe<Component, CheckOutEventArgs>(ComponentCheckOutWarning,
EventPhases.Initiated);
}
private void ComponentCheckOutWarning(Component component,
CheckOutEventArgs args, EventPhases phase)
{
logdetails("Checkout User-->" + component.CheckOutUser.Title.ToString());
}
When I try to checkout the Component/Page I get this error in the Tridion Explorer error message box.
Object Reference not set to an instance of an object
Solution
You have subscribed to Initiated
phase, in this phse item is not checked out yet, hence no CheckOutUser
. You should subscribe to some of the later phases.
Also, I don't know what your code is doing, but you might consider subscribing to some generic class, like VersionedItem
that contains both component and page.
Again, not knowing your idea, but take a look at SubscribeAsync
method if you want to have just warning. This way it will execute faster.
If you still want to know the user in the initiated phase - you can get it from session:
component.Session.User.Title
You can subscribe to VersionedItem and then in the event handler do:
private void CheckOutWarning(VersionedItem item, CheckOutEventArgs args, EventPhases phase)
{
if (item.GetType().Name == "Component" || item.GetType().Name == "Page")
{
}
}
Do you want to have just warning, or do you want to prevent CheckOut?