Update Task Level Custom Field using CSOM
Update Task Level Custom Field using CSOM
Update Task Level Custom Field using CSOM. In this post, I will describe you to update task level custom field using CSOM. During a project plan, you want to add a custom field to a task schedule.Go to Server Settings and add a Task Level custom field and add it to task schedule and Task Summary. Now update a task level custom field by CSOM then below is code.[AdSense-A]
using System; using System.Collections.Generic; using System.Linq; using System.Security; using Microsoft.ProjectServer.Client; using Microsoft.SharePoint.Client; using PRCLNT = Microsoft.ProjectServer.Client; namespace UpdateTaskLevelCustomField_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 ProjectCont1 = new PRCLNT.ProjectContext(OnlinePWA))//PWA Url { SecureString passWord3 = new SecureString(); foreach (char c in Passwords.ToCharArray()) passWord3.AppendChar(c); ProjectCont1.Credentials = new SharePointOnlineCredentials(UserName, passWord3); var projCollection = ProjectCont1.LoadQuery( ProjectCont1.Projects .Where(p => p.Name == "Test Project")); ProjectCont1.ExecuteQuery(); PublishedProject proj2Edit = projCollection.First(); DraftProject projCheckedOut = proj2Edit.CheckOut(); ProjectCont1.Load(projCheckedOut.Tasks); ProjectCont1.ExecuteQuery(); DraftTaskCollection tskcoll = projCheckedOut.Tasks; foreach (DraftTask Task in tskcoll) { if ((Task.Name != null) && (Task.Name == "Taks1")) { ProjectCont1.Load(Task.CustomFields); ProjectCont1.ExecuteQuery(); Task.Duration = "21d"; Task["Custom_b7cf72f87bbae511a30d00155d018400"] = "Custom Field Value"; //Inernal name of Task level custom field AssignmentCreationInformation r = new AssignmentCreationInformation(); r.Id = Guid.NewGuid(); r.TaskId = Task.Id; Task.Assignments.Add(r); } } projCheckedOut.Publish(true); QueueJob qJob = ProjectCont1.Projects.Update(); JobState jobState = ProjectCont1.WaitForQueue(qJob, 20); } } } }