Remove Resource from Project Task using CSOM

Remove Resource from Project Task using CSOM

Remove Resource from Project Task using CSOM. In this post, I am trying to Remove Resource from Project Task using CSOM. During a project plan, multiple resources can be assigned to the single task.But in this post, you will learn programmatically remove the resource from a task using CSOM.In my previous p, st i explained Assign Resource on Task using CSOM[AdSense-A]

If you don’t know to add references in your solutions then follow the post below

Add  references in Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using PRCLNT = Microsoft.ProjectServer.Client;

namespace RemoveResourceFromTask_Using_CSOM
{
    class Program
    {
        static string UserName = "UserName";
        static string Passwords = "Password";
        public static string OnlinePWA = "PWAPath";
        static void Main(string[] args)
        {
            using (PRCLNT.ProjectContext ProjectCont = new PRCLNT.ProjectContext(OnlinePWA))//PWA Url
            {
                SecureString passWord4 = new SecureString();
                foreach (char c in Passwords.ToCharArray()) passWord4.AppendChar(c);
                ProjectCont.Credentials = new SharePointOnlineCredentials(UserName, passWord4);
                var projCollection = ProjectCont.LoadQuery(ProjectCont.Projects.Where(p => p.Name == "Project Name"));
                ProjectCont.ExecuteQuery();
                PublishedProject proj2Edit = projCollection.First();
                DraftProject projCheckedOut = proj2Edit.CheckOut();
                ProjectCont.Load(projCheckedOut.Tasks);
                ProjectCont.ExecuteQuery();
                DraftTaskCollection tskcoll = projCheckedOut.Tasks;
                foreach (DraftTask tsk in tskcoll)
                {
                    if ((tsk.Name != null) && (tsk.Name == "Task Name"))
                    {
                        ProjectCont.Load(tsk.Assignments);
                        ProjectCont.ExecuteQuery();
                        foreach (var user in tsk.Assignments)
                        {
                            ProjectCont.Load(user.Resource);
                            ProjectCont.ExecuteQuery();
                            if (user.Resource.Name == "Resource Name")
                            {
                                tsk.Assignments.Remove(user);
                            }
                        }
                    }
                }
                projCheckedOut.Publish(true);
                QueueJob qJob = ProjectCont.Projects.Update();
                JobState jobState = ProjectCont.WaitForQueue(qJob, 10);
            }
        }
    }
}


 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!