The code you are showing in your question is certainly not going to save your Component correctly, it has all sorts of issues:
if (component.IsShared)
{
component.Localize();
}
So the first thing you try to do is localize the Component in case it is shared. This might be a correct and understandable action, but you will have to ask yourself, is this really what you want? Because in this case you will not be editing the parent Component, but one of its localized children.
if (component.IsCheckedOut)
{
component.CheckIn(true);
}
Okay next step, you try to check out the Component (with a permanent lock) in case it was already checked out. If the current user had this Component checked out, you didn't need to do this. If another user had checked out this Component, this line will probably fail, unless the current user is an Administrator. All in all, this is most likely not what you need in your event system.
component.CheckOut(true);
bool isCheckOut = component.TryCheckOut();
Here you first checkout the Component with a permanent lock (why?), and then you try to see if it is checked out? If you are worried the Component could be checked out, then use the TryCheckOut()
method, in this situation, I think it will fail, since the Component has just been checked out by you (with a permanent lock).
if (isCheckOut) // This always comes as false
{
component.Save(true);
}
If my previous statement is correct, then indeed your isCheckOut
parameter will be false, and you will never try to save your Component. But there is something more wrong with this code, considering the previous. You are executing a Save(true)
but you never made any changes to the Component, so Save will not fire. Also since you checked it out with a permanent lock, you will need to check it in through code, calling Save(true)
is not enough in this case.
If all you are trying to accomplish is just make a simple save to a Component, use the following:
Component comp = new Component(new TcmUri("tcm:1-2"), subject.Session);
try
{
comp.CheckOut();
comp.Title = "My Edit Worked";
comp.Save(true);
}
catch (Exception ex)
{
Logger.Write(ex, "My Event Handler", LoggingCategory.General, TraceEventType.Error);
}
This will work, or log an error in the Tridion eventlog, which should give you a reasonable explanation as to what went wrong. If you are expecting the item to be shared, handle localization if you want it localized, or make sure you get the TCMURI of the parent and use that instead. If your Component is one that can be checked out by other processes, then handle it like this:
// I'm worried my Component might already be checked out
//comp.CheckOut();
bool isCheckedOut = comp.TryCheckOut();
if (isCheckedOut)
{
comp.Title = "My Edit Worked";
comp.Save(true);
}
else
{
// I have a problem, the Component is being edited by somebody else,
// so I should not simply ignore this but probably log a warning
string msg = string.Format("{0} is already checked out", comp.Id);
Logger.Write(msg, "My Event Handler", LoggingCategory.General, TraceEventType.Warning);
}