|
Certified Developer v7
|
emanuele.firmani-aduno-gruppe
-
10/26/2011 6:52:25 AM
TreeNodeDataSet.Items throwing exception
I'm moving my websites to Kentico 6. I'm trying to use the new APIs to retrieve the menu items. Here is the code I wrote: var tree = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser); var pages = tree.SelectNodes(siteName, null, CurrentCultureCode, true, "CMS.MenuItem", null, null, -1, true); foreach (var p in pages.Items) { ... }
I get the following exception when accessing the Items property: System.InvalidCastException Unable to cast object of type 'CMS.TreeEngine.TreeNodeCollection' to type 'CMS.TreeEngine.TreeNodeCollection`1[CMS.TreeEngine.TreeNode]'.
I had to modify the code this way: foreach (var p in ((ObjectDataSet<TreeNode>)pages).Items)
Is there any other solution? Many thanks Regards Emanuele Firmani
|
|
|
Kentico Developer
|
kentico_ivanat
-
10/27/2011 7:56:11 AM
RE:TreeNodeDataSet.Items throwing exception
Hi, could you please confirm that you sow API example for selecting nodes? Here is an example: private bool GetAndUpdateDocuments() { // Create an instance of the Tree provider first TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
// Fill dataset with documents DataSet documents = tree.SelectNodes(CMSContext.CurrentSiteName, "/API-Example/%", "en-us", false, "CMS.MenuItem");
if (!DataHelper.DataSourceIsEmpty(documents)) { // Loop through all documents foreach (DataRow documentRow in documents.Tables[0].Rows) { // Create a new Tree node from the data row TreeNode editDocument = TreeNode.New(documentRow, "CMS.MenuItem", tree);
string newName = editDocument.DocumentName.ToLower();
// Change coupled data editDocument.SetValue("MenuItemName", newName); // Change document data editDocument.DocumentName = newName;
// Save to database editDocument.Update(); }
return true; }
return false; }
Could you please try to use above code? More examples you can find here: http://your_domain/CMSAPIExamples/Default.aspxBest regards, Ivana Tomanickova
|
|
|
Certified Developer v7
|
emanuele.firmani-aduno-gruppe
-
10/27/2011 8:31:28 AM
RE:TreeNodeDataSet.Items throwing exception
Your code works, but I cannot understand why mine doesn't. Have you tried it? Another question: I'm using two different pieces of code: var pages = tree.SelectNodes(siteName, null, CurrentCultureCode, true, "CMS.MenuItem", null, null, 1, true);
var pages = TreeSecurityProvider.FilterDataSetByPermissions(tree.SelectNodes(siteName, null, CurrentCultureCode, true, "CMS.MenuItem", null, null, 1, true), NodePermissionsEnum.Read, CMS.CMSHelper.CMSContext.CurrentUser);
The first one works, the second doesn't, but I didn't set any security configuration in my pages (all of them have inherits checkbox checked). Maybe I can send you my DB. Thanks Regards Emanuele Firmani
|
|
|
Kentico Developer
|
kentico_ivanat
-
11/7/2011 4:12:26 AM
RE:TreeNodeDataSet.Items throwing exception
Hi, I have tried the code. It throws an error: Unable to cast object of type 'CMS.TreeEngine.TreeNodeCollection' to type 'CMS.TreeEngine.TreeNodeCollection`1[CMS.TreeEngine.TreeNode]'.This should be fixed in the hotfix 4. As for your second question, I have tried both lines and received the same result. Are you calling both methods with the same user?
// Is the tree created using: // TreeProvider tree = new TreeProvider(CMSContext.CurrentUser); var pages = tree.SelectNodes("CorporateSite", null, "en-US", true, "CMS.MenuItem", null, null, 1, true);
var pages2 = TreeSecurityProvider.FilterDataSetByPermissions(tree.SelectNodes("CorporateSite", null, "en-US", true, "CMS.MenuItem", null, null, 1, true), NodePermissionsEnum.Read, CMS.CMSHelper.CMSContext.CurrentUser);
Thank you in advance for information. Best regards, Ivana Tomanickova
|
|
|
Certified Developer v7
|
emanuele.firmani-aduno-gruppe
-
11/7/2011 7:02:09 AM
RE:TreeNodeDataSet.Items throwing exception
I tried your code, but it didn't work. Finally, I deleted and recreated the page, and now it's working. Couldn't understand what went wrong.
TreeProvider.SelectNodes returns a typed DataSet (more or less); will it be a TreeSecurityProvider.FilterDataSetByPermissions that returns also a typed DataSet?
Many thanks for your support. Regards Emanuele Firmani
|
|
|
Kentico Developer
|
kentico_ivanat
-
11/14/2011 2:24:08 AM
RE:TreeNodeDataSet.Items throwing exception
Hi,
both of above methods returns DataSet.
The first one TreeProvider.SelectNodes returns data set with coupled data if class name is specified. Coupled data are data from custom fields defined in document types.
The second method only filter given DataSet and returns the new one with only those records for which the user was granted with required permission.
Best regards, Ivana Tomananickova
|
|
|