10

I'm trying to move a component using Tridion 2011 Event system.

First I tried to do this using SaveEventArgs in phase processed:

EventSystem.Subscribe<Component, SaveEventArgs>(CompSavePost, EventPhases.Processed); 

But then I got this horrible error:

TransactionScope nested incorrectly

Then I read this article on how to move a component using CheckInEventArgs using phase TransactionCommitted. My code now works fine, no errors, but the component is not moved.

Since my setup is key here, this is the basic version:

...
EventSystem.Subscribe<Component, CheckInEventArgs>(CheckInPost, 
                                                   EventPhases.TransactionCommitted); 
...

private void CheckInPost(Component component, CheckInEventArgs args, EventPhases phase)
{
    AccessoriesHelper helper = new AccessoriesHelper();
    helper.MoveAccessory(component);
}

And then, in my AccessoriesHelper class

public void MoveAccessory(Component component)
{
    Session session = component.Session;
    ItemFields metadata = new ItemFields(component.Metadata, component.Schema);
    KeywordField categoryField = (KeywordField)metadata["Category"];
    Keyword categoryKeyword = categoryField.Value;

    List<Keyword> hierarchy = Utilities.GetKeywordHierarchy(categoryKeyword);

    Folder baseFolder = (Folder)session.GetObject(Utilities.GetSettingFromConfig(
                                                      "AccessoriesCopyBaseFolderUri"));
    Folder saveFolder = GetFolderBasedOnKeywords(baseFolder, hierarchy);

    if (!saveFolder.Id.Equals(component.OrganizationalItem.Id))
    {
        logging.Debug(String.Format(
                      "Accessory needs to move from {0} ({1}) to  {2} ({3})", 
                      component.OrganizationalItem.Title, 
                      component.OrganizationalItem.Id, 
                      saveFolder.Title, saveFolder.Id));
        component.Move(saveFolder);
    }
}

The thing is, when I move the component.Move(folder) part to the top of my MoveAccessory method, all works fine. But in the setup as shown above, the component is not moved but no errors too... The 'Accessory needs to move...' part is logged and seems correct:

Accessory needs to move from 040 Accessories Content Master English (tcm:113-19424-2) to Bags, Cases & Pads (tcm:113-19631-2)

so the Move method should be called...

2
  • Have you tried debugging to see if you perhaps get an unexpected error on the component.Move() call? Commented Mar 22, 2013 at 17:09
  • We've never managed to get remote debugging working correctly over here, so unfortunately, no. Commented Mar 25, 2013 at 11:57

2 Answers 2

7

If your component.Move works at the top of your MoveAccessory method, does it have a correct value for the saveFolder variable?

If not perhaps the actual value here is incorrect, perhaps the ID is wrong or the context publication of the folder is wrong.

1
  • Good point, I'm pretty sure the saveFolder object is correct. This is what is logged: "Accessory needs to move from 040 Accessories Content Master English (tcm:113-19424-2) to Bags, Cases & Pads (tcm:113-19631-2)", which is valid and as expected. The tcm:113-19631-2 uri is also the one I used when I put the .Move at the top. Commented Mar 22, 2013 at 14:18
6

In the end, I was able to fix this issue by passing the Session object to my custom 'GetFolderBasedOnKeywords' method. In that method, I created a new Session object using an impersonation user, but using the same Session object as the component uses seems to have fixed it.

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.