2
votes

I have a branch template that inherits from a standard template. If I add renderings to the branch template and add to Sitecore using the menu functions (insert -> insert from template -> etc) the renderings, etc. appear as expected, i.e. exactly as they are in the branch template. Good as I expected.

The problems though arise when I try and add a branch template in code. Doing it this way seems to ignore any renderings in the branch template. The renderings shown are those in the standard template only. I've tried moving all the rendering data into the branch template but this results in no layout data being sent to the the newly created item. I create the item thus:

Item parent = SitecoreService.Database.GetItem(parentPath);
if (parent == null)
   throw new NullReferenceException("Cannot load item to act as parent from path: " + parentPath);

Item branchItem = SitecoreService.Database.GetItem(new ID(branchTemplateId));
branchItem.Fields.ReadAll();
//BranchItem branch = SitecoreService.Database.GetItem(new ID(branchTemplateId));
if (branchItem == null)
   throw new ApplicationException("Failed ot find template branch with the id:" + branchTemplateId);

Item itemFromBranch = parent.Add(itemName, new BranchItem(branchItem)); 

I've actually attempted several variation of the above (using implicit cast, adding and removing ReadAll from the Fields, etc.).

The rendering field is blank when created though code, when created though the menu it's "Raw" value is thus:

<r  xmlns:xsd="http://www.w3.org/2001/XMLSchema"><d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}" l="{9FCEC8D8-9676-4C12-A9E2-72E0EAD07B5B}"><r ds="{CE6EB528-B937-4B0F-9897-B26C4EF8AFEE}" id="{89E00DE8-774D-4584-8002-9C28E8099711}" par="" ph="header" uid="{9AAB1306-BF20-44E7-B83C-26DA7173DC9C}" /><r ds="{A046699C-5FDF-468F-8FDF-3E9FAB0A35D0}" id="{19DD096B-F95E-46C0-A290-12A4B04E910C}" par="" ph="header" uid="{EB1AEB53-E001-42B3-BDFD-7570817E3B59}" /><r id="{7DE1675E-B511-4879-9003-7AC46E83A07B}" ph="header" uid="{4CF3F325-258D-4749-9CE0-61B4675692E1}" /><r id="{A00BE211-B0E0-462A-B377-BEBFF753D250}" ph="header" uid="{EC988DBC-AE99-44A9-BE4A-153B9C02C9F7}" /><r ds="{5A6B1B00-6941-44BB-9418-07784826E5AF}" id="{784DE1DD-B3FB-4A2D-BF4D-7CF9DDEA8C49}" par="" ph="footer" uid="{948F0EC3-DB97-47E8-8C06-4FACC2FBF7DC}" /></d></r>

What am I missing here?


Tried Mareks' suggestion below, but appears to do the same:

Item parent = SitecoreService.Database.GetItem(parentPath);
if (parent == null)
    throw new NullReferenceException("Cannot load item to act as parent from path: " + parentPath);

BranchItem branchItem = SitecoreService.Database.GetItem(new ID(branchTemplateId));

if (branchItem == null)
     throw new ApplicationException("Failed ot find template branch with the id:" + branchTemplateId);


Item itemFromBranch = Context.Workflow.AddItem(itemName, branchItem, parent);
2

2 Answers

2
votes

UI command uses Workflow.AddItem method.

Try code below:

Item branchItem = (BranchItem)SitecoreService.Database.GetItem(new ID(branchTemplateId));
if (branchItem == null)
    throw new ApplicationException("Failed ot find template branch with the id:" + branchTemplateId);

Context.Workflow.AddItem(itemName, branchItem, parent);
1
votes

Try using the following

var branchpath = "your/branch/template/path";

using (new SecurityDisabler())
{
   var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");

   Item parentItem = masterDb.Items["path/to/parent/item/here"];

   BranchItem template = masterDb.GetItem(branchpath);

   parentItem.Add("YourItemName", template);

}

Thanks