Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

The main API to work with content is the interface EPiServer.IContentRepository. The interface instance is exposed through the Locate property that is exposed on many base classes (for example, PageBase, UserControlBase, PropertyData). You can also get it from IOC container through call:

C#
var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();

For those who prefer to work with a concrete class the corresponding class is EPiServer.DataFactory which has a property Instance that gives access to the singleton instance of the repository.

Through the repository you could do CRUD Create, Read, Update, Delete) operations as well as other operations like listings, move etc. on content instances (that is instances implementing EPiServer.Core.IContent). Below are examples of some of the most common operations.

Loading an instance

The method to load a single content instance is to call the method: Get<T> where T : IContentData. The type parameter T specifies which type you want. So if there is a type representing a page as:

C#
[ContentType]
public class TextPage : PageData
{
    public virtual string MainBody { get; set; }
}

and you know which type a given reference refers to then you could load the page as:

C#
TextPage page = Locate.ContentRepository().Get<TextPage>(pageLink);

In case the content item is not assignable to the type given as type argument a TypeMismatchException will be thrown. If you do not know the type of an item you can safely load it with IContent as type argument (the constraint is IContentData but in runtime each instance loadable from repository will implement IContent):

C#
var content = Locate.ContentRepository().Get<IContent>(pageLink);

And if you want to load it as a given type but do not want an exception to be thrown in case type is not correct you could load it as:

C#
TextPage page = Locate.ContentRepository().Get<IContent>(pageLink) as TextPage;

It is also possible to load several content instances at the same time by calling. When loading several items TypeMismatchException is not thrown if a type is not matching. Instead the result is filtered for types that are assignable to type argument:

C#
IEnumerable<ContentReference> references = GetSomeReferences();
IEnumerable<IContent> items = Locate.ContentRepository().GetItems(references, LanguageSelector.AutoDetect());

You can specify which language version of a content you want by using overload that takes an ILanguageSelector. If no language selector is specified the content will be retrieved in the same language that the current request specifies (see ContentLanguage.PreferredCulture). Below is an example on how to get the Swedish version of the page.

C#
page = Locate.ContentRepository().Get<TextPage>(pageLink, new LanguageSelector("sv"));

Listing children

Content in CMS are stored in a tree hierarchy. You can get the children to a content instance as:

C#
IEnumerable<IContent> children = Locate.ContentRepository().GetChildren<IContent>(pageLink);

And if you want to get all children that are pages to a content instance you would call like:

C#
IEnumerable<PageData> pages = Locate.ContentRepository().GetChildren<PageData>(pageLink);

There are overloads to GetChildren that takes an ILanguageSelector if you want the children in a specific language.

Persisting an instance

To persist an IContent instance you call the Save method. In case the IContent instance implements IReadOnly you need to call CreateWritableClone before you can modify the instance. The save method have a SaveAction flag parameter that controls which action that should be done during save for example if the page should be published. Below is an example how to programmatically update a property of a page.

C#
var writablePage = Locate.ContentRepository().Get<TextPage>(pageLink).CreateWritableClone() as TextPage;
writablePage.MainBody = "something";
Locate.ContentRepository().Save(writablePage, SaveAction.Publish);
Do you find this information helpful? Please log in to provide feedback.

Last updated: Apr 02, 2014

Recommended reading