Automatically accepting changes for cloned items


Description

When Cloning functionality is used widely throughout the Sitecore solution, it may be necessary to automatically accept changes on the cloned items.

For example, this functionality may be helpful when items are in a workflow. Specifically, in some setups, having to accept changes on cloned item each time a new item version of the source item is created may be considered as excessive actions. You may allow cloned items to be updated automatically instead.

This article provides guidance on how to implement the behavior of automatically accepting changes on the cloned items.

Solution

To automatically accept changes on the cloned items, perform the following steps:

  1. Create your own notification provider that inherits the SqlServerNotificationProvider class.
  2. Override the AddNotification method so that it automatically accepts notifications when required.
  3. public override void AddNotification(Notification notification)
    {
      if (this.ShouldAutomaticallyAcceptChanges(notification))
      {
        try
        {
         notification.Accept(ItemManager.GetItem(notification.Uri.ItemID, notification.Uri.Language, notification.Uri.Version, Database.GetDatabase(notification.Uri.DatabaseName)));
         }
         catch (NullReferenceException ex)
         { }
      }
      else
      {
        base.AddNotification(notification);
      }
    }
  4. Implement the ShouldAutomaticallyAcceptChanges method according to your requirements. For example, you may accept all notifications based on your custom web.config setting, or you can check for notifications of a specific type, e.g. VersionAddedNotification:
  5. private bool ShouldAutomaticallyAcceptChanges(Notification notification)
    {             
      //Accepting notifications based on the setting value
      return Settings.GetBoolSetting("Cloning.AcceptChangesAutomatically", false);
    
      //Accepting notifications of the specific type
      //return notification is VersionAddedNotification;
    }
  6. Compile the created notification provider and place the assembly in the \bin folder of your website.
  7. In the web.config file for each database that uses clones, replace the default notification provider with your own as follows:
  8. <NotificationProvider type="[Fully qualified path to the notification provider class], [assembly containing the class]">
      <param connectionStringName="$(id)">
      </param>
      <param desc="databaseName">$(id)</param>
    </NotificationProvider>

Notes: