How to assign task in C# OM Porject Server - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Tuesday, May 28, 2019

How to assign task in C# OM Porject Server

How to assign task in C# OM Porject Server

Song Nghia - Technical Consultant

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