This content is archived. See latest version here

Table of Contents


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 (e.g. PageBase, UserControlBase, PropertyData). You can also get it from IOC container through this call:

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:

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:

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

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 the type is not correct, you can load it as:

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

You can load several content instances at the same time as shown below.

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

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.

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.

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:

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:

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.

var writablePage = Locate.ContentRepository().Get<TextPage>(pageLink).CreateWritableClone() as TextPage;
writablePage.MainBody = "something";
Locate.ContentRepository().Save(writablePage, SaveAction.Publish);

Last updated: Mar 25, 2013