none
Create project site with CSOM RRS feed

  • Question

  • 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, which object has the call?

    I know that we can do it through PowerShell, but I want (if possible) to do it just using code.

    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 in this case.

    Monday, August 8, 2016 2:29 PM

Answers

  • Hi Pedro,

    I've answered the same question recently on SharePoint.StackExchange. Assuming you mean C# on "code".

    First of all, you have to add references to the following assemblies in your project:

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

    Next, add these using statements to your code:

    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);
            }
        }

    I hope it helps.

    Regards,
    Peter


    • Edited by Peter Holpar Tuesday, August 9, 2016 10:28 AM
    • Marked as answer by Pedro Gordo Tuesday, August 9, 2016 10:57 AM
    Tuesday, August 9, 2016 10:16 AM

All replies

  • Hello,

    Last time I looked you could do this via CSOM. You could via the PSI.

    If you are doing this via a PowerShell script already why not just write / convert the script to .Net.

    Paul


    Paul Mather | Twitter | http://pwmather.wordpress.com | CPS | MVP | Downloads

    Monday, August 8, 2016 7:10 PM
    Moderator
  • Hi Pedro,

    I've answered the same question recently on SharePoint.StackExchange. Assuming you mean C# on "code".

    First of all, you have to add references to the following assemblies in your project:

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

    Next, add these using statements to your code:

    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);
            }
        }

    I hope it helps.

    Regards,
    Peter


    • Edited by Peter Holpar Tuesday, August 9, 2016 10:28 AM
    • Marked as answer by Pedro Gordo Tuesday, August 9, 2016 10:57 AM
    Tuesday, August 9, 2016 10:16 AM
  • Hi Peter,

    That will create the projects, I think the question was around creating just the project sites - I assumed the Project existed without a project site etc.

    Paul


    Paul Mather | Twitter | http://pwmather.wordpress.com | CPS | MVP | Downloads

    Tuesday, August 9, 2016 10:50 AM
    Moderator
  • Hi Paul

    No problem, his solution works as well.

    Cheers

    Tuesday, August 9, 2016 10:57 AM
  • Hi Paul,

    Looking up the right SharePoint web site template for the enterprise project type is possible via the  WorkspaceTemplateName property of the EnterpriseProjectType instance. You can create a SharePoint site based on this web site template under the PWA root site using the SharePoint client object model.

    I have my doubt, however, that there is a method to bind the new web site to an existing project via the Project Server client object model. At least, the ProjectSiteUrl property of the DraftProject class (inherited from the Project class) is read-only. I think this action is available only via PSI using the UpdateProjectWorkspaceAddress method of the project web service. See a further example here.

    Peter


    Tuesday, August 9, 2016 11:36 AM
  • Tuesday, August 9, 2016 12:02 PM
    Moderator