6

I am having a problem logging deletions for pages when a structure group is deleted. The structure group deletion is logged but the pages in the structure group are not logged when the structure group is deleted. I am trying to do this by creating two events; the initiated and the committed but having problems. I am just not sure what to put in the initiated section and how to get the items to be deleted to the committed section Here is my code.

// We are getting all the objects deleted prior to it being deleted


      private void StructureGroupPreDeleteAction(StructureGroup subject, DeleteOrganizationalItemEventArgs args, EventPhases phase)

     {

         StringBuilder deletesg = new StringBuilder();

         foreach (var item in args.ItemsToDelete)
         {

         }

     }


    // Grab the objects the were collected before deletions and send them to the log.

    private void StructureGroupDeleteAction(StructureGroup subject, DeleteOrganizationalItemEventArgs args, EventPhases phase)
    {
        string logDate = DateTime.Now.ToString("T");

        string fileName = String.Format(@"{0}\{1}.{2}.{3}", logFilePath, deleteLogName.ToLower().Replace(" ", "_"), DateTime.Now.ToString("yyyy-MM-dd"), "txt");

        TextWriter tw = new StreamWriter(fileName, true);

        tw.WriteLine(logDate + "itemsToDelete" );

        tw.Close(); 
    }

2 Answers 2

6

You can collect information in the Pre-event and store it in the args.ContextVariables. In the Post-event you can read this information and use it. Something like:

private void StructureGroupPreDeleteAction(StructureGroup subject, DeleteOrganizationalItemEventArgs args, EventPhases phase)

{

     StringBuilder deletesg = new StringBuilder();

     foreach (var item in args.ItemsToDelete)
     {
          deletesg.Append("deleting " + item.Title + ",");
     }
     args.ContextVariables.Add("itemsToDelete-" + subject.Id, deletesg.ToString());


 }


// Grab the objects the were collected before deletions and send them to the log.

private void StructureGroupDeleteAction(StructureGroup subject, DeleteOrganizationalItemEventArgs args, EventPhases phase)
{
    string logDate = DateTime.Now.ToString("T");

    string fileName = String.Format(@"{0}\{1}.{2}.{3}", logFilePath, deleteLogName.ToLower().Replace(" ", "_"), DateTime.Now.ToString("yyyy-MM-dd"), "txt");

    TextWriter tw = new StreamWriter(fileName, true);

    tw.WriteLine(logDate + "itemsToDelete: "  + args.ContextVariables["itemsToDelete-" + subject.Id]);

    tw.Close(); 
}
5
  • Makes sense. I wasn't familiar with ContextVariables but realized later it is Tridion's way to work with a .Net dictionary. I will try this out. Thank you.
    – BryanD
    Commented Mar 26, 2014 at 14:14
  • I am not getting an event error stating that the "The given key was not present in the dictionary." Any ideas?
    – BryanD
    Commented Mar 26, 2014 at 20:16
  • The ID of the structure group folder I am deleting exists in the dictionary of the initiate phase method StructureGroupPreDeleteAction .
    – BryanD
    Commented Mar 26, 2014 at 20:20
  • Strange, Bryan. I have just implemented the same (or very similar) functionality and I have no trouble getting my values back from the ContextVariables in the Post action. Are you sure you are using the same key to put and to pull?
    – Quirijn
    Commented Mar 28, 2014 at 19:46
  • By the way, I corrected the code sample because the original could have caused errors with threading. The key now contains the Id of the subject.
    – Quirijn
    Commented Mar 28, 2014 at 20:25
2

Didn't get a chance to figure out why I could not get Quirijin's solution to work. I did however get it to work(logging deletions for all items in a structure group, not just the structure group) with the following code;

// Subscribe to the post-StructureGroupDelete event
        EventSubscription deleteStructureGroup = EventSystem.SubscribeAsync<StructureGroup, DeleteOrganizationalItemEventArgs>(StructureGroupDeleteAction, EventPhases.TransactionCommitted);

this.Subscriptions.Add(deleteStructureGroup);

private void StructureGroupDeleteAction(StructureGroup subject, DeleteOrganizationalItemEventArgs args, EventPhases phase)
    {
        // Log the deletions of Structure groups. The pages and nested structure groups will be logged as well.
        string logDate = DateTime.Now.ToString("T");

        string fileName = String.Format(@"{0}\{1}.{2}.{3}", logFilePath, deleteLogName.ToLower().Replace(" ", "_"), DateTime.Now.ToString("yyyy-MM-dd"), "txt");

        TextWriter tw = new StreamWriter(fileName, true);

        foreach (var items in args.ItemsToDelete)
        {
            tw.WriteLine(String.Format("{0} Deleted a [{1} ** {2}] {3} From: [{4}] | {5}:{6}[{7}]", logDate, subject.GetType().Name, items, items.Title, subject.WebDavUrl, subject.Session.User.Title, subject.Session.User.Description, subject.Session.User.Id));
        }

        // Fix this here for double logging
        tw.WriteLine(String.Format("{0} Deleted a [{1} {2}] {3} From: [{4}] | {5}:{6}[{7}]", logDate, subject.GetType().Name, subject.Id, subject.Title, subject.WebDavUrl, subject.Session.User.Title, subject.Session.User.Description, subject.Session.User.Id));
        tw.Close();
    }

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.