10

Out of the box it is possible to restrict items in a fields data source by its base template type. I can specify a filter as follows that restricts to items of type Employee...

DataSource=/sitecore/content/home/people/employees&IncludeTemplatesForSelection=Employee

If Employee uses Person as its base template, I'd like to specify something like this...

DataSource=/sitecore/content/home/people&IncludeBaseTemplatesForSelection=Person

That would allow me to select any item that has Person as its base template (so allow I could select Person and Employee items, or any other item that derives from Person base template.).

Is this possible?

6
  • I do not believe this is possible out of the box. You would need a custom solution for it
    – Richard Seal
    Commented Oct 5, 2016 at 14:00
  • More than happy to write a custom solution. I was thinking I might have to plug something somewhere into the pipeline. I've already have a thorough poke around without much luck though. I wonder if anyone's already done something custom around this already that's in the public domain? Commented Oct 5, 2016 at 14:05
  • @PhilHarvey Are you wanting to make this change for the "Datasource Location" field on renderings or the "Source" of template fields (e.g., Droptree, Treelist, Multilist)? Commented Oct 5, 2016 at 14:13
  • @CoreySmith i'd like to do this on the "Source" of template fields. It would be nice of I could do it on Datasource Location too, but my requirement is to have this on fields. Commented Oct 5, 2016 at 14:19
  • @JammyKam and I have both blogged on extending the Treelist field Source. You might find these helpful in your custom solution. - JammyKam's post: jammykam.wordpress.com/2016/01/06/… - My post: blogs.perficient.com/microsoft/2016/05/… Commented Oct 5, 2016 at 14:36

1 Answer 1

1

I've had to do something similar recently which involved extending the treeview and used JammyKams post as a guide so thought I'd have a go at putting something together for this:

public class TreelistWithChildTemplateSupport : TreeList
{

    [Description("Base templates to include.")]
    [Category("Data")]
    public string IncludeBaseTemplatesForSelection
    {
        get { return GetViewStateString("IncludeBaseTemplatesForSelection"); }
        set
        {
            Assert.ArgumentNotNull(value, "value");
            SetViewStateString("IncludeBaseTemplatesForSelection", value);
        }
    }

    public new virtual void Add()
    {
    if (!this.HasIncludeBaseTemplatesForSelection(selectionItem))
      return;
    base.Add();
   }

  private bool HasIncludeBaseTemplatesForSelection(Item item)
  {
  Assert.ArgumentNotNull((object) item, "item");
  if (this.IncludeBaseTemplatesForSelection.Length == 0)
    return true;
  return HasItemBaseTemplate(item, this.IncludeBaseTemplatesForSelection);
 }
}

Then in the HasItemBaseTemplate() you could do something similar to Anders post here: https://laubplusco.net/sitecore-extensions-does-a-sitecore-item-derive-from-a-template/

I've not tested this code but hopefully this is a good start for you.

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.