London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Creating a project

  • 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 ProjectResolver.
    ProjectResolver projectResolver = ServiceLocator.Current.GetInstance<ProjectResolver>();
    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 has been published.
    await projectPublisher.ReactivateAsync(project);
  • There is a possibility to 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.FindItems(contentReferences).Any()) { }
  • Check if a content item is in a project with any version.
    IEnumerable<IContent> contentReferences = new[] { contentReference. ToReferenceWithoutVersion() };
    if (projectRepository.FindItems(contentReferences).Any()) { }
  • Check if a content item is part of a project.
    if (projectRepository.FindItems(new[] { contentReference }).Any(x => x.ProjectID == projectID)) { }
  • To get the projects that are connected to a list of content.
    IEnumerable<Project> projects = projectRepository
      .FindItems(new[] { contentReference })
      .Select(item => item.ProjectID)
      .Distinct()
      .Select(id => projectRepository.Get(id));
  • Use ProjectLoaderOptions to only load content contained in a list of projects.
    IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
    LoaderOptions loaderOptions = new LoaderOptions { new ProjectLoaderOption { ProjectIds = new[] { projectID } } };
    contentRepository.Get(contentReference1, loaderOptions);

Last updated: Nov 02, 2015