How to update Assigned Resource work hrs duration for each month

image.png (42.7 KB)

I am new in aspose library. i want to update the duration (hrs) of assigned resource in the right pan.
based on the calendar by default work duration has been auto assigned. i want to modify its value for each month using API.
is it possible to iterate monthly and set the new value (in hrs) using Aspose API.

@chetanbhola

Can you please explain the requirements in more details and also in the form of source MPP and desired MPP that you want to generate.

Thanks for your reply mudassir.
I have Source MPP file in which One Task (Task-01.01.001) is already exist. And one resource (Resource1) has been already assign for this task.
Work in hours is assigned daily basis in the right side of the sheet like below for Example:

1st Jan 2ndJan 3rd Jan
8 hrs 8 hrs 8 hrs

Now the requirement is i want to update those works programmatically using aspose library. so the output mpp file contains below :

1st Jan 2ndJan 3rd Jan
6 hrs 7 hrs 5.5 hrs

i don’t want to do any changes in task and resource. only want to update work hours for each date as per the condition in my project.
i’ll browse one Mpp file and after doing above changes i need to save it again.
hope this explanation would be clear to you. :slight_smile: :

RegardsOutputMpp.png (97.1 KB)
SourceMPP.png (86.9 KB)

Chetan

@chetanbhola

I suggest you to please visit this documentation section link for options related to working with tasks. You may see the examples for changing the task progress as well.

Thanks for the document link.
I had gone through the documents but still my problem has not resolved.

About InputMPP File:
File contain task “Supply Lot Sale Agreement” and one resource named “Builder” has already assign to it.

Now the requirement is to assign two more resource “Client” and “Sales Dept.” to the above task and assign timephased data 20d for june’2008 and 20d for july’2008 month.
And at the same time edit the timephase data of already assigned Resource “Builder” to 15 days and 20 days in June and July month.

Now the problem is for already assign resource “Builder”. Timephased data is not reflecting in the outputMPP file.

i am sharing my code below and attaching the ImputMPP , OutputMPP and ExpectedMPP snap shot.
Awaiting your quick reply.

public void UpdateMPPFile1()
{
string dataDir = “C:\ABC\”;
try
{

            Project project = new Project(dataDir + "MPPFileUpdate2.mpp") { CalculationMode = CalculationMode.None }; 

            Task tsk1 = project.SelectAllChildTasks().FirstOrDefault(t => t.Get(Tsk.Name) == "Supply Lot Sale Agreement");              

            foreach (ResourceAssignment assignment in tsk1.Assignments)
            {
                assignment.Set(Asn.WorkContour, WorkContourType.Contoured);
                assignment.Set(Asn.Units, 0);
                assignment.TimephasedData.Clear();

                CreateTimephasedDataAssignment(
                    project,
                    assignment,
                    new DateTime(2008, 6, 1, 8, 0, 0),
                    new DateTime(2008, 6, 30, 16, 0, 0),
                    120 * 60, false);

                CreateTimephasedDataAssignment(
                    project,
                    assignment,
                    new DateTime(2008, 7, 1, 8, 0, 0),
                    new DateTime(2008, 7, 31, 16, 0, 0),
                    160 * 60, true);

            }

            Resource rsc1 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Name) == "Sales Dept.");
            ResourceAssignment assignment1 = project.ResourceAssignments.Add(tsk1, rsc1);
            assignment1.Set(Asn.WorkContour, WorkContourType.Contoured);
            assignment1.Set(Asn.Units, 0);

            ClearTimephasedData(assignment1);

            CreateTimephasedDataAssignment(
                project,
                assignment1,
                new DateTime(2008, 6, 1, 8, 0, 0),
                new DateTime(2008, 6, 30, 16, 0, 0),
                160 * 60, false);

            CreateTimephasedDataAssignment(
                project,
                assignment1,
                new DateTime(2008, 7, 1, 8, 0, 0),
                new DateTime(2008, 7, 31, 16, 0, 0),
                160 * 60, true);

            Resource rsc2 = project.Resources.FirstOrDefault(r => r.Get(Rsc.Name) == "Client");
            ResourceAssignment assignment2 = project.ResourceAssignments.Add(tsk1, rsc2);
            assignment2.Set(Asn.WorkContour, WorkContourType.Contoured);
            assignment2.Set(Asn.Units, 0);

            ClearTimephasedData(assignment2);

            CreateTimephasedDataAssignment(
                project,
                assignment2,
                new DateTime(2008, 6, 1, 8, 0, 0),
                new DateTime(2008, 6, 30, 16, 0, 0),
                120 * 60, false);

            CreateTimephasedDataAssignment(
                project,
                assignment2,
                new DateTime(2008, 7, 1, 8, 0, 0),
                new DateTime(2008, 7, 31, 16, 0, 0),
                160 * 60, true);


            project.RecalculateResourceStartFinish();
            project.RecalculateResourceFields();
            project.Recalculate();

            // Save the project as MPP project file
            project.Save(dataDir + "AfterLinking_out.mpp");//,Aspose.Tasks.Saving.SaveFileFormat.MPP);
        }
        catch (Exception e)
        {
            
        }
    }

   private static IEnumerable<DateTime> EachDay(DateTime from, DateTime to)
    {
        for (var day = from.Date; day.Date <= to.Date; day = day.AddDays(1))
        {
            yield return day;
        }
    }

    private static int DaysWorking(Aspose.Tasks.Calendar calendar, DateTime dateStart, DateTime dateFinish)
    {
        int nDaysWorking = 0;
        if (calendar != null)
        {
            var nExceptions = calendar.Exceptions.Count(p => p.FromDate >= dateStart && p.FromDate <= dateFinish);

            foreach (DateTime day in EachDay(dateStart, dateFinish))
            {
                if (calendar.IsDayWorking(day))
                {
                    nDaysWorking++;
                }
            }

            return nDaysWorking - nExceptions;
        }

        foreach (DateTime day in EachDay(dateStart, dateFinish))
        {
            nDaysWorking++;
        }

        return nDaysWorking;
    }

    private static void ClearTimephasedData(ResourceAssignment assignment)
    {
        assignment.TimephasedData.Clear();
    }

    private static void CreateTimephasedDataAssignment(Project project, ResourceAssignment assignment, DateTime dateStart,
        DateTime dateFinish, double workInMinutes, bool isLastPortion)
    {
        var cal = assignment.Get(Asn.Task).Get(Tsk.Calendar) == null ? project.Get(Prj.Calendar) : assignment.Get(Asn.Task).Get(Tsk.Calendar);
        var nDaysWorking = DaysWorking(cal, dateStart, dateFinish);
        double workPerDay = nDaysWorking == 0 ? 0 : workInMinutes / nDaysWorking;
        workPerDay = Math.Round(workPerDay, 1);

        var days = EachDay(dateStart, dateFinish).ToList();
        while (!cal.IsDayWorking(days[0]))
        {
            days.RemoveAt(0);
        }
        var tds = new List<TimephasedData>();
        DateTime lastNonWorkingDay = DateTime.MinValue;
        DateTime lastWorkingDay = DateTime.MinValue;
        foreach (DateTime day in days)
        {
            if (cal.IsDayWorking(day))
            {
                if (lastNonWorkingDay != DateTime.MinValue)
                {
                    var start = lastWorkingDay.AddDays(1);
                    var finish = lastNonWorkingDay;
                    finish = cal.GetNextWorkingDayStart(finish);
                    TimephasedData item = TimephasedData.CreateWorkTimephased(
                        assignment.Get(Asn.Uid),
                        start,
                        finish,
                        TimeSpan.Zero,
                        TimeUnitType.Hour,
                        TimephasedDataType.AssignmentRemainingWork);
                    tds.Add(item);
                    lastNonWorkingDay = DateTime.MinValue;
                }

                var workStart = cal.GetNextWorkingDayStart(day.AddDays(-1));
                var fromHours = TimeSpan.FromMinutes(workPerDay);
                TimephasedData td = TimephasedData.CreateWorkTimephased(
                    assignment.Get(Asn.Uid),
                    workStart,
                    workStart.AddDays(1),
                    fromHours,
                    TimeUnitType.Hour,
                    TimephasedDataType.AssignmentRemainingWork);
                tds.Add(td);
                lastWorkingDay = workStart;
            }
            else
            {
                lastNonWorkingDay = day;
            }
        }<a class="attachment" href="/uploads/default/50216">ExpectedMPP.png</a> (32.3 KB)

InputMPP.png (31.7 KB)
OutputMPP.png (27.3 KB)
ExpectedMPP.png (32.3 KB)

        if (isLastPortion)
        {
            tds[tds.Count - 1].Finish = cal.GetPreviousWorkingDayEnd(tds[tds.Count - 1].Finish);
        }

        assignment.TimephasedData.AddRange(tds);
    }

@chetanbhola

Can you please share the source MPP file as well that we may use on our end to reproduce the issue.

i have attached source MPP file. MPPFileUpdate2.zip (50.1 KB)

@chetanbhola

Thank you for sharing the details. In order to investigate the issue further on our end a ticket with ID TASKSNET-4854 has been created. This thread has been linked with the issue so that you may be notified once the issue will be addressed.

Thanks for creating ticket wrt to queries. But I have not received any update in that thread yet.
Awaiting your feedback.

@chetanbhola

I regret to share that at present there are no updates available for the status of the issue. However, I have requested team to share updates and will share them with you as soon as it will be fixed.

Hi, Status for my issue is showing Resolved for TASKSNET-4854.
But i have not received any notification. can you please let me know the resolution steps need to do to fix it.
Thanks in advance.

@chetanbhola

The issue has been marked as fixed for upcoming Aspose.Tasks for .NET 21.6. We will share the notification with you once it will be shared online.

Hi Mudassir, Thanks for the resolution given in the new release.
I have download the latest Release from the URL.
Now i need to test the feature but my trial license has been expired as it was only for 1 month.
Can you please help me to extend the trial license for one more month, so that i can test all the features before purchasing it.

Thanks in advance.

@chetanbhola

I may help you in dealing with issues incurring with API. You may please consult our sales team in Aspose.Purchase forum in this regard for extending or getting new trial license.

The issues you have found earlier (filed as TASKSNET-4854) have been fixed in this update.