Umbraco 4.11 get Parent Node - c#

Should be a simple answer.
I am using Umbraco 4.11 and I need to get the parent Node of a cast node. I am a bit of a noob to C# and I am fixing a control someone else made. Should be simple but it was originally written before the DLL update from 4.7 to 4.11.
So below is my code. I need to get the parent Node. What would be the correct syntax to do this. You can see where the old code is commented out.
Thanks in advance.
//New using
using umbraco.NodeFactory;
private string GetEmailContactProperty()
{
Node node = Node.GetCurrent();
string email = null;
do
{
if (node.NodeTypeAlias == NodeTypeAlias)
{
email = node.GetProperty("emailContact").Value;
if (!String.IsNullOrEmpty(email))
break;
}
//node = node.Parent;
//***Need Parent Node here. new Node is asking for Overload.
node = new Node().Parent;
} while(node.Parent.Id > -1);

The original code should do what you are asking with regards to getting the parent node.
node = node.Parent;

Basically, from your code you want to traverse up the tree, across your ancestors, to find a property named "emailContact" that is not IsNullOrEmpty.
I think what you are looking for is a piece of code like this:
var emailContact = CurrentModel.AncestorsOrSelf().Items.Where(n => !string.IsNullOrWhiteSpace(n.GetProperty("emailContact").Value))
Another way would be to get property with the recursive flag set to true like so:
var emailContact = Model.GetProperty("emailContact ", true).Value;
(See this post: http://our.umbraco.org/forum/developers/razor/19005-Recursive-fields-using-Razor-macro?p=1)
On a side not, it looks like you are currently working with Documents and not "content nodes", is this a back office control or a front end control?
Hope this helps

Related

Selenium C#: Not able to read value from textbox with "pre" html tag

I am trying to read the text value from "Subject" field as mentioned below.
string settingsEmailSubject = iBrowserr.GetValue("//div[#class='sc-view sc-pane sc-panel sc-sheet settings-pane']/div/div[#class='sc-view sc-text-field-view subject-line allow-select']");
But, my code is returning null value. I am seeking your advice to solve this issue.
Notes:
Tried with Webelement, driver.GetAttribute, driver.Selected.GetSelected etc.
xpath:
//div[#class='sc-view sc-pane sc-panel sc-sheet settings-pane']/div/div[#class='sc-view sc-text-field-view subject-line allow-select']/pre[#contenteditable='true' is not working as well.
You can achieve this by selecting the parent element of the pre and then searching in the element for the pre element:
var parent_of_pre = iBrowserr.FindElement(By.XPath("xpath-got-from-chrome"))
var pre_element = parent_of_pre.FindElement(By.XPath("pre"))
string text_pre_element = pre_element.Text
Note that this will work only if the xpath is correct.

Umbraco: Get root Node ID based on Node ID

I'm new to Umbraco, the issue that I have a 5 root nodes, and I've got a list of random pages that are contained within those root nodes. The data I'm receiving back from these pages are NodeId, NodeName and Level. What I'm trying to do is get root node information for each of the pages I have. Unfortunately this is where I'm getting issues, is there a way to get the root node or level 1 node's information based on a NodeId.
This is what I've got so far:
foreach (var item in pages)
{
int level = item["level"].AsInt();
if (level > 1){
var currentItem = library.GetCurrentDomains(item.Id);
}
}
Ive tried library.GetCurrentDomains(item.Id) however this doesnt work.
Not entirely sure if this is what you need, nor if it's the best way, but you could do something like
item.Path.Split(',')[1]
to get the second level "root" of any node. I think ;-)
Assuming the list of random pages are all IPublishedContent, you can use the extension method AncestorOrSelf(1) on the page, which will get the root node for the page. E.g.
foreach (var item in pages)
{
var rootPage = item.AncestorOrSelf(1);
//do something with the root node here
}

Umbraco: Get "Last Edits" values from content

In my umbraco project, i require values in the "last Edited" tab, inside C# code.
How can i get those values?
Please Help,
Thanks.
It's quite simple. Here's one way of doing it:
// Assure the query is done to the root node, in this case I was on the root controller.
var recentUpdatedNodes =
Model.Content.DescendantsOrSelf().OrderByDescending(x =>
x.UpdateDate).Take(50);
foreach (var node in recentUpdatedNodes)
{
#node.Name
#node.Url
#node.Id
}

TreeNode key issue

I am using TreeView control in WinForm.
I am trying to use the following code, but getting "NullReferenceException".
I am following the syntax provided i.e. tree.Nodes[key].Nodes.Add(key,text)
I don't know whats wrong with the code.
Please have a look at the code i used -
tvTree.Nodes.Add("Subjects", "Subjects");
tvTree.Nodes["Subjects"].Nodes.Add("Physics", "Physics");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP1", "Paper1");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP2", "Paper2");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP3", "Paper3");
Thanks for sharing your time.
Your problem is that the "Physics" nodes are not direct children of tvTree but instead are children of the "Subjects" node. What should make this easier is that TreeNodeCollection.Add returns a TreeNode that you can reference later on.
var subjects = tvTree.Nodes.Add("Subjects", "Subjects");
var physics = subjects.Nodes.Add("Physics", "Physics");
physics.Nodes.Add("PhysicsP1", "Paper1");
physics.Nodes.Add("PhysicsP2", "Paper2");
physics.Nodes.Add("PhysicsP3", "Paper3");
If you only have the name, you can use Find:
var parentName = "from wherever";
var parentNodes = tvTree.Nodes.Find(parentName, true);
/* handle multiple results */
/* add children */
Also you may achieve this with
tvTree.Nodes.Add("Subjects", "Subjects");
tvTree.Nodes["Subjects"].Nodes.Add("Physics", "Physics");
var phyNode = tvTree.Nodes.Find("Physics", true).First();
phyNode.Nodes.Add("PhysicsP1", "Paper1");
phyNode.Nodes.Add("PhysicsP2", "Paper2");
phyNode.Nodes.Add("PhysicsP3", "Paper3");
You can use this
tvTree.Nodes["Subjects"].Nodes["Physics"].Add("PhysicsP1", "Paper1");

How to get Hierarchy Node levels of a Telerik treeview

Hi I am populating an dynamic XML file into telerik treeview as hierarchy tree view as below.
Modules //root node
* Organization //Parent node
*Policy //Child of Organization
*Transports
* Employess
*Salary
* Rewards
*Winners
When i am going to select Modules-> Organization -> Policy , I want to get this hierarchy in my client event but Now I am getting the last node value only as "Policy". I want to get the full path of this herarchy.
And my client event code is:
function onSelect(e) {
debugger;
var tv = $('#HierarchyView').data('tTreeView');
var file = tv.getItemValue(e.item); // Here I am getting the last node name as "Policy", I want to get it as Modules-> Organization -> Policy
var nodeElement = e.item;
}
Can anyone please tell me the way to get that Hierarchy? Its Urgent.
Thanks.
This is the solution:
Telerkik Documentation for Node Path with javascript
regards

Categories