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.

  • 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:

    • The instructions above apply to the solutions that are using the SQL Server database, but you can also adjust them to Oracle with minor changes.
    • You can download the sample code here.
    • In newer Sitecore versions, you can enable this feature using the "ItemCloning.ForceUpdate" setting.

Applies to:

CMS 6.4 - 7.2 Update-4, 7.5, 8.0 Initial Release - 8.0 Update-5, 8.1 Initial Release

CMS 7.2 Update-5 (Service Pack-2), 8.0 Update-6, 8.1 Update-1, 8.2 Initial Release

April 06, 2015
November 08, 2016

Reference number:

343487

Keywords: 

  • CMS