Access to projects

The following code demonstrates the various ways available to access the servers projects:

using erminas.SmartAPI.CMS.Project;
using erminas.SmartAPI.CMS.ServerManagement;
using erminas.SmartAPI.CMS.Utils;
using System;
using System.Net;

public class MyClass
{
  public void DoSomething()
  {
    var authData = new PasswordAuthentication("username", "password");
    var url = "http://mycmsserver/cms";

    var login = new ServerLogin(url, authData);
    using (var session = SessionBuilder.CreateOrReplaceOldestSession(login))
    {
      //this is the currently active project in the session
      var selectedProject = session.SelectedProject;

      var serverManager = session.ServerManager;

      //these are all projects, the active user is assigned to
      var currentUsersProjects = serverManager.Projects.ForCurrentUser;

      //these are all projects for the user xy, you need to be server manager to do this
      var user = serverManager.Users["some username"];
      var projectsOfUserXy = serverManager.Projects.ForUser(user.Guid);

      //these are all projects on the server, you need to be server manager to access projects you are not assigned to
      var allProjects = serverManager.Projects;

      //you can access a single project by name
      var myProject = serverManager.Projects.ForCurrentUser["myproject"];
      //this is the short for
      var myProjectToo = serverManager.Projects.ForCurrentUser.GetByName("myproject");

      //if you do not know wether a specific project exists, you can use
      IProject unknownProject;
      if (serverManager.Projects.ForCurrentUser.TryGetByName("myproject", out unknownProject))
      {
        Console.WriteLine("Found project with guid: " + unknownProject.Guid);
      } else {
        Console.WriteLine("No project with name myproject assigned to user");
      }

      //you can also get a project by guid
      var projectGuid = new Guid("...");
      var projectByGuid = serverManager.Projects.ForCurrentUser.GetByGuid(projectGuid);
      // a TryGetByGuid method is available, too

      //print all project names and guids
      Console.WriteLine("Projects:");
      foreach (var curProject in serverManager.Projects)
      {
        Console.WriteLine(curProject);
      }
    }
  }
}

Leave a comment