Many times we have realized the need of Treelist field which can take multiple datasource from different locations.
Suppose we have article which author can be from “Management Team” and “Board of directors”.

If we have such a need we can create our own custom field named “MultiDatasourceTreeList” in the core database at the location “/sitecore/system/Field types”-

1. Create a folder “Custom Type”
2. Add a field with name “Multi Datasouce Treelist” in this folder of the template type “/sitecore/templates/System/Templates/Template field type”
3. Add the Assembly and class details which extend the functionality of sitecore out of the box tree list class.

using System;
using System.Linq;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.ContentEditor;
using Sitecore.Text;
using Sitecore.Web;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.Web.UI.WebControls;


namespace CommonLibrary
{
    public class MultiDatasourceTreeList : TreeList
    {
        protected override void OnLoad(EventArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            base.OnLoad(args);

            if (!Sitecore.Context.ClientPage.IsEvent)
            {
                var existingTreeView = (TreeviewEx)WebUtil.FindControlOfType(this, typeof(TreeviewEx));
                var treeviewParent = existingTreeView.Parent;

                existingTreeView.Parent.Controls.Clear();
                
                var dataContext = (DataContext)WebUtil.FindControlOfType(this, typeof(DataContext));
                var dataContextParent = dataContext.Parent;

                dataContextParent.Controls.Remove(dataContext); 
               
                var MultiRootTreeView = new Sitecore.Web.UI.WebControls.MultiRootTreeview();
                MultiRootTreeView.ID = existingTreeView.ID;
                MultiRootTreeView.DblClick = existingTreeView.DblClick;
                MultiRootTreeView.Enabled = existingTreeView.Enabled;
                MultiRootTreeView.DisplayFieldName = existingTreeView.DisplayFieldName;
               
                var dataContexts = ParseDataContexts(dataContext);

                MultiRootTreeView.DataContext = string.Join("|", dataContexts.Select(x => x.ID));

                foreach (var context in dataContexts)
                {
                    dataContextParent.Controls.Add(context);
                }

                treeviewParent.Controls.Add(MultiRootTreeView);
            }
        }
               
        protected virtual DataContext[] ParseDataContexts(DataContext originalDataContext)
        {
            return new ListString(DataSource).Select(x => CreateDataContext(originalDataContext, x)).ToArray();
        }

        /// 
        /// Creates a DataContext control for a given Sitecore path data source
        /// 
        protected virtual DataContext CreateDataContext(DataContext baseDataContext, string dataSource)
        {
            DataContext dataContext = new DataContext();
            dataContext.ID = GetUniqueID("D");
            dataContext.Filter = baseDataContext.Filter;
            dataContext.DataViewName = "Master";
            if (!string.IsNullOrEmpty(DatabaseName))
            {
                dataContext.Parameters = "databasename=" + DatabaseName;
            }
            dataContext.Root = dataSource;
            dataContext.Language = Language.Parse(ItemLanguage);

            return dataContext;
        }
    }
}

For author take the filed type “Multi Datasource Treelist” with Pipe (|) separated datasouce: Datasource=/sitecore/content/Home/Management Team|/sitecore/content/Home/Board of Directors

Finally at article you will able to select authors form both the section “Management Team” and “Board of directors”

Advertisements