Sitecore Create/Read/Update/Delete/Copy/Move/Rename item – the beginners guide

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:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, General .NET, Sitecore 5, Sitecore 6, Sitecore 7, Sitecore 8, Sitecore 9 and tagged , . Bookmark the permalink.

1 Response to Sitecore Create/Read/Update/Delete/Copy/Move/Rename item – the beginners guide

  1. Michael Mavrofrides says:

    Did I just capture a glitch in the matrix? Line 4 literally just had a span with a class for skimlinks-unlinked after the word “new”

    I screen shot it to send to you, then realized I couldn’t find your email address (either poor detective skills on my part or great stealth mode on yours). So I was then just going to leave this comment and copy paste the markup that looked like it got injected into your post, only to find it now looks correct. send me an email “mma at sitecore dot com” and I’ll send over the screenshot if you’re interested.

    Oh, and by the way…great post. I’ll share it with my new developers in class.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.