This resource provides an introduction for .NET developers who need to create items and update field values in a Sitecore Web Content Management repository. This resource assumes the reader is familiar with various concepts fundamental to Sitecore, such as data templates and fields. For additional information about the APIs used, see Sitecore API documentation (http://sdn.sitecore.net/Reference/Sitecore%206/Sitecore_6_API_Reference.aspx) on the Sitecore Developer Network (http://sdn.sitecore.net).
In order to create an item, you must:
- Implement an ASP.NET component such as a layout, sublayout, or Web control to invoke the appropriate Sitecore APIs.
- Determine the database in which to create the item.
- Locate the parent item.
- Locate the data template for the new item.
- Determine a name for the new item.
- Address security.
Note that both the parent item and the data template for the new item must exist before you can create the new item.
You almost always want to create items in the Master database and publish them to one or more publishing target databases. So you can generally reference the Master database explicitly:
Once you have the database, you can retrieve the parent of the new item either by using its path or by using its ID. For example, to retrieve the item that by default represents the home page:
You can retrieve a data template from the database using the ID of the data template or its path relative to /Sitecore/Templates. For example, to retrieve the Sample Item data template in the Sample folder (/Sitecore/Templates/Sample/Sample Item):
Then you can add the new item named child under the existing parent:
If the context user does not have access rights to create items, you must impersonate another user or disable Sitecore security constraints.
Sitecore.Data.Items.Item parent = master.GetItem(“/sitecore/content/home”);
Sitecore.Data.Items.TemplateItem template = master.Templates["Sample/Sample Item"];
if (parent!=null && template!=null)
Setting Field Values
Excluding the binary component of media items, all field values consist of text strings. You can set these fields easily, but your code may raise an exception if you do not first place the item in an editing state, confirm that the item contains the field, and ensure that the context user has the write access rights to the item and field. You may want to impersonate another user or disable security before placing the item in an editing state or you will raise an exception. You should also consider errors that could occur during sequential updates to multiple field values, and either commit or cancel changes changes due to any exception. For example, to set the value of the title and text fields in the item created in the previous example, rolling back in case of exception:
Sitecore.Data.Items.Item item = master.GetItem(“/sitecore/content/home/child”);
item.Fields["text"].Value = item.Name;
If you know how the field formats data, you can set any field to a text value as demonstrated previously For example, you can store a 1 to select a Checkbox field, or populate a Date or Datetime field with a date in the yyyyMMddTHHmmss format, or store the GUID of an item in a field that references another item (Droplink, Droptree, or Grouped Droplink):
item.Fields["datetimefield"].Value = Sitecore.DateUtil.ToIsoDate(DateTime.Now);
Sitecore provides a number of classes to simplify changes to more complex fields. For example, to set a Checkbox field:
Sitecore.Data.Fields.CheckboxField check =item.Fields["checkboxfield"];
check.Checked = true;
To set the value of a File field, you must first locate the media item in the database that contains the content item. For example:
if (media != null)
file.MediaID = media.ID;
The logic to set an Image field value is almost the same, except an image has additional properties:
if (image != null)
imagefield.Alt = image.Alt;
imagefield.MediaID = image.ID;
Processing fields that contain the GUIDs of multiple other items, such as Checklist, Multilist, Treelist, and TreelistEx, is slightly more complicated. Use the same class (Sitecore.Data.Fields.MultilistField) for all of these field types: