6

I'm using Sitecore 8.1 rev. 151207 - And I have that rendering which only should be placed once on a page.

Is it possible to limit that in terms of letting the user not add another one of the same rendering again. The only way I can think of would lead to possible wasted Renderings and items which are put on the page but are not shown and I think thats not best practice.

I already searched for solutions but nothing showed up so far

1

2 Answers 2

5

Yes you can. It will take a little code and a pipeline change, but you can find the code for mvc here and webforms here.

What you want to do it count the number of times a rendering has been put on the page and stop it if it passes your max number.

public class PerformRendering : Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.PerformRendering
{
    protected override IEnumerable<Rendering> GetRenderings(string placeholderName, RenderPlaceholderArgs args)
    {
        // get all renderings
        var renderings = base.GetRenderings(placeholderName, args);

        // return all renderings in Experience Editor
        if (Sitecore.Context.PageMode.IsExperienceEditor) return renderings;

        // get the maximum number of components
        var maxComponents = this.GetMaxComponents(args);

        // return only first n components
        return maxComponents > 0 ? renderings.Take(maxComponents) : renderings;
    }

    private int GetMaxComponents(RenderPlaceholderArgs args)
    {
        var maxComponents = 0;
        if (args.CustomData.ContainsKey("maxComponents"))
        {
            int.TryParse(args.CustomData["maxComponents"].ToString(), out maxComponents);
        }

        return maxComponents;
    }
}

The config change

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <mvc.renderPlaceholder>
        <processor patch:instead="*[@type='Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.PerformRendering, Sitecore.Mvc']"
                   type="Website.Pipelines.RenderPlaceholder.PerformRendering, Website" />
      </mvc.renderPlaceholder>
    </pipelines>
  <sitecore>
</configuration>
2
  • The OP is trying to limit the use of a single rendering, not the number of renderings in a single placeholder Feb 2, 2017 at 6:01
  • 1
    So like only use this rendering once on a page?
    – Chris Auer
    Feb 2, 2017 at 12:13
1

There's nothing built in, and there's two ways you could go about it.

You could hook into the item:saving event and check the renderings there. If there are duplicates of known "single-use" renderings, you throw an error which will prevent the save. This could also be done via a custom validator.

Alternatively, you could allow the save but then only render the first instance. You should just be able to use dnstommy's answer as a base and filter out any renderings that have already executed this request, using a Set<ID> stored in HttpContext.Current.Items to keep track of them. However, you might run into problems with this approach if you have caching rules on some higher level composite renderings.

Your Answer

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

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