Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
Applies to versions: 10 and higher
Other versions:
ARCHIVED This content is retired and no longer maintained. See the version selector for other versions of this topic.

Working with projects programmatically

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

This topic explains how to create, update, access and delete projects and project items programmatically in Optimizely CMS.

Examples are written for the Projects feature (aka Project Mode).

Note: 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.

Creating a project

Note: 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 };
    projectRepository.Save(project);
    
  • There are a couple of static events for saving and deleting project 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[] { projectItem });
    

Accessing projects

  • Get a specific project.
    Project project = projectRepository.Get(projectId);
  • List projects
    • List all 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 all 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 all project items that matches 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();

Updating

  • To change the name of a project.
    project.Name = updatedProjectName;
    projectRepository.Save(project);
  • To update existing project items and/or adding new, just save the changed/new items.
    projectRepository.SaveItems(new ProjectItem[] { existingProjectItem, newProjectItem });

Deleting

  • To delete a project, call Delete.
    projectRepository.Delete(project.ID);
  • When you delete project item(s), send in the IDs of the items to DeleteItems.
    projectRepository.DeleteItems(new int[] { projectItem.ID });
  • Delete all project items for a project.
    projectRepository.DeleteItems(projectRepository.GetItems(project.ID).Select(item => item.ID));

Publishing 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[] { projectItem1, projectItem3, projectItem4 }, null, AccessLevel.NoAccess);

Working with projects and content

  • Check if a content item is in a project.
    IEnumerable<IContent> contentReferences = new[] { contentReference };
    if (projectRepository.GetItems(contentReferences).Any()) { }
  • Check if a content item is in a project with any version.
    IEnumerable<IContent> contentReferences = new[] { contentReference. ToReferenceWithoutVersion() };
    if (projectRepository.GetItems(contentReferences).Any()) { }
  • Check if a content item is part of a project.
    if (projectRepository.GetItems(new[] { contentReference }).Any(x => x.ProjectID == projectID)) { }
  • To get the projects that are connected to a list of content.
    IEnumerable<Project> projects = projectRepository
      .GetItems(new[] { contentReference })
      .Select(item => item.ProjectID)
      .Distinct()
      .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[] { projectID } } };
    contentLoader.Get<IContent>(contentReference, loaderOptions);
Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 02, 2021

Recommended reading