4

I have a custom controller to send message. So I need to get the value of property field name and alias = "email", this will be used to send the email to.

this code below works

 var id = umbraco.uQuery.GetNodeByUrl("/contact-us");
 IPublishedContent root = Umbraco.TypedContent(id.Id);
 return root.GetProperty("email", true).Value.ToString();

However the problem here is if the page name changes, the url will change and the code will break.

So how can I change the code above to get the current page id and insert it here (???);?

I think the code should be something like this:

 IPublishedContent root = Umbraco.TypedContent(???);
 return root.GetProperty("email", true).Value.ToString();

Any help will be apprecciated

2 Answers 2

10

Your solution will bring a problem if you have more than 1 'ContactUs' node, or none. Then you don't know which one is going to be get.

(Actually it's the first one found in the node tree, but then someone can change the order of them...)

Is your controller a Surface controller? You can just do this:

IPublishedContent currentNode = Umbraco.TypedContent(CurrentPage.Id)
2

Ok, I found the solution for my question.

var nodes = umbraco.uQuery.GetNodesByType("ContactUs");
if (nodes.Any())
{
   IPublishedContent node = Umbraco.TypedContent(nodes.First().Id);
   return node.GetProperty(property, true).Value.ToString();
}

Hope this will help somebody with the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.