2

In the PagePublishPost method , i am accessing a component and trying to save it, but that component does not get saved. Below is my code:

            if (component.IsShared)
              {
                    EventHelper.WriteEventToHTMLLog("S-Going to Component Localize");
                    component.Localize();
                    EventHelper.WriteEventToHTMLLog("E-Going to Component Localize");
              }

              if (component.IsCheckedOut)
              {
                    EventHelper.WriteEventToHTMLLog("S-CheckIn");
                    component.CheckIn(true);
                    EventHelper.WriteEventToHTMLLog("E-CheckIn");
               }
                EventHelper.WriteEventToHTMLLog("S-CheckOut");
                component.CheckOut(true);
                EventHelper.WriteEventToHTMLLog("E-CheckOut");
bool isCheckOut = component.TryCheckOut();
              if (isCheckOut) // This always comes as false
              {
                    component.Save(true);
             }

What can be the possible reasons and how to fix this. I need to save the component and publish the page which has that component.

10
  • Why don't you try this in PagePublishPre method instead of PagePublishPost Commented Mar 18, 2014 at 8:57
  • I dont think it matters if you do Initiated (pre) or TransactionCommitted (post) as the subject of the event is page, and the item being saved is a component
    – Will Price
    Commented Mar 18, 2014 at 9:00
  • This doesn't appear to be a full code example (what is isCheckOut, where are you actually updating the component) so its hard to suggest where the issue is
    – Will Price
    Commented Mar 18, 2014 at 9:02
  • @Will, isCheckOut is a boolean variable and the code is ' bool isCheckOut = component.TryCheckOut(); EventHelper.WriteEventToHTMLLog("isCheckOut forcefully-" + isCheckOut.ToString());' Commented Mar 18, 2014 at 9:16
  • @Pankaj- I did not thought of the pagepublishpre method, in my old event system code which was 2009, it was post method and the event code ran when the page was published, so i decided to use pagepublishpostmethod, my aim is to geneate a xml file at a path, which will be generated by the page i publish, that page has the component attached to it, and the field called xmlbody in the component has the xml stuff. Commented Mar 18, 2014 at 9:18

2 Answers 2

1

To simplify your code, I guess you should use TryCheckOut as in the below code sample. also if you want the component saved forcefully(in case its checked out by some other user), try component.UndoCheckOut and save it.

 if (component.IsShared)
                {
                    EventHelper.WriteEventToHTMLLog("S-Going to Component Localize");
                    component.Localize();
                    EventHelper.WriteEventToHTMLLog("E-Going to Component Localize");
                }

                bool IsCheckedout = component.TryCheckOut();
                if (IsCheckedout)
                {

                    component.Save(true);

                }
                else
                { 
                    // code for: if the component is already checked out by some other user

                }
1

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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