Friday, 17 April 2020

programmatically project online to update project assignment of actual work and cost using csom

private static void updateAssignement() {
 try {
  //Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
  //Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fileName);
  #
  region Update assignments


  //xlWorksheet = xlWorkbook.Sheets[4]; //Assignments sheet
  //xlRange = xlWorksheet.UsedRange;


  //rowCount = xlRange.Rows.Count;
  //colCount = xlRange.Columns.Count;
  SecureString passWord = new SecureString();
  foreach(char c in "password321@".ToCharArray()) passWord.AppendChar(c);
  projContext.Credentials = new SharePointOnlineCredentials("projectonlinepremium@projectonlinepremium.onmicrosoft.com", passWord);
  ProjectContext context = projContext;
  PublishedProject publishedProject = context.Projects.GetByGuid(new Guid("7092e816-d581-e911-b07b-00155d20e111"));
  DraftProject draftProject = publishedProject.Draft;
  JobState jobState1 = context.WaitForQueue(draftProject.CheckIn(true), 200);


  DraftProject checkedOutProject = publishedProject.CheckOut();


  context.Load(checkedOutProject.Tasks, t => t.IncludeWithDefaultProperties(ta => ta.ActualWork, ta => ta.ActualCost, ta => ta.Assignments, ta => ta.IsMilestone, ta => ta.Name, ta => ta.Id, ta => ta.OutlineLevel, ta => ta.OutlinePosition, ta => ta.Work, ta => ta.StatusManager));
  context.ExecuteQuery();


  context.Load(checkedOutProject.ProjectResources, r => r.IncludeWithDefaultProperties(re => re.Id, re => re.Name));
  context.ExecuteQuery();




  DraftTaskCollection taskCollection = checkedOutProject.Tasks;


  AssignmentCreationInformation newAssignment;
  //for (int i = 2; i <= rowCount; i++)
  //{
  string resourceName = "Demo Resource Teammember"; //xlRange.Cells[i, 2].Value2.ToString();
  string taskName = "MPPProject2020_Export_Items"; //xlRange.Cells[i, 3].Value2.ToString();


  //double startdate = double.Parse("04/16/2020");//double.Parse(xlRange.Cells[i, 4].Value2.ToString());
  DateTime start = DateTime.Parse("04/16/2020");


  //var task = taskCollection.Where(t => t.Name == taskName && t.Start.Day == start.Day && t.Start.Month == start.Month && t.Start.Year == start.Year);
  var task = taskCollection.Where(t => t.Name == taskName);
  if (task == null || task.Count() == 0) {
   Console.WriteLine("Task not found in Project tasks");
   //continue;
  }
  string actualWork = "115"; //xlRange.Cells[i, 6].Value2.ToString().Split()[0];
  string work = "115"; //xlRange.Cells[i, 9].Value2.ToString().Split()[0];


  task.First().ActualWork = "128h"; //(Convert.ToDecimal(task.First().ActualWork.Replace("h", "")) + Convert.ToDecimal(actualWork)).ToString() + "h";
  task.First().Work = "115h"; //(Convert.ToDecimal(task.First().Work.Replace("h", "")) + Convert.ToDecimal(work)).ToString() + "h";


  //if (task.First().OutlineLevel != 1)
  //{
  // if (task.First().Duration == "1d")
  // task.First().Duration = (Convert.ToDecimal(task.First().Duration.Replace("d", "")) * Convert.ToDecimal(work) / 8) + "d";
  // else
  // task.First().Duration = (Convert.ToDecimal(task.First().Duration.Replace("d", "")) + Convert.ToDecimal(work) / 8) + "d";
  //}




  var res = checkedOutProject.ProjectResources.Where(r => r.Name == resourceName);
  if (res == null || res.Count() == 0) {
   Console.WriteLine("Task - " + taskName + " can not be assigned as " + resourceName + " does not exist in PWA");
   //continue;
  }


  newAssignment = new AssignmentCreationInformation();
  newAssignment.Id = Guid.NewGuid();
  newAssignment.TaskId = task.First().Id;
  newAssignment.ResourceId = res.First().Id;
  newAssignment.Start = start;

  task.First().Assignments.Add(newAssignment);
  checkedOutProject.Assignments.Add(newAssignment);
  //}
  QueueJob queueJob1 = checkedOutProject.Update();
  jobState1 = context.WaitForQueue(queueJob1, 200);
  QueueJob queueJob2 = checkedOutProject.Publish(false);
  jobState1 = context.WaitForQueue(queueJob2, 200);
  checkedOutProject.CheckIn(false);
  QueueJob queueJob3 = context.Projects.Update();
  jobState1 = context.WaitForQueue(queueJob3, 200);
  context.ExecuteQuery();#
  endregion


  //xlWorkbook.Close();
 } catch (Exception ex) {

 }
}

No comments:

Post a Comment