Creating Items from branch template in Sitecore Programmatically

Branch Templates are mainly used to build the reusable collection of items, or the hierarchy of items. Branch templates are very useful in creating the same structure at any path in Sitecore. In this article, let’s look at Creating Items from branch template in Sitecore Programmatically.

Creating Items from branch template in Sitecore Programmatically

If we are automating the content for Multi-site or Localizing the site then we can make use of branch template and feed the content to Sitecore.

For Eg: Let’s say we have created a Product branch template and we need to create products under Sitecore home node.

Below is the C# code to create items using the branch templates in Sitecore.

using(new Sitecore.SecurityModel.SecurityDisabler()) {
  string branchItem = "/sitecore/templates/Branches/Product";
  BranchItem branch = Sitecore.Context.Database.GetItem(branchItem);
  Assert.IsNotNull(branch, "Could not find Data branch at " + branchItem);
  Item homeItem = Sitecore.Context.Database.GetItem("/sitecore/content/home");
  homeItem.Add("product-1", branch);

}

This will add product-1 item under the home node and now we can fetch the product-1 item and edit the content accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like