You often end up in a situation where you want alter or create an item from C# through the Sitecore API. This articles walks through the different possibilities provided by the Sitecore API for viewing content, altering and creating items in C#.
This is the first part of two articles. You can read the second part here: Programatically reading, altering and creating items - part 2.
Retrieving items
All items are accessed through the Sitecore.Data.Items.Item class. If you take a look at the class, you will see that it has a lot of members which you can use. I will only go through a few of them, but feel free to explore them yourself.
You are mostly going to get an instance of an item through the Sitecore.Data.Database class. This allows you to retrieve items normally by specifying a path or an ID. Like this:
//Get the master database through a Factory
Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");
//Retrieving an item with a path through the database
Sitecore.Data.Items.Item myItem = masterDb.Items["/sitecore/content/home"];
//Retrieving an item from an item id
Sitecore.Data.Items.Item mySecondItem = masterDb.Items[new ID("{BB07A18A-580D-4105-9A1D-F3072D1A0BE3}")];
Further there are a couple of other parameters you can pass to get the item. If you need to get the item in a specific language or version, you can pass this in as well; otherwise it will use the current context to determine these things.
//Retrieving an item in english and version number 2
Sitecore.Data.Items.Item myThirdItem = masterDb.Items["/sitecore/content/home", Sitecore.Globalization.Language.Predefined.English, Sitecore.Data.Version.Parse(2)];
You can also use the current context to retrieve the item. The context item is by standard determined by the URL. For instance when this URL is accessed www.mysite.com/sitecore/content/home.aspx, the context item is the /sitecore/content/home node.
//Retrieving an item through the context.
Sitecore.Data.Items.Item myForthItem = Sitecore.Context.Item;
There are other ways to get your item, but these are probably the most used.
Reading values from an Item
When you have retrieved the item, you want to start reading the content from it. This is mainly done by using the Fields property on the item. The different fields and field values are retrieved using a string, which indicates the name of the field you want to retrieve.
Depending on what you are trying to do, you can retrieve the value directly or declare the field first. Like this:
string fieldValue;
//Option 1: Getting the fieldvalue directly
fieldValue = Sitecore.Context.Item["MyField"];
//Option 2: Getting the field and then reading the value
Sitecore.Data.Fields.Field myField = Sitecore.Context.Item.Fields["MyField"];
if (myField != null)
fieldValue = myField.Value;
Option 1 is great for simple field types like the single-line field. It just gets the value or returns an empty string if the field doesn’t exist. This ensures that you don’t get any null reference exceptions.
Option 2 is great for more advanced field types, where you want perform other operations then just reading a value or if the field holds more information than a simple value such as the ImageField. For instance consider this example:
//Get a multilist field from the current item
Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["myMultilistFieldName"];
if (multilistField != null)
{
//Iterate over all the selected items by using the property TargetIDs
foreach (ID id in multilistField.TargetIDs)
{
Item targetItem = Sitecore.Context.Database.Items[id];
// Do something with the target items
// ...
}
}
Here you want to iterate over all items selected in a multilistfield. This is much easier, than reading the string value from the field, split the IDs and the get the items.
When you declare the field always remember to check for null. You don’t know if the item you are operating on is of a certain template or if the field exists.
Read the second part of the article here, where you learn to alter and create items through the API.