HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Program projects

Describes how to create, update, access and delete projects and project items programmatically in Optimizely Content Management System (CMS).

This topic contains examples of the Projects feature.



An enum, ProjectStatus, used by the "old" project gadget, is not documented here. For example, the List method has an overload that takes this parameter. You should not use ProjectStatus because it is being phased out.

Currently, there is no public API for programmatically creating comments and custom notifications on projects.

Create a project



The examples here use ServiceLocator.Current to access DI container. In "real" code, you should take in the dependencies through a constructor instead.

  • Create a project and save it.

    ProjectRepository projectRepository = ServiceLocator.Current.GetInstance<ProjectRepository>();
    Project project = new Project {
      Name = newProjectName
  • There are a couple of static events for saving and deleting projects and items that can be listened to.

    ProjectRepository.ProjectSaved += OnProjectSaved;
    ProjectRepository.ProjectDeleted += OnProjectDeleted;
    ProjectRepository.ProjectItemsSaved += OnProjectItemsSaved;
    ProjectRepository.ProjectItemsDeleted += OnProjectItemsDeleted;
  • Create project items.

    ProjectItem projectItem = new ProjectItem(project.ID, content);
    projectRepository.SaveItems(new ProjectItem[] {

Access projects

  • Get a specific project.

    Project project = projectRepository.Get(projectId);
  • List projects.

    • List projects.

      IEnumerable<Project> projectList = projectRepository.List();
    • Or get a paged list.

      IEnumerable<Project> projectList = projectRepository.List(0, 10, out totalCount);
  • Get a specific project item.

    ProjectItem projectItem = projectRepository.GetItem(projectItemId);
  • List project items

    • List project items.

      IEnumerable<ProjectItem> projectItems1 = projectRepository.ListItems(project.ID);
    • Or get a paged list.

      IEnumerable<ProjectItem> projectItems2 = projectRepository.ListItems(project.ID, contentLanguage, 0, 10, out totalCount);
    • Or only projects of a specific category.

      IEnumerable<ProjectItem> projectItems3 = projectRepository.ListItems(project.ID, category, contentLanguage, 0, 10, out totalCount);
  • You also can send in a list of content and get project items that match that list.

    IEnumerable<ProjectItem> projectItems = projectRepository.Findtems(new ContentReference[] { projectItem1.ContentLink });
  • To get the projectIds of the currently active projects, use the IProjectResolver.

    ProjectResolver projectResolver = ServiceLocator.Current.GetInstance<IProjectResolver>();
    IEnumerable<int> currentProjects = projectResolver.GetCurrentProjects();

Update projects

  • To change the name of a project.

    project.Name = updatedProjectName;
  • To update or add project items, save the items.

    projectRepository.SaveItems(new ProjectItem[] {

Delete projects

  • To delete a project, call Delete.

  • When you delete project item(s), send in the IDs of the items to DeleteItems.

    projectRepository.DeleteItems(new int[] {
  • Delete project items for a project.

    projectRepository.DeleteItems(projectRepository.GetItems(project.ID).Select(item => item.ID));

Publish a project

  • Get a ProjectPublisher.

    ProjectPublisher projectPublisher = ServiceLocator.Current.GetInstance<ProjectPublisher>();
  • Publish a project.

    await projectPublisher.PublishAsync(project);
  • To delay the publishing of a project, use the DateTime overload.

    await projectPublisher.PublishAsync(project,laterDateTime);
  • Publish a project without checking access rights; for example, in a scheduled job, use a method that takes an AccessLevel and send in NoAccess to skip the access check.

    await projectPublisher.PublishAsync(project, AccessLevel.NoAccess);
  • To reactivate a project that was published.

    await projectPublisher.ReactivateAsync(project);
  • You can publish a subset of a project by sending in a list of project items.

    await projectPublisher.PublishAsync(project, new [] {
    }, null, AccessLevel.NoAccess);

Work with projects and content

  • Check if a content item is in a project.

    IEnumerable<IContent> contentReferences = new [] {
    if (projectRepository.GetItems(contentReferences).Any()) {}
  • Check if a content item is in a project with any version.

    IEnumerable<IContent> contentReferences = new [] {
    if (projectRepository.GetItems(contentReferences).Any()) {}
  • Check if a content item is part of a project.

    if (projectRepository.GetItems(new [] {
      }).Any(x => x.ProjectID == projectID)) {}
  • To get the projects that are connected to a list of content.

    IEnumerable<Project> projects = projectRepository
      .GetItems(new [] {
      .Select(item => item.ProjectID)
      .Select(id => projectRepository.Get(id));
  • Use ProjectLoaderOptions to load content with versions found in projects rather than loading the published version.

    IContentLoader contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
    LoaderOptions loaderOptions = new LoaderOptions {
      new ProjectLoaderOption {
        ProjectIds = new [] {
    contentLoader.Get<IContent>(contentReference, loaderOptions);