Creating items from branches in Sitecore

The branch concept in Sitecore is a method for creating multiple items at once. A branch consists of a branch item and below that item is several items. All of these items will be created when adding the branch item.

To do this in C# code is very simple:

using Sitecore.Data.Items;
using Sitecore.Diagnostics;

private const string _BRANCH_ITEM = "/sitecore/templates/Branches/MyBranches/MyBranch";

public void Create(Item parent, string name)
{
  BranchItem branch = globalDataFolder.Database.GetItem(_BRANCH_ITEM);
  Assert.IsNotNull(branch, "Could not find Data branch at " + _BRANCH_ITEM);
  parent.Add(name, branch);
}

The BranchItem class wraps the Sitecore item as a branch item. And the standard Add function on an item will take a branch item as parameter, creating the branch as subitems below the item.

As you can see there really is no difference from creating items based on branches compared to items created on templates, as the Add function takes both a TemplateItem and a BranchItem as parameter.

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 6 and tagged , . Bookmark the permalink.