Creating Project site(Workspace) using CSOM

In this blog I’m gonna discuss on creating project site for a given project using CSOM.

Scenario: Say, your organization started using Project Server or Project Online without using Project site. But later, your org decided to have a project site for each of the existing project on the server. In that case, you can create project sites for all the projects and link it.

Here in the below piece of code, have created project site for only one project but if you want to create project sites in bulk for all projects just read all projects and keep this logic in a loop for every project.


protected void btnCreateProjectSite_Click(object sender, EventArgs e)
{
string projectName = "01-test";
context = CreatePWAConn();

var collection = context.LoadQuery(context.Projects.Where(p => p.Name == projectName).IncludeWithDefaultProperties(c=>c.Name,c=>c.ProjectSiteUrl));
context.ExecuteQuery();

if(collection.ToList().Count>0) // You can also use collection.Any();
{
PublishedProject publishedProject = collection.FirstOrDefault();
string url = publishedProject.ProjectSiteUrl;
publishedProject.CreateProjectSite("projectSite_" + projectName);
JobState jobState = context.WaitForQueue(context.Projects.Update(), 10);
if(jobState==JobState.Success)
{
ClientScript.RegisterStartupScript(this.GetType(), "ALERT", "alert('Project site created..')", true);
}
}

}