Domanda

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.

È stato utile?

Soluzione

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);
        }
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top