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.

Advertisements

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.

3 Responses to Creating items from branches in Sitecore

  1. Pingback: Removing dead masters from Sitecore « Brian Pedersen’s Sitecore and .NET Blog

  2. Pingback: Create and publish items in Sitecore « Brian Pedersen’s Sitecore and .NET Blog

  3. Pingback: Sitecore 7 create item and update index at the same time | Brian Pedersen's Sitecore and .NET Blog

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