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 - Assign resource to the task in PWA - CSOM


In this article, I will show you how to assign a resource to task for selected 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 button control to the windows 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;

Step6: Add following code on button click event

private void assignResource_Click(object sender, EventArgs e)

{
// create PWA context

projContext = new ProjectContext(pwaPath); 

// Access with default logged in credentials

projContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 

projContext.Load(projContext.Projects); 

                projContext.ExecuteQuery();          

// pass project GUID in place of combo box selection

PublishedProject pubProject = projContext.Projects.GetByGuid(Guid.Parse(comboProjects.SelectedValue.ToString())); 

            projContext.Load(pubProject);

              projContext.ExecuteQuery(); 
projContext.Load(projContext.EnterpriseResources);

            projContext.ExecuteQuery(); 

            int numResInCollection = projContext.EnterpriseResources.Count(); 

            Microsoft.SharePoint.Client.User getUser = projContext.Web.SiteUsers.GetByLoginName   
            ("i:0#.w|xxx\\xxx"); // Replace with your user

            EnterpriseResource userRes = projContext.EnterpriseResources.GetByUser(getUser); 

            projContext.Load(userRes); 

            projContext.ExecuteQuery();   
 
            DraftProject projectDraft = pubProject.CheckOut(); 

            projContext.Load(projectDraft.Tasks);

            projContext.ExecuteQuery(); 

            DraftTaskCollection taskCollection = projectDraft.Tasks; 

            foreach (DraftTask task in taskCollection)
            {
                if (numResInCollection > 0)
                {

           if (task.Name == comboTaskList.SelectedItem.ToString()) // Replace with specific task

                    {
                        //task.Duration = "5d";

AssignmentCreationInformation assignment = new AssignmentCreationInformation();

                        assignment.Id = Guid.NewGuid();

                        assignment.TaskId = task.Id;

                        assignment.ResourceId = userRes.Id;

                        task.Assignments.Add(assignment);

                        projectDraft.Assignments.Add(assignment);

                        projectDraft.Update();

                    }

                }            

            }          

projContext.ExecuteQuery();

            projectDraft.Publish(true); 

            //QueueJob qJob = projContext.Projects.Update();

            //JobState jobState = projContext.WaitForQueue(projContext.Projects.Update(), 10); 

            projectDraft.CheckIn(true);

            projContext.ExecuteQuery();

Happy project server programming...

 

No comments:

Post a Comment