3

I found an article showing how to create an item using a branch. https://briancaos.wordpress.com/2009/12/01/creating-items-from-branches-in-sitecore/

I found another article showing how to create an image and being able to specify the new item's ID. http://mixedsitecore.blogspot.com/2014/08/creating-new-item-with-specific-id.html

I can't find how to create a new item with a specific ID and a branch. Can I assign a branch after creating the item? Can I change an item's ID after it's created?

  • is there a reason why you need a specific ID? if not you can use this example on creating items based on the item template stackoverflow.com/questions/35776380/… – Diego Mar 6 '17 at 20:18
  • Did you try ItemManager.CreateItem with a branchID (as templateID)? Didn't try myself, but might be worth a shot.. – Gatogordo Mar 6 '17 at 20:34
  • I've tried as best I can to answer your specific questions. But I can't help but feel, if you would share with us the root problem you're trying to solve, we would be able to offer you a better solution. Creating items with specific IDs is a technique only rarely used in day-to-day code. Actually, the only case I can think of for this is during publishing. If you wish - please raise additional new questions (don't edit this one). – Mark Cassidy Mar 6 '17 at 22:40
  • I'm migrating a site from Sitecore 6.6 to a fresh install of Sitecore 8.2. Since much of my content has links to other Sitecore content it makes sense to keep the same IDs instead of having to fix all of the links. I'm not sure that I MUST create an item with the branch instead of just the template, but I want to. – Kevin Mar 8 '17 at 12:15
  • @Gatogordo Yes, I did try that. No bueno. – Kevin Mar 8 '17 at 12:16
3

If what you are looking to do is create a new item with a specific ID from a specific branch template then what you are looking for is the following:

var newItemName = "Name of Root Item to Create" // this may be overridden by your branch template depending on how you set it up
var branchId = ... // ID of your branch template
var parentItem = ... // parent item under which the branch should be created as a child
var newItemId = ... // ID of the item to create 

ItemManager.AddFromTemplate(newItemName, branchId, parentItem, newItemId);

The above call to the static Sitecore.Data.Managers.ItemManager.AddFromTemplate(string, ID, Item, ID) method will allow you to create an item with a specific ID and branch.

Note that the non-static methods Sitecore.Data.Items.Item.Add(string, BranchItem) and Sitecore.Data.Items.Item.Add(string, BranchId) both call the static method, above.

| improve this answer | |
3

What you're asking for, doesn't entirely make sense. At least not to me. But let's start with the questions:

The questions

Can I create a new Item with a specific ID and branch?

Well you can create a new Item with a specific ID, sure. It will blow up if the ID you're selecting already exists however. It looks like this:

parent.Add("myNewItem", templateID, newItemId);

You could theoretically add information on this newly created Item to indicate a branch, but it would mean nothing at this point. Branches only exist and have an effect when used to create items - not after the fact.

There is no way to specify a Branch Template Id instead of a Template Id in Item creation API. The reason (probably) is; a Branch Template results in not 1 Item but 1..n items.

Can I assign a branch after creating the item?

Yes, but it would have no effect.

parent.Fields[FieldIDs.Branches].Value = branchId.ToString();

All it would do, is show "created from [your branch]" in the Content Editor. It would have no other effect.

Can I change an Item ID after it is created?

No. Not using official APIs. On a database level, yes, but I strongly advise you not to go there. Doing so would not be supported by Sitecore either.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.