2

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.

1 Answer 1

2

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);
        }
    }
3
  • 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. Aug 8, 2016 at 14:08
  • PowerShell is code either. Do you mean C#?
    – pholpar
    Aug 9, 2016 at 6:41
  • Yes, I was referring to C#. Aug 9, 2016 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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