Custom Collection of Items in Sitecore Multilist field datasource

I recently got a requirement to show upcoming News (News which date is greater then today’s date) into multilist field on Sitecore Item from News collections in content tree. So after looking into the requirement first thing came in my mind to create a custom Multilist field as we need to show dynamic collections of news on the bases  of current date (Today’s date). Or we need to provide custom datasource to multilist which should be driven by our custom code.

As much as I know about datasource is we can use fast query and query in that but I was not sure we can use custom code also in Datasource field. And I am sure many of us don’t know about the same.

I can see few of blog post about the same but I think because of keyword or meta data it is very difficult to find so I thought to write it again.

First I have created a class file and inherited that class with IDataSource interface (We need to provide reference of Sitecore.Buckets.dll for IDataSource).  And then I have implemented ListQuery method which is defined in IDataSource interface.

ListQuery method return type is Array of items so here we can do our custom code to get the array of items and return that collection as given in below script  –

In below code I am returning upcoming news items  –

public Item[] ListQuery(Item item)
            item = Sitecore.Context.ContentDatabase.GetItem("/sitecore/content/Habitat/Home/Modules/Feature/News/News");
            return item.Axes.GetDescendants().Where(i=>i.IsDerived(Templates.NewsArticle.ID)).Where(i=>DateUtil.IsoDateToDateTime(i[Templates.NewsArticle.Fields.Date])>DateTime.Now).ToArray();

And then I logged in to Sitecore and defined the Datasource in datasource field. Now in datasource field we need to define  code:NameSpace.ClassName, AssmeblyName as per given in below screenshot.

We will come with something new in next post…..

This entry was posted in Sitecore, Sitecore Multilist, Sitecore Search and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s