Program projects
Describes how to create, update, access and delete projects and project items programmatically in Optimizely Content Management System (CMS).
Examples for the Projects feature.
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 useProjectStatus
because it is being phased out.
There is no public API for programmatically creating comments and custom notifications on projects.
Create 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 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[] { projectItem });
Access 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 match that list.
IEnumerable<ProjectItem> projectItems = projectRepository.Findtems(new ContentReference[] { projectItem1.ContentLink });
-
To get the
projectIds
of the active projects, use theIProjectResolver
.ProjectResolver projectResolver = ServiceLocator.Current.GetInstance<IProjectResolver>(); IEnumerable<int> currentProjects = projectResolver.GetCurrentProjects();
Update a project
-
To change the name of a project.
project.Name = updatedProjectName; projectRepository.Save(project);
-
To update or add project items, save the changed or new items.
projectRepository.SaveItems(new ProjectItem[] { existingProjectItem, newProjectItem });
Delete a project
-
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));
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 inNoAccess
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);
Work 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);
Updated 6 months ago