This little helper class is something we use internally. If like
us, you sometimes still like to use usercontrols when creating
sites and want to have a nice easy way to grab nodes like you can
in Razor - then this should be right up your street (You could in
fact use this in your Razor files too)
Features
You now have access to all of the below. These all return
'DynamicNodeList' apart from GetRootNode(), GetCurrentNode() &
GetNodeByUrl() which return 'DynamicNode'.
public static RazorLibraryCore Library
public static DynamicNode GetRootNode()
public static DynamicNodeList GetNodesByDocTypeAlias(string docTypeAlias, int? startNodeId)
public static DynamicNode GetCurrentNode()
public static DynamicNodeList GetChildNodes(int? parentNodeId, bool getGrandChildrenToo = false)
public static DynamicNodeList GetNodesByName(string nodeName, int? startNodeId)
public static DynamicNodeList GetNodesFromCsv(string csv)
public static DynamicNodeList GetNodesByPropertyValue(string propertyName, string propertyValue, int? startNodeId)
public static DynamicNodeList GetNodesByXpath(string xpath)
public static DynamicNode GetNodeByUrl(string url)
Important
If you are keen on performance, then we strongly suggest you
always try to pass in a 'startNodeId' when its an option to try and
filter down the amount of nodes it has to go through. If you don't
then the method will work out the root of your site and query over
the entire XML, and just like with XSLT this can start to take a
few seconds if you have 10K+ nodes.
nQuery.GetRootNode()
Description: This helper tries to get your home
page, it does not get the actual root (-1). If you want to get the
root node just use the NodeById() Library method.
Example:
var node = nQuery.GetRootNode();
node.Name;
nQuery.GetNodesByDocTypeAlias(string docTypeAlias, int?
startNodeId)
Description: Returns all nodes with a specific
doctype, either pass in null to find all from your home page down -
Or pass in a start/parent node id.
Example:
var sb = new StringBuilder();
var nodes = nQuery.GetNodesByDocTypeAlias("MyDocTypeAlias", null);
foreach (DynamicNode node in nodes)
{
sb.AppendFormat("<p>{0}</p>", node.Name);
}
Response.Write(sb.ToString());
nQuery.GetCurrentNode()
Description: Says what it does on the tin
;)
Example:
var node = nQuery.GetCurrentNode();
node.Name;
nQuery.GetNodeByUrl(string url)
Description: Gets the node from a specified
url
Example:
var node = nQuery.GetNodeByUrl("/my-cool/url/");
node.Name;
nQuery.GetChildNodes(int? parentNodeId, bool
getGrandChildrenToo = false)
Description: Gets child nodes from either a
parent node or uses current page (Pass in null if no parentNodeId
needed), getGrandChildrenToo Optional: pass in true to get all
child nodes regardless of level
Example:
var sb = new StringBuilder();
var nodes = nQuery.GetChildNodes(null, true);
foreach (DynamicNode node in nodes)
{
sb.AppendFormat("<p>{0}</p>", node.Name);
}
Response.Write(sb.ToString());
nQuery.GetNodesByName(string nodeName, int? startNodeId)
Description: Find names with a specific
nodename, it does part match so you can put in the first few
letters and it will find any that contains them. startNodeId
Optional: Starting node id, will start from root if not
supplied
Example:
var sb = new StringBuilder();
var nodes = nQuery.GetNodesByName("Contact", null);
foreach (DynamicNode node in nodes)
{
sb.AppendFormat("<p>{0}</p>", node.Name);
}
Response.Write(sb.ToString());
nQuery.GetNodesFromCsv(string csv)
Description: Pretty self explanatory, just pass
in a valid CSV of node ids and get them back as DynamicNodes
Example:
const string csv = "1084,1085,1078,1156";
var sb = new StringBuilder();
var nodes = nQuery.GetNodesFromCsv(csv);
foreach (DynamicNode node in nodes)
{
sb.AppendFormat("<p>{0}</p>", node.Name);
}
Response.Write(sb.ToString());
nQuery.GetNodesByPropertyValue(string propertyName, string
propertyValue, int? startNodeId)
Description: Searches all nodes by a property
value, either starting at root or passed in start/parent node id.
Examples of use would be getting a node by a string in bodyText or
by another string value - Its also case insensitive too.
Example:
var sb = new StringBuilder();
var nodes = nQuery.GetNodesByPropertyValue("bodyText", "the", null);
foreach (DynamicNode node in nodes)
{
sb.AppendFormat("<p>{0}</p>", node.Name);
}
Response.Write(sb.ToString());
nQuery.GetNodesByXpath(string xpath)
Description: Pretty self explanatory again,
just pass in XPATH and get nodes returned
Example:
var sb = new StringBuilder();
var nodes = nQuery.GetNodesByXpath("//*");
foreach (DynamicNode node in nodes)
{
sb.AppendFormat("<p>{0}</p>", node.Name);
}
Response.Write(sb.ToString());
Also…
You have access to the Library helper too that's used in the
Umbraco Razor files, so you can use the Get Media and Get Node
methods like you do in a razor file.
nQuery.Library.MediaById()
Description: This is the standard get media
call, just exposed so you can use it in your usercontrols.
Example:
var media = nQuery.Library.MediaById("YourMediaId");
var mediaUrl = media.umbracoFile;
nQuery.Library.NodeById()
Description: Again, this is just the standard
dynamic get node by id call exposed so you can use it in your
usercontrols.
Example:
var dNode = nQuery.Library.NodeById("YourNodeId");
var myPropertyNameValue = dNode.MyPropertyName;
This helper class just wraps up the 'DynamicNode' class and
'RazorLibraryCore' class to give you some time saving helpers, hope
its useful to everyone else - We'd love to hear some feedback
below.
Use It
All you need to do is download the DLL below, unzip it and dump
it in your bin folder.