Umbraco: How to get nodeID by node name by programatically in c#.

Scenario: We come in situation during developing to get node Id in content section programatically. For Instance you have created a package and it contains xyz node in content section and this package is getting installed at another umbraco at that time if you are adding or getting node property then you don’t know exactly how to get there property and set there value. In this situation we will start looking from root node in content section which id we all know  Root node id = -1  and stop until we find our node for which we are looking. For doing so you have to run below code.

First Reference: umbraco.NodeFactory;


// -1 is Node id for root.
Node node = new Node(-1);
int nodeId = 0;
int nodeTemplate = 0;
string nodeName ="xyz";
foreach (Node childNode in node.Children)
{
    var child = childNode;
    if (child.Name == nodeName)
      {
        nodeId = child.Id;
        nodeTemplate = child.template;
        break;
      }
}
Document doc = new Document(nodeId);

// First Tell umbraco to  refresh node cache for geting uncahed value.
umbraco.library.UpdateDocumentCache(doc.Id);

// Retrive content/node property value.
var HederTitle = Convert.ToString(doc.getProperty("HederTitle").Value);

How to Get All node by It’s document Type in umbraco by c#.

GetAllNodeByDocType(-1, “nishant”);

public void GetAllNodeByDocType (int nodeId string docTypeName)
{
       Node node = new Node(nodeId);
       foreach (Node childNode in node.Children)
       {

           var child = childNode;
           if (child.NodeTypeAlias == docTypeName)
              {
                 // Do something here like print node name.
              }
           if (child.Children.Count > 0)
             {
                    GetAllNodeByDocType(child.Id,docTypeName);
             }
       }
}

Another Umbraco Article

Page keyword: Get All node name in umbraco, How to retrieve all node name in umbraco by c#, How to set node property in umbraco, how to set documenttype property in umbraco.

About these ads

6 thoughts on “Umbraco: How to get nodeID by node name by programatically in c#.

  1. Pingback: Umbraco: How to Log or store error in umbraco Log Table by c#. | [Code Destination]

  2. Pingback: Umbraco: How to Create Custom data type in umbraco using c#. | [Code Destination]

  3. Pingback: Create your first Rss Feed Burner in umbraco | [Code Destination]

  4. Pingback: Install umbraco manually. | [Code Destination]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s