Showing the thumbnail image of the current rendering

Search for a command to run...

No comments yet. Be the first to comment.
Polly is great library for easily configuring different kinds of policies around code execution, be it retry, circuit breaker, caching or lots of other things. It's probably most often used around HTTP requests. If you haven't heard about it you defi...

This is part 2 of my series on unit testing in .NET (C#) and will be about my conventions regarding naming unit tests, how to structure them and which parts to test. And lastly I will help you increase readability of assertions and potential error me...

I feel like unit testing is very important and very beneficial in a lot of ways. It helps with not breaking existing functionality and thereby also making you more confident in changing code without fear of breaking important stuff. Often it also mak...

I've been working with Swagger through NSwag a lot recently and also needed to get it to work when hidden behind a reverse proxy - i.e. another service forwarding a request to the service exposing the Swagger UI. Some of the issues I've had along the...

The other day I was writing a unit test to verify what was supposed to happen when a Task would throw an exception when awaited. It wasn't as straight forward as I first assumed, but I quickly found this Stack Overflow post that helped me: https://st...

I recently ran into an issue with a component in a placeholder that made it troublesome for the editors to select it within the Experience Editor to either select a new data source for the component or to get to the placeholder to insert more renderings.
The issues came from the fact that it was a “live” component with an <iframe>, so when you clicked on the component you actually clicked inside the iframe and Sitecore didn’t catch that, so you couldn’t select the component.
I decided to go with a simple solution – just show the rendering thumbnail (the image shown when you are choosing the rendering to insert into a placeholder) when in the Experience Editor. That way you will still get a good preview of the layout of the page although the component isn’t “live” anymore, but this is only while editing.
To do this I needed to get the current rendering item, from there the thumbnail and in the end the URL for that image.
I ended up with something like this method:
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Mvc.Presentation;
using Sitecore.Resources.Media;
public static string GetRenderingThumbnailUrl() {
var renderingItem = RenderingContext.Current.Rendering.RenderingItem.InnerItem;
var thumbnailField = (ImageField)renderingItem.Fields[Sitecore.FieldIDs.Thumbnail];
return MediaManager.GetMediaUrl(thumbnailField.MediaItem);
}
You can expose/use it in many different ways, but basically you just get the current rendering item from the RenderingContext and from that you get the thumbnail from the corresponding field and use the MediaManager to create the URL.