Sitecore Multi list Code Data Source




Recently, in my project, I had a requirement where, I had to show Items in a list dynamically based on its parent, and we are currently using Multi list with search,

and I found it's better to use a custom data source as the query and fast query doesn't work for my case,

so, I used the "code" Data source as it's the best solution for my case

Below is an example of how I implemented this approach:

    Case:

    My case was to map the department with it's company,
    so I put the path of the company on the parent of the department

    Code:

    firstly, you have to add reference for Sitecore.Bucket.dll as we will implement IDatasource interface
        
        public class DepartmentCustomDataSource : IDataSource
        {
            public Item[] ListQuery(Item item)
            {
                // department company
                Item parent = item.Parent;
                string departmentLink = parent[Templates.CompanyDepartment.Fields.DepartmentMapping];
                var database = Sitecore.Context.ContentDatabase;
                var erpCompany=database.GetItem(departmentLink);
                var erpDepartments = erpCompany.Children.ToArray();
                return erpDepartments;
            }
        }
        
        

    CMS:

    in the source of your Multi list field put it the below format

    code:"your class namespace with method", "dll name"


    Sitecore multi list code data source







    Comments

    Post a Comment

    Popular posts from this blog

    Organize media files using Sitecore PowerShell

    C# Numeric types Overflow