How to sort documents based on two document types using API

   —   
You might need to be able to select documents, like articles and news, and sort them according to the name of the document. 
1. The default behavior after you have used the code below is that the data set will contain two tables, one with articles, the second with news.

TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
DataSet nodes = tree.SelectNodes("CorporateSite", "/%", "en-US", true, "cms.article;cms.news","","NodeName ASC");


2. If you would like to merge data into one table you can extend the code using:

TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
tree.MergeResults = true;
tree.SortMergedResults = true;

// Get parent node
DataSet nodes = tree.SelectNodes("CorporateSite", "/%", "en-US", true, "cms.article;cms.news","","NodeName ASC");


The result will be that you have one table containing both articles and news. Both sets of documents will be ordered, but separately (not mixed), so all articles will appear above all news documents.

3. Now if your goal is to mix the documents together and order them according to the NodeName field, you will need to add the last line to your code:

TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
tree.MergeResults = true;
tree.SortMergedResults = true;

// Get parent node
DataSet nodes = tree.SelectNodes("CorporateSite", "/%", "en-US", true, "cms.article;cms.news","","NodeName ASC");
DataView nView = nodes.Tables[0].DefaultView;


Note: To order data correctly, you have to use the field which is common to both document types.

-it-


See also: Document API

Applies to: Kentico CMS 6.x, 7.x
Share this article on   LinkedIn