Sometimes you will need to check programmatically if rendering is already added to a Presentation Details in Sitecore. In my case Content Authors doesn’t want to add a modal in renderings each time that they added another component to the view. Also, you can add multiple times that Widget rendering on this page, but the modal should be added once.

To provide a solution I added a helper I will show you the code that I used.


  public static class RenderingHelper
    {
        public static bool HasRendering(string guid)
        {

            var refs = Sitecore.Context.Item.Visualization.GetRenderings(Context.Device, false);
            if (!refs.Any())
                return false;

            var renderingReferences = refs.Where(r => r.RenderingID.ToString().Equals(guid, StringComparison.InvariantCultureIgnoreCase)).ToList();

            if (renderingReferences.Any())
                return true;

            return false;

        }
    }

Then, when you implement the validation the view you can add something like this


@if (!RenderingHelper.HasRendering("{--YOUR RENDERING ID HERE--}"))
{
    @Html.Sitecore().Rendering("{--YOUR RENDERING ID HERE--}", new
{
    DataSource = "{--YOUR DATASOURCE ITEM ID HERE--}"
})
}


I hope this will be useful for you. Happy Coding.