About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Tuesday 14 June 2016

Project Server 2013 - Create project in PWA - CSOM

In this article, I will show you how to create project in PWA site using CSOM in project server 2013. In this series of articles, I am taking windows application as a working example to read/write data from PWA.
Prerequisites
1.      Visual studio 2010/2012/2013/2015
2.      Copy Microsoft.ProjectServer.Client.dll from SharePoint 2013 servers from the location
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI
Step1: Create windows application from visual studio
Step2: Add Microsoft.ProjectServer.Client.dll
Step3: Add one two text boxes and one button control to the form.
Step4: Declare PWA site URL in form load. Change URL accordingly based on your environment
private const string pwaPath = "https://dev.xxx.com/PWA/";
Step5: Declare project context and project type on Form load
private static ProjectContext projContext;
private static string basicEpt = "Enterprise Project - Default";   // Basic enterprise project type.
Step6: Add following code on button click event
private void createproject_Click (object sender, EventArgs e)
{
 
// create PWA context
projContext = new ProjectContext(pwaPath);
 
// Access with default logged in credentials
projContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
 
            try
             {               
                ProjectCreationInformation newProj = new ProjectCreationInformation();
 
                newProj.Id = Guid.NewGuid();
                newProj.Name = txtProjectName.Text;
                newProj.Description = txtProjectDesc.Text;
                newProj.Start = DateTime.Today.Date;
 
                newProj.EnterpriseProjectTypeId = GetEptUid(basicEpt);
 
                PublishedProject newPublishedProj = projContext.Projects.Add(newProj);
                QueueJob qJob = projContext.Projects.Update();
 
                projContext.Load(qJob);
                projContext.Load(projContext.Projects);
                projContext.ExecuteQuery();
 
                JobState jobState = projContext.WaitForQueue(qJob, timeoutSeconds);
 
                label1.ForeColor = Color.Green;
 
                label1.Text = "Successfully created project-" + txtProjectName.Text;
 
                txtProjectDesc.Text = "";
                txtProjectName.Text = "";
 
                //LoadProjects(projContext);
 
            }
 
            catch (Exception ex)
            {
                label1.ForeColor = Color.Red;
                label1.Text = ex.Message.ToString();
            }
 
        }
           
            //Get/Read the GUID of the specified enterprise project type.
        private static Guid GetEptUid(string eptName)
        {
            Guid eptUid = Guid.Empty;
 
            try
            {
projContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
               
                var eptList = projContext.LoadQuery(
                    projContext.EnterpriseProjectTypes.Where(
                        ept => ept.Name == eptName));
                projContext.ExecuteQuery();
 
                eptUid = eptList.First().Id;
            }
            catch (Exception ex)
            {
                string msg = string.Format("GetEptUid: eptName = \"{0}\"\n\n{1}",
                    eptName, ex.GetBaseException().ToString());
                throw new ArgumentException(msg);
            }
            return eptUid;
        }
Happy project server programming...

No comments:

Post a Comment