Placeholder Settings allow you to choose what Renderings / Sublayouts you want to be selectable to add in to the placeholders of your layouts.

Each time you create a new Rendering, you need to update the placeholder settings too so that the editors can use the rendering. If you keep your renderings nicely organised within a folder structure, then there’s a simple method for making this a bit more flexible and having Sitecore automatically pick up the renderings by referencing a folder of renderings rather than the rendering itself.

Within the Content Editor for the settings, Sitecore allows you to add a rendering folder, rather than a rendering itself:

Typically, selecting the folder would have no effect. However, with the following pipeline, we can alter the functionality of Sitecore so that if a folder has been added, it pulls in all of the child renderings that sit underneath it.

This can be very useful if you keep your renderings organised by functionality, so for example you want a “Forms” placeholders setting that will contain all of the Form renderings you have created. If you keep them together in the same folder, you can have them added automatically, without having to update the settings.

Creating the pipeline class

The following is all the code we need for the pipeline step:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class GetRenderingsFromFolders
{
public void Process(GetPlaceholderRenderingsArgs args)
{
Assert.IsNotNull((object)args, "args");
if (args.PlaceholderRenderings == null || args.PlaceholderRenderings.Count == 0)
return;
var newRenderings = args.PlaceholderRenderings
.Where(ItemIsAFolder)
.SelectMany(GetChildRenderings)
.Distinct(new ItemIdComparer())
.ToList();
args.PlaceholderRenderings.AddRange(newRenderings);
}
private bool ItemIsAFolder(Item item)
{
return item.DerivesFromTemplate(ID.Parse(TemplateIds.Folder)) ||
item.DerivesFromTemplate(ID.Parse(TemplateIds.RenderingsFolder));
}
private IEnumerable<Item> GetChildRenderings(Item item)
{
// Can just return all children - the pipeline already handles only keeping hold of items
// that are rendering items.
return item.Children;
}
}

The code for this is relatively straight-forward. It makes use of a standard DerivesFromTemplate extension method that you’ll need to implement. There is also a static class used for ID references, but you could just drop the IDs straight in.

The GetChildRenderings method is actually extremely simple and could be removed, but it’s left in to illustrate that you could replace this with other logic, such as all descendants of an item, or perhaps using a parameter to determine what renderings to pick up.

Adding the config

With this code in our solution, we need to add the command to config so that it can be referenced. This can done with a simple config file in the Include folder, with the appropriate assembly + class reference:

1
2
3
4
5
6
7
8
9
10
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<getPlaceholderRenderings>
<processor type="YourWebsite.Sc.Pipelines.GetPlaceholderRenderings.GetRenderingsFromFolders, YourWebsite.Sc"
patch:after="*[@type='Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings, Sitecore.Kernel']" />
</getPlaceholderRenderings>
</pipelines>
</sitecore>
</configuration>

And that’s it! You can now just reference folders in your Placeholder Settings and all of the child renderings underneath will be pulled in.