What is your opinion on the following code, i have to find parent of a specific type. I Know that "Document" is deprecated, so what is the best way of doing this with "Content".
thanks..
Maanehunden
public static Document FindParentNodeByAlias(this Document current, string alias)
{
Document found = current;
while (found.Id != -1)
{
found = new Document(found.ParentId);
if (found.ContentType.Alias == alias)
{
break;
}
}
return found;
}
public static DynamicNode GetParentByNodeType(DynamicNode node, string parentNodeType) { DynamicNode parent = node; for (int i = 0; i < node.Level; i++) { if (parent.NodeTypeAlias == parentNodeType) { break; } parent = parent.Parent; } return parent; }
I hope that helps. Also about getting a parent I have this ones in my helper:
public static Document GetParentByLevel(Document node, int level) { CMSNode p = new CMSNode(node.Id); for (int i = 0; i < node.Level; i++) { if (p.Level == level) { break; } p = p.Parent; } return new Document(p.Id); } public static List<string> GetAllParentToLevel(DynamicNode node, int level) { DynamicNode parent = node; List<string> parents = new List<string>(); for (int i = 0; i < node.Level; i++) { if (parent.Level == level) { break; } parents.Add(parent.Id.ToString()); parent = parent.Parent; } return parents; } You can use either Document or DynamicNodes.
I am working on a custom datatype for the backoffice, so i can't use Model.Conten ): the Node is not populated so i am forced to use Document, because it works.
Finding Parent Node By Alias using Umbraco 6 ?
Hi All
What is your opinion on the following code, i have to find parent of a specific type. I Know that "Document" is deprecated, so what is the best way of doing this with "Content".
thanks.. Maanehunden
Hi, I usually use this method to find it:
public static DynamicNode GetParentByNodeType(DynamicNode node, string parentNodeType) { DynamicNode parent = node; for (int i = 0; i < node.Level; i++) { if (parent.NodeTypeAlias == parentNodeType) { break; } parent = parent.Parent; } return parent; }I hope that helps. Also about getting a parent I have this ones in my helper:
public static Document GetParentByLevel(Document node, int level) { CMSNode p = new CMSNode(node.Id); for (int i = 0; i < node.Level; i++) { if (p.Level == level) { break; } p = p.Parent; } return new Document(p.Id); } public static List<string> GetAllParentToLevel(DynamicNode node, int level) { DynamicNode parent = node; List<string> parents = new List<string>(); for (int i = 0; i < node.Level; i++) { if (parent.Level == level) { break; } parents.Add(parent.Id.ToString()); parent = parent.Parent; } return parents; } You can use either Document or DynamicNodes.The document api was meant for creating/updating content. If you are using v6 have a look at the contentservice API (http://our.umbraco.org/documentation/reference/Management-v6/Services/ContentService)
For querying content to use on the frontend you should use the Node API (v4) or TypedContent(V6). As a alternative you could use uQuery.
You can find some documentation here : http://our.umbraco.org/documentation/Reference/Querying/uQuery/Content/Nodes (v4 and v6)
http://our.umbraco.org/documentation/reference/Mvc/querying (v6 only)
So you would be looking for something like this :
This would result in the first ancestor node with node type alias "home"
Dave
Thanx for your replies Dave and João.
uQuery is clearly the way (c:
/Maanehunden
uQuery is not the way! uQuery doesn't use the new library that was introduced in umbraco 6. It uses the old libraries from 4.
@maanehunden - where are you trying to use this method, a view? if so, you should be able to use:
Model.Content.AncestorOrSelf("yourAliasHere")
Tnx for the reply Andrew.
I am working on a custom datatype for the backoffice, so i can't use Model.Conten ): the Node is not populated so i am forced to use Document, because it works.
any other suggestions ?
Document is now known in the umb6 API as "Content":
http://our.umbraco.org/documentation/reference/Management-v6/Models/Content
although, that page says not to use it for "complex queries" as it might not be fast enough, so I'm not sure what the Umbraco team suggest instead :(
is working on a reply...