ASPX templates
Version 5.x > ASPX templates > Retrieve parent document name View modes: 
User avatar
Member
Member
srizzetto-microgate - 7/5/2010 11:59:25 AM
   
Retrieve parent document name
Is there a method to retrieve the parent's document name of a specific nodelevel ?

Eg. I have this tree:
Menu1
Menu1.1
Menu1.1.1
Menu1.1.1.1
Menu1.1.1.2
Menu1.1.2

Menu2
Menu2.1

..

If I am in one of the red Menu1.1.x page (3rd, 4th, etc level) I want to retrieve my "root" father (in this case Menu1.1)

thanks

User avatar
Kentico Support
Kentico Support
kentico_radekm - 8/2/2010 3:07:10 AM
   
RE:Retrieve parent document name
Hello.

You can use following code and compose cycle (while () {} ) to get requested design.

1. Get current node´s parentID:
CMS.CMSHelper.CMSContext.CurrentDocument.NodeParentID;


2. Get parent according to its NodeParentID via:
CMS.WorkflowEngine.DocumentHelper.GetDocument(Int32, TreeProvider)


You need TreeProvider as it is described here: http://devnet.kentico.com/docs/devguide/selecting_nodes.htm

3. Repeat selection above until current node´s NodeLevel property is requested value.

Best Regards,
Radek Macalik

User avatar
Member
Member
srizzetto-microgate - 8/6/2010 10:36:11 AM
   
RE:Retrieve parent document name
Thanks Radek,
it is exactly what I did. I created a usercontrol with a asp:literal in it and with this recursive code (RootLevel is a public property to set which level of "father" I want to obtain)

Hope it helps to someone else



using CMS.TreeEngine;
using CMS.CMSHelper;

public partial class MicrogatePortal_CMSTemplates_UserControls_RootDocName : System.Web.UI.UserControl
{
private int _rootLevel = 2;
public int RootLevel
{
get { return this._rootLevel; }
set { this._rootLevel = value; }
}


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (CMSContext.CurrentDocument.NodeLevel <= _rootLevel)
lit.Text = CMSContext.CurrentDocument.DocumentName;
else
FindRoot(CMSContext.CurrentDocument.NodeParentID);

}
}

private void FindRoot(int nodeID)
{

CMS.TreeEngine.TreeNode tn = CMS.CMSHelper.TreeHelper.SelectSingleNode(nodeID);

if (tn != null)
{
if (tn.NodeLevel == _rootLevel)
lit.Text = tn.DocumentName;
else
FindRoot(tn.NodeParentID);
}


}


}

User avatar
Kentico Support
Kentico Support
kentico_radekm - 8/9/2010 6:21:10 AM
   
RE:Retrieve parent document name
Hello.

Thank you for posted your solution here. I believe it can inspirate some other users in case of need.

Best Regards,
Radek Macalik