your link to better business solutions

Importing and Updating Data with Sitecore

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).

Creating Items

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:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase(“master”);

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:

Sitecore.Data.Items.Item parent = master.GetItem(“/sitecore/content/home”);

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):

Sitecore.Data.Items.TemplateItem template = master.Templates["Sample/Sample Item"];

Then you can add the new item named child under the existing parent:

Sitecore.Data.Items.Item item = parent.Add(“child”, template);

If the context user does not have access rights to create items, you must impersonate another user or disable Sitecore security constraints.

For example:

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase(“master”);

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)

{

using (new Sitecore.SecurityModel.SecurityDisabler())

{

Sitecore.Data.Items.Item item = parent.Add(“child”, template);

}

}

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.Database master = Sitecore.Configuration.Factory.GetDatabase(“master”);

Sitecore.Data.Items.Item item = master.GetItem(“/sitecore/content/home/child”);

if(item!=null)
{

using (new Sitecore.SecurityModel.SecurityDisabler())
{
item.Editing.BeginEdit();
try
{

if(item.Fields["title"] != null)
{

item.Fields["title"].Value = item.Name;
item.Fields["text"].Value = item.Name;

}

}
catch(Exception ex)
{

item.Editing.CancelEdit();
throw(ex);

}

item.Editing.EndEdit();
}

}

Field Types

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["checkboxfield"].Value = “1″;
item.Fields["datetimefield"].Value = Sitecore.DateUtil.ToIsoDate(DateTime.Now);
item.Fields["droplinkfield"].Value =item.ID.ToString();

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:

Sitecore.Data.Items.MediaItem media = item.Database.GetItem(“/sitecore/media library/files/file”);

if (media != null)
{

Sitecore.Data.Fields.FileField file =item.Fields["filefield"];
file.MediaID = media.ID;
file.Src =Sitecore.Resources.Media.MediaManager.GetMediaUrl(media);

}

The logic to set an Image field value is almost the same, except an image has additional properties:

Sitecore.Data.Items.MediaItem image = item.Database. GetItem(“/sitecore/media library/images/image”);

if (image != null)
{

Sitecore.Data.Fields.ImageField imagefield =
item.Fields["imagefield"];
imagefield.Alt = image.Alt;
imagefield.MediaID = image.ID;
imagefield.Src =
Sitecore.Resources.Media.MediaManager.GetMediaUrl(image);

}

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:

Sitecore.Data.Fields.MultilistField multilistfield = item.Fields["multilistfield"];

multilistfield.Add(item.ID.ToString());

if (!multilistfield.Contains(item.Parent.ID.ToString()))
{

multilistfield.Add(item.Parent.ID.ToString());

}

if (multilistfield.Contains(item.ID.ToString()))
{

multilistfield.Remove(item.ID.ToString());

}

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

2 Responses »

  1. THANK YOU! THANK YOU! THANK YOU!

    I looked through no less than 3 dozen pages looking for the super simple answer you provided for accessing the Image Field Values.

    Every stinking think I found was using the <sc image field.

    Thank you again!


    Ellis Benus
    Small Business Web Guru
    Wordpress Expert
    eb@ellisbenus.com
    http://EllisBenus.com/
    (573) 557-WEB0 (9320)

Trackbacks

  1. CMS Squad » Blog Archive » Importing and Updating Data with Sitecore

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.