2

I'm trying to get the renderings of the current item being edited. So, when components are added to a placeholder in the Experience Editor, I want to be able to get those renderings.

If I use Context.Item.Visualization.GetRenderings, this obviously gets the renderings of the context item, but it's the saved item, not the one that's currently being edited.

I'm wondering if there's any way to either get the current edit item, or if there's a pipeline that I can intercept what has currently beeing added.

3 Answers 3

1

If you want to get that information when you're saving an item, you can add your own processor to item:saving event.

public class MyHandler
{
  protected void OnItemSaving(object sender, EventArgs args)
  {
    Item newItem = Event.ExtractParameter(args, 0) as Item;

    if (newItem == null || newItem.Database.DataManager.GetWorkflowInfo(newItem) == null)
    {
      return;
    }

    Item originalItem =
      newItem.Database.GetItem(newItem.ID, newItem.Language, newItem.Version);
      
    var newItemRenderings = newItem.Visualization.GetRenderings(Context.Device, false);
    var originalItemRenderings = originalItem.Visualization.GetRenderings(Context.Device, false);
    
    // your code to compare newItemRenderings with originalItemRenderings
  }
}

Remember to register your processor:

<sitecore>
  <events>
    <event name="item:saving">
      <handler type="My.Assembly.Namespace.MyHandler,
        My.Assembly" method="OnItemSaving" />
    </event>
  </events>
</sitecore>
3
  • The point is the item is being edited, not saved. If it was saved, I could just get the context item. But I want to get the rendering of the item being edited - it hasn't been saved yet.
    – Jon Morgan
    Commented Sep 7, 2022 at 9:45
  • item:saving event is called before the item is saved. You can still cancel the save action. But I get your point. I don't think you can really get the information you're looking for in any way in the backend.
    – Marek Musielak
    Commented Sep 7, 2022 at 9:47
  • Yeah, I thought that may be the case. I think it's done mostly front-end, although it does call the controller when I add a new component to the placeholder. It's at this point I want to calculate the number of components in the placeholder.
    – Jon Morgan
    Commented Sep 7, 2022 at 10:02
0

You can get the current item rendering through this code :-

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item currentItem = master.GetItem("<path of your item>");

RenderingReference[] renderings = currentItem.Visualization.GetRenderings(Context.Device, false);

foreach (RenderingReference rendering in renderings)
{
<your code>
}

You can use Context.Database.Name to get the database name dynamically.

Hope this helps!!

2
  • 2
    this one takes just the saved components from the current item. Commented Sep 7, 2022 at 8:40
  • The item is being edited. So in your code, 'currentItem' would need to get the edited item - it hasn't been saved yet, so it's not going to exist in master (or any) database.
    – Jon Morgan
    Commented Sep 7, 2022 at 9:47
-1

I would say that there are some events that you can identify when you see the ShowConfig

https://yoursite/sitecore/admin/showconfig.aspx

These events are

<event name="item:added"/>
<event name="item:adding"/>

And you can check their config details also. You need to extract the DLLs of these events and you will get some logic that you can override and use your assembly in place of this.

Refer to this article for more details.

https://doc.sitecore.com/xp/en/SdnArchive/Articles/API/Using%20Events.html

I hope this help.

1
  • I think events only get fired on database changes; and these, only with items. So the change in rendering whilst rendering doesn't fire any events.
    – Jon Morgan
    Commented Sep 7, 2022 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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