Remove Sitecore Personalization Rules in Bulk

Hello Sitecorians,

Do you need to remove the Personalization Rules for many items in bulk or you would like to learn about that on how to do that quickly? If yes then you are at right place. 🙂

Recently there was a requirement to add personalization throughout the site. So the team did that at the template level for the required pages. But they didn’t want that for the specific node and all it’s child items and wanted to keep that away from all personalization and also it will never be applied to that items. There were approx. 1000+ items, so if they remove it manually for each item — it will take ample amount of time. In order to save the time we created a utility that will do the job in few minutes.

We need the following:

  • Parent Item ID – So that we can iterate through all the child items
  • Rendering/Sub-layout ID
  • Data-Source Value – If you would like to add/update
	
	// Remove Personalization Rules in Bulk and Update Data-Source
 
	// Default device from Sitecore
	var deviceItem = db.GetItem("{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}");
	DeviceItem device = new DeviceItem(deviceItem);
 
  //Iterate through all the items
  foreach (Item childItem in parent.Axes.GetDescendants())
  {
    if (childItem.Fields != null && childItem.Fields["__Renderings"] != null && childItem.Fields["__Renderings"].ToString() != string.Empty)
    {
      // Get the rendering where personalization is needed to be removed.
	    var renderings = childItem.Visualization.GetRenderings(device, true).Where(r => r.RenderingID == Sitecore.Data.ID.Parse(txtRenderingID.Text)).ToArray();
 
      if (renderings.Count() == 0)
	    {
        string log = "Item not updated: " + childItem.Paths.Path + ", Info: Rendering with ID " + txtRenderingID.Text + " not found in the item presentation!";
        Sitecore.Diagnostics.Log.Info(log, this);
        continue;
      }
 
	     // Get the layout definitions and the device
       Sitecore.Data.Fields.LayoutField layoutField = new Sitecore.Data.Fields.LayoutField(childItem.Fields[Sitecore.FieldIDs.LayoutField]);
 
       if (!string.IsNullOrEmpty(layoutField.Value))
       {
 
          Sitecore.Layouts.LayoutDefinition layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(layoutField.Value);
 
		      Sitecore.Layouts.DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(device.ID.ToString());
 
		      var renderingItem = deviceDefinition.GetRendering(renderingID);
 
          if (renderingItem != null)
          {
            // Remove all the personalization rules
            renderingItem.Rules.RemoveAll();
 
            // Update the renderings datasource value
            renderingItem.Datasource = txtNewDataSourceID.Text;
 
            // Save the layout changes
            childItem.Editing.BeginEdit();
            layoutField.Value = layoutDefinition.ToXml();
            childItem.Editing.EndEdit();
          }
        }
    }
  }

Let me explain to you what I did in above code.

  1. Get the default device from Sitecore
  2. Iterate through all the child items
  3. Proceed further if an item has any rendering assigned to it
  4. Proceed further if an item has the required rendering, if not then continue with next item
  5. Get the layout definition and the device
  6. Get the rendering and remove all the rules
  7. Save the changes

This is the quickest and easiest solution I found, as there was no Sitecore Powershell installed. If you know any other better solution then do comment below.

Happy Sitecoring!