Level: 100
 

Programmatically reading, altering and creating items - Part 1

c# code

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.

Written by: Jens Mikkelsen
Fri, Jun 26 2009

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.

 

Please rate this article


16 rates / 4,38 avg.

  • About the author:

    Jens Mikkelsen

    Jens Mikkelsen is a partner at Inmento Solutions a Sitecore consulting firm. He works as a Sitecore specialist and consulting helping clients architect and build quality Sitecore solutions using the newest modules and tools. 

    Further he has been deeply envolved in various complex solutions and has built up a strong knowledge of Sitecore architecture and best practices. He has especially focused on and is specialized in debugging and analyzing Sitecore solutions.

     

    Jens is very interested in the technical mechanisms in the new marketing products such as Sitecore DMS and Sitecore ECM.

    My Sitecore Freelance CV

5 responses to "Programmatically reading, altering and creating items - Part 1"

Sam
i Have a treelist field in my website and i want to retrieve part of content from all folder under the main folder. Actually i want to pull certain fields from all the items in all folders. Any help , i am new to Sitecore.
Posted: Sunday, March 24, 2013 8:16 PM
I am not sure that your question is clear enough for us to help you...

Mortaza
Posted: Monday, April 15, 2013 9:16 PM
Is it possible to get a list of the dll's needed to do this development? Obviously i'm a newbie.
Posted: Tuesday, April 14, 2015 12:42 PM
As a beginner, so much helpful article and save me a lot time.

Thank you.
Posted: Wednesday, March 09, 2016 11:02 AM
test
Posted: Wednesday, March 30, 2016 11:22 PM

Leave a reply


Notify me of follow-up comments via email.
 
 
#nbsp;