IContentRepository
Describes repository methods for Optimizely content, and how to load, list, and persist content instances.
EPiServer.IContentRepository is the primary API that defines repository methods for IContent objects. Through the repository, perform CRUD (Create, Read, Update, Delete) operations on content instances implementing EPiServer.Core.IContent, such as listing and moving.
Get an instance of any interface using dependency injection (for example, by constructor injection). Alternatively, get a content instance by calling the inversion of control (IoC) container directly:
var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
For read-only access to content, use the IContentLoader interface instead:
var loader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
NoteAvoid using the service locator. Use dependency injection instead to get instances of
IContentRepositoryorIContentLoader.
The following examples show common operations for instances of content.
Load a content instance
The Get<T> method loads a single content instance, where T is IContentData. The T type parameter specifies the desired type. For example, if a type represents a page as follows:
[ContentType]
public class TextPage : PageData
{
public virtual string MainBody { get; set; }
}If the type of a given reference is known, load the page as follows:
TextPage page = loader.Get<TextPage>(contentReference);
If the content item cannot be assigned to the type given as a type argument, a TypeMismatchException occurs. To safely load an item of unknown type, use IContent as the type argument. (The constraint is IContentData, but at runtime, each instance loadable from the repository implements IContent.)
var content = loader.Get<IContent>(contentReference);
To load an item as a given type without generating an exception when the type does not match, use the following approach:
if (loader.TryGet<TextPage>(contentReference, out var page))
{
// use page
}Load several content instances simultaneously with the following calls.
IEnumerable<ContentReference> references = GetSomeReferences();
IEnumerable<IContent> items = loader.GetItems(
references,
new LoaderOptions { LanguageLoaderOption.FallbackWithMaster() });When loading several items where a type does not match, the result is filtered for assignable types to the type argument (and does not generate a TypeMismatchException).
Specify a language version of content by using an overload that takes a CultureInfo or a LanguageLoaderOption. If CultureInfo is not specified, content is retrieved in the same language that the current request specifies. See ContentLanguage.PreferredCulture. To consider the language fallback and replacement settings, use a LanguageLoaderOption with fallback enabled. The following example shows how to get the Swedish version of the page.
page = loader.Get<TextPage>(contentReference, CultureInfo.GetCultureInfo("sv"));
List children of a content instance
Optimizely Content Management System (CMS) stores content in a tree hierarchy. The following example shows how to get the children of a content instance:
IEnumerable<IContent> children = loader.GetChildren<IContent>(contentReference);
To get children that are pages of a content instance, use the following call:
IEnumerable<PageData> pages = loader.GetChildren<PageData>(contentReference);
To get children in a specific language, use overloads to GetChildren that take a LoaderOptions parameter.
Persist a content instance
The Save method persists an IContent instance. If the IContent instance implements IReadOnly, call CreateWritableClone before modifying the instance. The Save method has a SaveAction flag parameter that controls the action that occurs when the content is saved, such as publishing the page. The following example shows how to update a page property programmatically.
var writablePage = (TextPage)repository.Get<TextPage>(contentReference).CreateWritableClone();
writablePage.MainBody = "something";
repository.Save(writablePage, SaveAction.Publish);Additional repository operations
The IContentRepository interface includes additional methods for managing content:
Copy– Creates a copy of a content item under a specified parent.Publish– Publishes a content item without requiring a separate save operation.ConvertLanguageBranch– Creates or converts a content item into another language branch.
NoteThe
Copymethod no longer publishes content automatically. Explicitly callPublishor useSaveAction.Publishafter copying.
Updated 7 days ago
