In my vsto add-in for Microsoft Project, following code is not including the all work hours which i have defined at variable 'entryDates'. Because, Inside the loop, existing assignment is updating with new start, finish, and work hour instead of including new.
How to include the employee's multiple work hours with dates on the task?
Project project = Globals.ThisAddIn.Application.ActiveProject;
project.DefaultTaskType = PjTaskFixedType.pjFixedWork;
Task testTask = project.Tasks.Add("Test Task");
Resource resource = project.Resources.Add("Sridhar Netha");
Assignment newAssignment = testTask.Assignments.Add(ResourceID: resource.ID);
var entryDates =new List<EntryDate>(){
new EntryDate{ DateEntered=DateTime.Parse("2023-09-05"), Hours=(decimal)2 },
new EntryDate{ DateEntered=DateTime.Parse("2023-09-12"), Hours=(decimal)5 },
new EntryDate{ DateEntered=DateTime.Parse("2023-09-19"), Hours=(decimal)4 }
};
foreach (EntryDate entryDate in entryDates)
{
newAssignment.Start = entryDate.DateEntered;
newAssignment.Finish = entryDate.DateEntered;
newAssignment.Work = entryDate.Hours+"h";
}
Here is pseudo code to add work hours to an assignment:
Note 1: The date arguments of the TimeScaleData method are date/time values so an EndDate of 9/19/23 would be midnight and therefore leave no working time on that date. Therefore, add a day to the end date to get a full working day as expected. E.g. TimeScaleData("10/2/23", "10/5/23") yields an array of 3 days: 10/2, 10/3, 10/4.
Note 2: Work is stored in minutes, so multiply hours by 60.
Solution: