Which pipeline should I use when a new rendering is added in the presentation?

I have a scenario to add renderings programmatically if a particular rendering is added in the item. I will write a custom method to compare if the added rendering's template Id matches the required template ID then it will add renderings programmatically. I have a logic in mind but not sure which pipeline to use.

  • 1
    Ultimately, nothing is added until item:saved or item:saving events fire. Sitecore has no specific pipeline for individual types of item changes. – Mark Cassidy Nov 5 '18 at 8:18
  • @MarkCassidy can we do something in core? – Owais Ahmed Nov 6 '18 at 2:59

It's not a pipeline, but you could use the item:saved event's event args' ItemChanges property for this.

Basically, you would:

  1. Check to see if the item saved was of the template you are looking for

    var savedItem = Event.ExtractParameter(args, 0) as Item;
    if (savedItem == null || savedItem.TemplateID != new ID("YOUR-TEMPLATE-ID"))
  2. Check to see if one of the Sitecore.Data.Items.ItemChanges was the Sitecore.FieldIDs.LayoutField or Sitecore.FieldIDs.FinalLayoutField

    var itemChanges = Event.ExtractParameter(args, 1) as ItemChanges;
    if (itemChanges == null || !itemChanges.HasFieldsChanged || (!itemChanges.IsFieldModified(FieldIDs.LayoutField) && !itemChanges.IsFieldModified(FieldIDs.FinalLayoutField)))
  3. Check to see if the difference between the two includes adding the rendering you care about

  4. Add your other renderings

Note that this will fire every time anything is saved in Sitecore (including the changes you make at step 4 above!), so make sure you put in checks early to get out if your code isn't going to apply.

  • 2
    Nice work adding a comment to exit early, I see too often that this isn't done in events. – jrap Nov 5 '18 at 16:18
  • Thanks for the code . Can we do something in core? Item:saved is triggered when we save an item but I want to do on select rendering button so that more renderings can be attached.Somewhere it is mentioned that Item:saved can affect the performance – Owais Ahmed Nov 6 '18 at 2:58
  • Certainly not easily. You may be able to add a custom experience button to Experience Editor that would programmatically add renderings, but it would either have to 1) force a save so that it could interact with the server version of the presentation details, or 2) add to the client-side version of the presentation details which is not well-documented and would require a deep understanding of Sitecore's Experience Editor JavaScript libraries. – Dan Sinclair Nov 6 '18 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.