How to Add Item in Sitecore

2015/05/011 min read
bookmark this
Responsive image

Table of Contents

Introduction

This post provides a sample code snippet demonstrating how to programmatically add an item in Sitecore using C#.

Add Item in C# Code

using (new Sitecore.SecurityModel.SecurityDisabler())
                {
                    var database = Sitecore.Configuration.Factory.GetDatabase("Master or Web");
                    TemplateItem template = database.GetItem("Your Page Item's page template url");
                    Item item = database.GetItem("where do you want to add the item");
                    Item newItem = item.Add("New Item name", template);
                    newItem.Editing.BeginEdit();
                    addItem(newItem);
                    newItem.Editing.EndEdit();
                    return newItem;
                }

Conclusion

Using Sitecore's SecurityDisabler, you can programmatically add items to the Sitecore content tree by specifying the database, template, and parent item. Remember to wrap field edits within BeginEdit() and EndEdit() calls.