Skip to content
Home » Helper to process the final renderings in Sitecore items

Helper to process the final renderings in Sitecore items

I was working on a maintenance task in Sitecore content; I needed to remove renderings related to dynamic placeholders, starting with the “small-stacked-accordion-item” name on many items.

The full example is here.

There are 2 main methods of the helper of the solution.

  • GetRenderingsStartingWith to find a specific rendering that starts with a specific name.
  • RemoveRendering to remove a rendering from the Final rendering field of an Item.

Below is the method to find the renderings that start with a specific string.

public static List<RenderingReference> GetRenderingsStartingWith(string startWith, Item item)
		{
			List<RenderingReference> ret = null;
			var renderings = GetRenderingReferences(item, "default");
			if (renderings != null)
			{
				ret = renderings.Where(r => r.Placeholder.Contains(startWith)).ToList();
			}
			return ret;
		}

To remove a specific rendering from the field “Final Layout” below the code.

public static bool RemoveRendering(Item targetItem, string renderingId)
		{
			bool isRenderingRemoved = false;
			int targetIndex = 2; //example
			int usecase = 1;    //example        
			RenderingDefinition instanceOfRendering = null;

			/// /sitecore/layout/Devices/Default
			string defaultDeviceId = "{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}";

			if (targetItem != null)
			{
				/// Get the layout definitions and the device definition
				LayoutField layoutField = new LayoutField(targetItem.Fields[FieldIDs.FinalLayoutField]);
				LayoutDefinition layoutDefinition = layoutField != null ? LayoutDefinition.Parse(layoutField.Value) : null;
				DeviceDefinition deviceDefinition = layoutDefinition != null ? layoutDefinition.GetDevice(defaultDeviceId) : null;
				DeviceDefinition ddef = deviceDefinition != null ? layoutDefinition.GetDevice(deviceDefinition.ID.ToString()) : null;

				if (ddef != null)
				{
					/// Get the array of all renderings for the target page item
					IEnumerable<RenderingDefinition> renderingsArray = ddef.Renderings.ToArray().Cast<RenderingDefinition>();

					if (renderingsArray.Count() > 0)
					{
						switch (usecase)
						{
							case 1: /// Remove first instance of rendering                       
							case 2: /// Remove last instance of rendering
							case 3: /// Remove rendering at specified index
								instanceOfRendering = GetRenderingDefinition(usecase, renderingId, renderingsArray, targetIndex);

								if (instanceOfRendering != null && !string.IsNullOrEmpty(instanceOfRendering.UniqueId))
								{
									ddef.Renderings = new ArrayList(renderingsArray.Where(r => r.UniqueId != instanceOfRendering.UniqueId).ToList());
								}

								break;
							case 4: ///Remove all instances of rendering
								ddef.Renderings = new ArrayList(renderingsArray.Where(r => r.ItemID != renderingId).ToList());
								break;
						}

						/// Save the layout changes
						using (new SecurityDisabler())
						{
							try
							{
								targetItem.Editing.BeginEdit();
								layoutField.Value = layoutDefinition.ToXml();
								targetItem.Editing.EndEdit();
							}
							catch(Exception)
							{ }
						}
					}

					isRenderingRemoved = ddef.Renderings.Count < renderingsArray.Count();
				}
			}

			return isRenderingRemoved;
		}

I hope the solution of this blog works for your needs. See you soon.