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:
TimeScaleValues tsvs = newAssignment.TimeScaleData("2023-09-05", "2023-09-20", pjAssignmentTimescaledWork, pjTimescaleDays);
foreach TimeScaleValue tsv in tsvs
{
/* check to see if tsv.StartDate is a date in the EntryDate list and if so,
add the hours using the Value property */
};
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:
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";
}*/
DateTime startDate = (DateTime)entryDates.Min(x => x.DateEntered);
DateTime endDate = (DateTime)entryDates.Max(x => x.DateEntered);
TimeScaleValues tsvs = newAssignment.TimeScaleData(startDate.Date, endDate.AddDays(1).Date, PjAssignmentTimescaledData.pjAssignmentTimescaledWork, PjTimescaleUnit.pjTimescaleDays);
foreach (TimeScaleValue tsv in tsvs)
{
// check to see if tsv.StartDate is a date in the EntryDate list and if so, add the hours using the Value property
var entDate = entryDates.Find(f => f.DateEntered.Value == tsv.StartDate);
if (entDate != null)
{
var minutes = entDate.Hours.Value * 60;
tsv.Value = minutes;
}
};