Getting the number of renderings within a placeholder

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...

Sometimes you might want to do something if there are no renderings in a placeholder or if there is a specific amount. You might want to show editors a warning or some help text if there are no renderings.
Unfortunately there is not a simple out-of-the-box way to do this in Sitecore, but it is somewhat trivial to do yourself.
I ended up with these two extension methods – one for default placeholders and one for dynamic placeholders which many (if not most) solutions use.
public static int RenderingsInDynamicPlaceholder(this Item item, string placeholder)
{
var regex = new Regex($"/{placeholder}" + @"_\w{8}(?:-\w{4}){3}-\w{12}$", RegexOptions.IgnoreCase);
var renderingReferences = item.Visualization.GetRenderings(Sitecore.Context.Device, true);
var renderingsInPlaceholder = renderingReferences.Where(r => regex.IsMatch(r.Placeholder));
return renderingsInPlaceholder.Count();
}
public static int RenderingsInPlaceholder(this Item item, string placeholder)
{
var renderingReferences = item.Visualization.GetRenderings(Sitecore.Context.Device, true);
var renderingsInPlaceholder = renderingReferences.Where(r => r.Placeholder.EndsWith("/" + placeholder));
return renderingsInPlaceholder.Count();
}
These might not work in 100% of cases, but they should work in most. You could make the GUID-regex for the dynamic placeholder more explicit, but I don’t see any practical need for it.
The methods basically run through all renderings and check their placeholder to see if it ends with the one we’ere looking for. In the case of the dynamic placeholders we need to use regex as they have a dynamically generated GUID appended to the placeholder name.