Post Pic

Umbraco : Locate nearest node with specific property

Umbraco logo

In an ongoing project I am working on I needed to adapt a custom user control that was being used as a DataType in Umbraco. I needed to locate the nearest node that had a specific property set and default the selected value of my control to the value of the property in the parent document.

Here is the final piece of code:

//- Get the current document as our starting point
Document currentDoc = new Document(Convert.ToInt32(Request.QueryString["id"]));

//- Climb the tree looking for a parent node with the property set
while (currentDoc.getProperty(“myProperty”) == null)
{
currentDoc = new Document(currentDoc.Parent.Id);
}
//- Set the value in the list if it exists
if (this.myDropDownList.Items.FindByValue(currentDoc.getProperty(“myProperty”).Value.ToString()) != null)
{
this.myDropDownList.Items.FindByValue(currentDoc.getProperty(“myProperty”).Value.ToString()).Selected = true;
this.myDropDownList.Enabled = false; //-Disable the control so the branch can’t be changed
}

Thanks again to Tim Geyssens for the tip that led me to my final solution.

Related Posts

  1. Umbraco : No node exists with id ’1040′

Related posts brought to you by Yet Another Related Posts Plugin.

One Response

03.31.09

Hi there, I’m looking for a certified Umbraco developer to help me with a website build. Would you be interested in finding out more? I can’t see a contact email on your site anywhere, could you drop me a line if you’re interested? Thanks!

Leave Your Response

* Name, Email, Comment are Required

Umbraco Certified Developer
FreshBooks
ElasticHosts: Flexible Servers in the Cloud
SagePay Approved Partner
Microsoft WebsiteSpark