---
title: Program projects
slug: cms-12/docs/creating-a-project-programmatically
docTags: 
createdAt: 2026-08-10T15:56:01.000Z
---

This topic contains examples of the Projects feature.

:::hint{type="info"}
**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.

## Create a project

:::hint{type="info"}
**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.

```csharp
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.

```csharp
ProjectRepository.ProjectSaved += OnProjectSaved;
ProjectRepository.ProjectDeleted += OnProjectDeleted;
ProjectRepository.ProjectItemsSaved += OnProjectItemsSaved;
ProjectRepository.ProjectItemsDeleted += OnProjectItemsDeleted;
```

- Create project items.

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

## Access projects

- Get a specific project.

```csharp
Project project = projectRepository.Get(projectId);
```

- List projects.
  - List projects.

```csharp
IEnumerable<Project> projectList = projectRepository.List();
```

:::Paragraph{listStyleType="disc" indent="2"}
Or get a paged list.
:::

```csharp
IEnumerable<Project> projectList = projectRepository.List(0, 10, out totalCount);
```

- Get a specific project item.

```csharp
ProjectItem projectItem = projectRepository.GetItem(projectItemId);
```

- List project items
  - List project items.

```csharp
IEnumerable<ProjectItem> projectItems1 = projectRepository.ListItems(project.ID);
```

:::Paragraph{listStyleType="disc" indent="2"}
Or get a paged list.
:::

```csharp
IEnumerable<ProjectItem> projectItems2 = projectRepository.ListItems(project.ID, contentLanguage, 0, 10, out totalCount);
```

:::Paragraph{listStyleType="disc" indent="2"}
Or only projects of a specific category.
:::

```csharp
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.

```csharp
IEnumerable<ProjectItem> projectItems = projectRepository.Findtems(new ContentReference[] { projectItem1.ContentLink });
```

- To get the `projectIds` of the currently active projects, use the `IProjectResolver`.

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

## Update projects

- To change the name of a project.

```csharp
project.Name = updatedProjectName;
projectRepository.Save(project);
```

- To update or add project items, save the items.

```csharp
projectRepository.SaveItems(new ProjectItem[] {
  existingProjectItem,
  newProjectItem
});
```

## Delete projects

- To delete a project, call `Delete`.

```csharp
projectRepository.Delete(project.ID);
```

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

```csharp
projectRepository.DeleteItems(new int[] {
  projectItem.ID
});
```

- Delete project items for a project.

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

## Publish a project

- Get a `ProjectPublisher`.

```csharp
ProjectPublisher projectPublisher = ServiceLocator.Current.GetInstance<ProjectPublisher>();
```

- Publish a project.

```csharp
await projectPublisher.PublishAsync(project);
```

- To delay the publishing of a project, use the `DateTime` overload.

```csharp
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.

```csharp
await projectPublisher.PublishAsync(project, AccessLevel.NoAccess);
```

- To reactivate a project that was published.

```csharp
await projectPublisher.ReactivateAsync(project);
```

- You can publish a subset of a project by sending in a list of project items.

```csharp
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.

```csharp
IEnumerable<IContent> contentReferences = new [] {
  contentReference
};
if (projectRepository.GetItems(contentReferences).Any()) {}
```

- Check if a content item is in a project with any version.

```csharp
IEnumerable<IContent> contentReferences = new [] {
  contentReference.ToReferenceWithoutVersion()
};
if (projectRepository.GetItems(contentReferences).Any()) {}
```

- Check if a content item is part of a project.

```csharp
if (projectRepository.GetItems(new [] {
    contentReference
  }).Any(x => x.ProjectID == projectID)) {}
```

- To get the projects that are connected to a list of content.

```csharp
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.

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