Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Sorting multiple doc types via dataset View modes: 
User avatar
Certified Developer 13
Certified Developer 13
matt-pixelbuilders - 8/13/2012 7:44:43 AM
   
Sorting multiple doc types via dataset
Hi guys,

Have been looking through some suggestions around how to get doc types to sort when multiple doc types are used but haven't managed to fix mine as yet.

My code is:

PageInfo currentPage = CMSContext.CurrentPageInfo;
CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider();
tree.MergeResults = true;
tree.SortMergedResults = true;

DataSet ds = tree.SelectNodes(CMSContext.CurrentSiteName, "/%", CMSContext.PreferredCultureCode, true, "Pixel.PromoHomepageNoHeader;Pixel.PromoHomepageWithHeader", "DocumentMenuItemHideInNavigation = 'False'", "NodeOrder ASC", -1, true, currentPage.NodeGUID, "isrelatedto", true, 6, "");
DataView dv = ds.Tables[0].DefaultView;


I'm trying to sort by NodeOrder and have taken the idea of using the last line from here:
http://devnet.kentico.com/Knowledge-Base/API-and-Internals/How-to-sort-documents-based-on-two-document-types.aspx

However I'm still getting the doc types being returned in the order they are passed into the dataset, can anyone help?

Thanks, Matt.


User avatar
Certified Developer 13
Certified Developer 13
matt-pixelbuilders - 8/13/2012 7:46:40 AM
   
RE:Sorting multiple doc types via dataset
It may also be worth noting that I'm pulling the doc types out onto the page via a foreach statement:

foreach (DataRow dr in ds.Tables[0].Rows)
{ }

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/13/2012 9:26:49 AM
   
RE:Sorting multiple doc types via dataset
When you return multiple document types in a query, they come back in separate tables in the dataset, mainly because they have different columns. I think you can sort them as one collection if you access them as TreeNodes. Try this:


var nodes = tree.SelectNodes(CMSContext.CurrentSiteName, "/%", CMSContext.PreferredCultureCode, true, "Pixel.PromoHomepageNoHeader;Pixel.PromoHomepageWithHeader", "DocumentMenuItemHideInNavigation = 'False'", "NodeOrder ASC", -1, true, currentPage.NodeGUID, "isrelatedto", true, 6, "");
if(!DataHelper.DataSourceIsEmpty(nodes))
{
var sorted = nodes.Items.OrderBy(x=> x.NodeOrder);
foreach(TreeNode n in sorted)
{
//DO SOMETHING
}
}



User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/13/2012 11:13:14 AM
   
RE:Sorting multiple doc types via dataset
Although it should be done during the SelectNodes method, try calling DataHelper.SortDataTable(ds.Tables[0], orderBy); on the dataset again to see if there is any change in your results.

User avatar
Certified Developer 13
Certified Developer 13
matt-pixelbuilders - 8/13/2012 11:32:20 AM
   
RE:Sorting multiple doc types via dataset
Thanks for your help Jiveabillion. I've tried both methods, the sortdatatable method doesn't appear to have any further effect when called.

Pulling the items out as TreeNodes was going OK, however it didn't seem take the ClassNames into effect as it just brought out all documents in the tree - not only the two doc types requested.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/13/2012 12:09:02 PM
   
RE:Sorting multiple doc types via dataset
That's very strange because it should only have the items that were in the TreeNodeDataSet returned by the SelectNodes method. Try calling the OrderBy expression directly on the nodes object.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 8/13/2012 12:14:56 PM
   
RE:Sorting multiple doc types via dataset
When you enumerate the TreeNodeDataSet returned by SelectNodes it actually calls GetEnumerator on the Items property. So it would be the same as foreach(var n in nodes.Items)

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/13/2012 7:59:00 PM
   
RE:Sorting multiple doc types via dataset
Hi,

are you sure that code is not working? I have just tested similar code sample and it worked correctly.:

CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider();
tree.MergeResults = true;
tree.SortMergedResults = true;

DataSet ds = tree.SelectNodes(CMSContext.CurrentSiteName, "/%", CMSContext.PreferredCultureCode, true, "cms.news;cms.article", "DocumentMenuItemHideInNavigation = 'False'", "NodeLevel, NodeOrder, NodeName ASC", -1, true, CMSContext.CurrentDocument.NodeGUID, "isrelatedto", false, 6, "");
DataView dv = ds.Tables[0].DefaultView;
}


I inserted the code on home document. Then in the content tree I had news and articles document (mixed up).

Then I created a relationship so that home document was on the right site in relation (that is a reason why I changed the parameter from true to false.

Finally, I change order by condition as NodeOrder can be same for several documents.

With above condition are documents selected in the order they exist in the content tree.

You can see it if you open dataview (DataView dv = ds.Tables[0].DefaultView;) in debug window.

Best regards,
Ivana Tomanickova

User avatar
Certified Developer 13
Certified Developer 13
matt-pixelbuilders - 8/14/2012 3:26:07 AM
   
RE:Sorting multiple doc types via dataset
Hi guys,

Figured out why using the DataView wasn't working, was foolishly not using the DataView for the foreach statement and was still looping through the original dataset so changed the foreach to:

foreach (DataRowView dr in dv)


This meant I also didn't need to use the SortMergedResults method.

Thanks for all your help,

Matt.