1

First time here. Is it possible to access the 'Datasource Template' field on a rendering? If so, how can this be achieved? I need to run a check to ensure that data sources are not removed but also ensure that the user does not copy and paste an invalid datasource path into the presentation details datsource field of a component.

I am quite surprised that Sitecore does not perform a validation on the datasource when this occurs as it does this everywhere else.

Any help or pointers on this issue would be greatly appreciated as there is little to no documentation about this.

I have followed this tutorial here:

http://www.chrissulham.com/protect-your-sitecore-renderings-from-bad-datasources/

However this did not provide any assistance and doesn't seem to work.

I am using GlassMapper.

Cheers,

Jason

1 Answer 1

1

You can have a custom processor in the <mvc.renderRendering> pipelines as below:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <mvc.renderRendering>
         <processor patch:after="processor[@type='Sitecore.Mvc.ExperienceEditor.Pipelines.Response.RenderRendering.AddWrapper, Sitecore.Mvc.ExperienceEditor']"
           type="YourNamespaceWithClass, YourAssembly" />
      </mvc.renderRendering>
    </pipelines>
  </sitecore>
</configuration>

Then in the code, it will be something as below:

using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;

public class CheckMissingDatasource : RenderRenderingProcessor
{
    public override void Process(RenderRenderingArgs args)
    {
        if (!this.IsDatasourceValid(args))
            return;

        //Log error here if datasource is not valid or missing

        args.AbortPipeline();
    }

    private bool IsDatasourceValid(RenderRenderingArgs args)
    {
        if (this.RequiresDatasource(args.Rendering.RenderingItem.InnerItem))
        {
            return !this.ContainsDatasource(args.Rendering);
        }

        return false;
    }

    private bool RequiresDatasource(Item item)
    {
        //Get the Datasource of the current item
        string str1 = item["Datasource Template"];

        if (!string.IsNullOrEmpty(str1))
        {
            return true;
        }
        return false;
    }

    private bool ContainsDatasource(Rendering rendering)
    {
        ID result;
        return !string.IsNullOrEmpty(rendering.DataSource) && ID.TryParse(rendering.DataSource, out result) && PageContext.Current.Database.GetItem(result) != null;
    }
}

Note that I haven't yet test the code, and this will execute for your different items when being rendered. You may also add a check in the method Process to check if you are using the Page Editor/Experience Editor to display an error message to let the user know that there is an invalid or missing datasource.

3
  • this is not an answer to the question. this code is completely different from what is requested by OP Commented Apr 11, 2017 at 7:15
  • @AndreyBobrov, not sure why you're saying it doesn't answer the question. This pipeline is when the renderings are being rendered on the page, so i think this is where the user should check the validity of the datasource Commented Apr 11, 2017 at 7:41
  • This does not work. Tried something similar already from a blogpost.
    – jezzipin
    Commented Apr 11, 2017 at 14:45

Your Answer

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

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