Wednesday, 22 April 2020

How to add assignments to Projects in SharePoint 2013 using CSOM

  1. PublishedProject publishedProject = null;
  2. DraftProject draftProject = null;
  3. DraftAssignment draftAssignment = null;
  4. AssignmentCreationInformation assignmentInfo = null;
  5. QueueJob queueJobAssignment = null;
  6. try
  7. {
  8.     publishedProject = availableProjects[new Guid(projectId)]; //find publishedProject from all available Projects
  9.     draftProject = publishedProject.CheckOut();
  10.     // Load/read all assignments
  11.     projectContext.Load(draftProject.Assignments);
  12.     projectContext.ExecuteQuery();
  13.     
  14.         assignmentInfo = new Microsoft.ProjectServer.Client.AssignmentCreationInformation();
  15.         try
  16.         {
  17.             assignmentInfo.Id = new Guid();
  18.             assignmentInfo.TaskId = new Guid();
  19.             assignmentInfo.ResourceId = new Guid();
  20.             assignmentInfo.Start = DateTime.ParseExact("2015-01-01 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
  21.             assignmentInfo.Finish = DateTime.ParseExact("2016-01-01 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
  22.             assignmentInfo.Notes = string.Empty;
  23.             // Add the assignment to the checked out project.
  24.             draftAssignment = draftProject.Assignments.Add(assignmentInfo);
  25.             //Update the assignment with the baseline information
  26.             draftAssignment.ActualWorkTimeSpan = TimeSpan.FromMilliseconds(Convert.ToDouble("100"));
  27.             draftAssignment.WorkTimeSpan = TimeSpan.FromMilliseconds(Convert.ToDouble("10"));
  28.             draftAssignment["BaselineWorkTimeSpan"] = TimeSpan.FromMilliseconds(Convert.ToDouble("50"));
  29.             draftAssignment.ActualCost = Convert.ToDouble("500");
  30.             draftAssignment.Cost = Convert.ToDouble("100");
  31.             draftAssignment["BaselineCost"] = Convert.ToDouble(drAssignments[ind]["BaselineCost"]);
  32.             draftAssignment.ActualStart = DateTime.ParseExact("2016-01-01 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
  33.             draftAssignment["BaselineStart"] = DateTime.ParseExact("2016-01-01 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
  34.             draftAssignment.ActualFinish = DateTime.ParseExact("2016-02-02 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
  35.             draftAssignment["BaselineFinish"] = DateTime.ParseExact("2016-02-02 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
  36.         }
  37.         catch (Exception ex)
  38.         {
  39.             // log exception assignment not added
  40.         }
  41.     
  42.     queueJobAssignment = draftProject.Update();
  43.     queueJobAssignment = draftProject.Publish(true);
  44. }
  45. catch (Exception ex)
  46. {
  47.     // log exception assignment not added
  48. }

No comments:

Post a Comment