New Sitecorians often ask the most trivial questions, and I am happy to answer them. This question popped up lately: How do you perform basic CRUD operations on your Sitecore items? Well, it’s easy:
READ ITEM (GET ITEM):
// Get item from content database: Item item = Sitecore.Context.ContentDatabase.GetItem("/sitecore/content/home"); // Get item from ID: Item item = Sitecore.Context.ContentDatabase.GetItem(new Sitecore.Data.ID("{9464f2c9-8490-40e9-a95b-17f8a5128da6}"); // Get item from named database Item item = Sitecore.Configuration.Factory.GetDatabase("master").GetItem("/sitecore/content/home");
CREATE ITEM:
// You need to have a root item to create item under: Item item = Sitecore.Context.ContentDatabase.GetItem("/sitecore/content/home"); // You also need a template to create the item from: TemplateID template = new TemplateID(new ID("{434b38a2-b929-4a89-bbc8-a6b66281e014}")); // Then you can create a new item: Item newItem = item.Add("new item", template); // If you wish to create an item based on a branch: BranchId branch = new BranchId(new ID("{4f254169-7666-4c2e-8021-a05026d5a2e2}")); Item newItem = item.Add("new item", branch);
UPDATE ITEM:
// You need to have an item to update: // Remember to always update items in the MASTER database, // NOT the WEB Database: Item item = Factory.GetDatabase("master").GetItem("/sitecore/content/home"); // You then set the item in editing mode item.Editing.BeginEdit(); try { // Change the contents of the fields to update item.Fields["field"].Value = "new value"; // End edit writes the updates to the database: item.Editing.EndEdit(); } catch (Exception ex) { // in case of an exception, you do not really // need to cancel editing, but it is good // manners and it indicates that you know // what the code is doing item.Editing.CancelEdit(); }
DELETE ITEM:
// You need to have an item to delete: // Remember to always update items in the MASTER database, // NOT the WEB Database: Item item = Factory.GetDatabase("master").GetItem("/sitecore/content/home"); // Remember that deleting one item also delete the children. // The item.Recycle() moves the item to the recycle bin, whilst the // item.Delete() permanently deletes the item: item.Recycle();
COPY ITEM:
// You need to have a source destination: Item destinationItem = Factory.GetDatabase("master").GetItem("/sitecore/content/home"); // You also need an item to copy: Item sourceItem = Factory.GetDatabase("master").GetItem("/sitecore/content/sourceitem"); // Then you can copy: sourceItem.CopyTo(destinationItem, "new name");
MOVE ITEM:
// You need to have a source destination: Item destinationItem = Factory.GetDatabase("master").GetItem("/sitecore/content/home"); // You also need an item to move: Item sourceItem = Factory.GetDatabase("master").GetItem("/sitecore/content/sourceitem"); // Then you can copy: sourceItem.MoveTo(destinationItem);
RENAME ITEM:
// You need to have an item to rename: Item item = Factory.GetDatabase("master").GetItem("/sitecore/content/home"); item.Editing.BeginEdit(); item.Name = "new name"; item.Editing.EndEdit();
MORE TO READ:
- Deleting items to recycle bin in Sitecore by briancaos
- Create a new version of item, in different language from stackexchange