Replace a Resource on an existing Task

Hi,

I’m exporting to an existing MS Project file (one that I would have generated new from Aspose Tasks during a previous export) of which I need to change the resource assigned to a task (simply change from Person 1 to Person 2). What is the proper way to do this?

Currently, I delete the assignment and add a new one with the proper resource. When I open up the exported project file the duration on this particular task is correct (e.g. 8 hours) but the finish date is 16 hours from the start. It seems as though the delete is not clearing the assignment and the adding of the new assignment with the new resource is causing the finish date to be pushed out by double the hours. Below is a sample of my code:

//I loop through to find the applicable assignment
_myAssignment = null;
foreach (ResourceAssignment _assignment in _myMSProject.ResourceAssignments)
{
if (_assignment.Get(Asn.Uid) == _myTask.Get(Tsk.Uid))
{
_myAssignment = _assignment;
break;
}
}

//I then delete the assignment
_myAssignment.Delete()

//I then add a new assignment with the updated resource
_myAssignment = _myMSProject.ResourceAssignments.Add(_myTask, myNewResource);

I’m using Aspose.Tasks for .Net version 19.1, exporting to MS Project 2010 and developing via VS 2017.

Thanks, Calvin

@calvin.howell

Resource assignment looks fine but for finish date, we need to investigate it. Can you please share sample file with so that we could investigate in detail.

Moreover, You may share confidential files in a private message and the files shared in private message shall only be accessible to Aspose staff. For this purpose, please find “Message” button by clicking on my name.

@calvin.howell

Please try the following code sample and share your feedback with us. We have tested it using Aspose.Tasks for .NET 19.1 and we are unable to reproduce the issue.

Resource rsc = project1.Resources.GetById(1);

ChildTasksCollector collector = new ChildTasksCollector();
TaskUtils.Apply(project1.RootTask, collector, 0);

ResourceAssignment _myAssignment = null;

Task tsk = null;
foreach (Task tsk1 in collector.Tasks)
{
    if(tsk1.Get(Tsk.Name) == "CHILD 2")
    {
        tsk = tsk1;
    }
}

foreach (ResourceAssignment _assignment in project1.ResourceAssignments)
{
    if (_assignment.Get(Asn.Uid) == tsk.Get(Tsk.Uid))
    {
        _myAssignment = _assignment;
        break;
    }
}

if (_myAssignment != null)
{
    _myAssignment.Delete();
    _myAssignment = project1.ResourceAssignments.Add(tsk, rsc);
}