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
}
Related
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
}
This problem seems simple enough. I have a treeview, let's call it MyTreeView, populated with all of the drive letters, so the treeview looks like this:
A:\
C:\
D:\
F:\
How do I check if the treeview contains a specific item? How does the treeview identify its items?
I have created a MessageBox to show MyTreeView.Items.GetItemAt(1), and it identifies item 1 as:
"System.Windows.Controls.TreeViewItem Header:C:\ Items.Count:1"
Try the easiest thing first, which obviously doesn't work:
if (MyTreeView.Items.Contains(#"C:\")
{
MessageBox.Show(#"Tree contains C:\");
}
The next easiest thing would be to try making a TreeViewItem that looks similar to what I want, which also doesn't work:
TreeViewItem myItem = new TreeViewItem();
myItem.Header = #"C:\";
if (MyTreeView.Items.Contains(myItem)
{
MessageBox.Show("Tree contains " + myItem.ToString());
}
Just to make sure I had the fundamental concept right, I tried some circular logic, which actually does work:
var myItem = MyTreeView.Items.GetItemAt(1);
if (MyTreeView.Items.Contains(myItem)
{
MessageBox.Show("Tree contains " + myItem.ToString());
}
Which outputs:
"Tree contains System.Windows.Controls.TreeViewItem Header:C:\ Items.Count:1"
What am I doing wrong? How do I check if my tree contains something like "C:\" ?
edit:
The code for building the tree is this:
(basically a copy and paste from the internet)
foreach (string myString in Directory.GetLogicalDrives())
{
TreeViewItem item = new TreeViewItem();
item.Header = myString;
item.Tag = myString;
item.FontWeight = FontWeights.Normal;
item.Items.Add(dummyNode); // this is (object)dummyNode = null
item.Expanded += new RoutedEventHandler(DWGFolder_Expanded);
item.Selected += new RoutedEventHandler(DWGFolder_Selected);
// the Expanded event is very similar,
// subitem.Header is the folder name (Testing),
// while subitem.Tag is the full path (C:\Testing)
MyTreeView.Items.Add(item);
}
So basically I'm trying to match TreeViewItem objects.
I believe .Contains() would check for the value by reference since it isn't a simple string object. This requires you to iterate through each of the items until you retrieve the item which matches the header.
LINQ Example
if (MyTreeView.Items.Cast<TreeViewItem>().Any(item => item.Header.ToString() == #"C:\"))
{
MessageBox.Show(#"Tree contains C:\");
}
Contains looks for the exact same instance inside the collection. If you don't have the object you want to check already, you can't use Contains.
But you can use some basic LINQ query... Add the LINQ namespace to your class:
using System.Linq;
If your items are indeed just Strings, then use this query (EDIT - Though, in case they're just Strings, Contains should work, since their equality comparers don't behave like those of regular reference types, but compare by value):
if (MyTreeView.Items.Cast<string>().Any(s => s == #"C:\"))
{
// Do stuff
}
If your items are TreeViewItems, you can use this one:
if (MyTreeView.Items.Cast<TreeViewItem>().Any(i => i.Header.ToString() == #"C:\"))
{
// Do stuff
}
But your items could be any class we don't know, or your header binding could change... Without knowing how you're adding the items to the TreeView, it's hard to give you the best alternative.
EDIT - Keep in mind that this will only search in the first level of the tree. If the item you're looking for is placed somewhere deeper, you'll have to do a recursive search. At that point, maybe just keeping the values stored somewhere from the start would be better.
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
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");
I nominate me for village idiot.
Why doesn't this work:
foreach (XElement clientField in _clientXml.Descendants("row").Descendants())
{
var newFieldName =
from sourceField in _sourceEntries.Descendants("Field")
where (string)sourceField.Attribute("n") == (string)clientField.Attribute("n")
select new
{
FieldName = ((string) sourceField.Attribute("n")),
AcordRef = ((string) sourceField.Attribute("m"))
};
foreach (var element in newFieldName)
{
Console.WriteLine("Field Name: {0}",
element.FieldName, element.AcordRef);
}
}
My source XML files are loaded with XElement.Load(myFileName). In debug, clientField has an attribute n="Policy Number". The first element of _sourceEntries.Descendants("Field") also has an attribute n="Policy Number". Indeed, each element in _clientXml.Descendants("row").Descendants() has a matching row in _sourceEntries.Descendants("Field"). And, I know just enough to know that the select is lazy, so in debug I look at the Console.WriteLine block. No matter what I've tried, newFieldName is an empty set.
Just in case, here's the first element of the client file:
<Column_0 n="Policy Number">ABC000123</Column_0>
And, here's the fist element of the _sourceEntries collection:
<Field n="Policy Number" c="1" l="" s="" cd="" m="1805" f="" />
I know it's going to be something simple, but I just don't see what I'm doing wrong.
Thanks.
Randy
This accomplished what I ultimately needed to do:
foreach (var clientField in _clientXml.Descendants("row").Descendants())
{
foreach (var acordMapRef in
from sourceEntry in _clientTemplate.Descendants("SourceEntries").Descendants("Field")
where (string) clientField.Attribute("n") == (string) sourceEntry.Attribute("n")
from acordMapRef in _clientTemplate.Descendants("Acord").Descendants("Field")
where (string) sourceEntry.Attribute("m") == (string) acordMapRef.Attribute("id")
select acordMapRef)
{
clientField.Attribute("n").Value = (string) acordMapRef.Attribute("n");
}
}
But, it's surely a candidate for ugliest code of the month. One thing I noticed in fooling around is that elements in an XElement tree don't seem to match to XElements in an IEnumerable collection. You might notice in the original code, above, I had an object _sourceEntries. This was a collection derived from _clientTemplate.Descendants("SourcEntries").Descendants("Field"). I would have thought that the two forms were essentially equivalent for my purposes, but apparently not. I'd appreciate somebody commenting on this issue.
Thanks folks!
Try changing:
where (string)sourceField.Attribute("n") == (string)clientField.Attribute("n")
To:
where sourceField.Attribute("n").Value == clientField.Attribute("n").Value