How can I create a project site using CSOM? I've checked everything I can call from PublishedProject and DraftProject but found nothing related to this. Is it at all possible? If yes, where can I locate the call?

I usually try to provide as much info on my posts as possible. However, I'm not sure what can I provide that might be helpful. APIs for the classes DraftProject and PublishedProject.

up vote 2 down vote accepted

Yes, you can. See the sample below. In this case I'm using PowerShell to create the new project via the Managed CSOM of Project Server:

$pwaUrl = "http://YourProjectSite/PWA/"
$timeoutSeconds = 1000
$projName = "NameOfYourProject"

# Name of the enterprise project template
$projType = "Enterprise Project"

# set the path according the location of the assemblies
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.ProjectServer.Client.dll" 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 

$projectContext = New-Object Microsoft.ProjectServer.Client.ProjectContext($pwaUrl) 

$projectTypes = $projectContext.EnterpriseProjectTypes
$projectContext.Load($projectTypes)
$projectContext.ExecuteQuery()

$entProjType = $projectTypes | ? { $_.Name -eq $projType }

Write-Host Creating project $projName
$projCreationInfo = New-Object Microsoft.ProjectServer.Client.ProjectCreationInformation
$projCreationInfo.Name = $projName
$projCreationInfo.EnterpriseProjectTypeId = $entProjType.Id

$projects = $projectContext.Projects
$newProj = $projects.Add($projCreationInfo)
$createJob = $projects.Update()
$createJobState = $projectContext.WaitForQueue($createJob, $timeoutSeconds);
Write-Host Create project job status: $createJobState

In C# you have to add references to the following assemblies in your project:

  • Microsoft.ProjectServer.Client
  • Microsoft.SharePoint.Client
  • Microsoft.SharePoint.Client.Runtime

then add these using statements:

using System.Linq;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;

then you can create a new project via this code:

    using (var projectContext = new ProjectContext("http://YourProjServer/PWA"))
    {
        var projTypeName = "Enterprise Project";
        var timeoutSeconds = 1000;
        var projName = "Name of your new project";

        var epts = projectContext.EnterpriseProjectTypes;
        projectContext.Load(epts, ept => ept.Include(pt => pt.Name, pt => pt.Id).Where(pt => pt.Name == projTypeName));
        projectContext.ExecuteQuery();

        var projectType = epts.FirstOrDefault();

        if (projectType != null)
        {
            var projCreationInfo = new ProjectCreationInformation
            {
                Name = projName,
                EnterpriseProjectTypeId = projectType.Id
            };

            var projects = projectContext.Projects;
            projects.Add(projCreationInfo);
            var createJob = projects.Update();
            var createJobState = projectContext.WaitForQueue(createJob, timeoutSeconds);
            Console.Write("Create project job status: {0}", createJobState);
        }
    }
  • Thanks mate! I need to check how to use PowerShell from the code because I have no idea how. In the meanwhile, do you know if it's possible to create them just using code? If it's not feasible, I'll try to use your solution. – Pedro Gordo Aug 8 '16 at 14:08
  • PowerShell is code either. Do you mean C#? – pholpar Aug 9 '16 at 6:41
  • Yes, I was referring to C#. – Pedro Gordo Aug 9 '16 at 8:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.