Some problem with resourceassignment

Hi
I’m using aspose task 6.3.0 & I want to add some resource assignment programmatically(actually i read data from database), so use the following sample code here, but result wasn’t what I want (a simple file with 2 task & 2 resource ), if you use the code the final file doesn’t assign resource properly so I modify it & new source was what i want

            Project prj = new Project();

        <span style="color:#2b91af;">Task</span> tskRoot = <span style="color:blue;">new</span> <span style="color:#2b91af;">Task</span>();
        <span style="color:#2b91af;">Task</span> tsk1 = <span style="color:blue;">new</span> <span style="color:#2b91af;">Task</span>(<span style="color:#a31515;">"Task1"</span>);
        <span style="color:#2b91af;">Task</span> tsk2 = <span style="color:blue;">new</span> <span style="color:#2b91af;">Task</span>(<span style="color:#a31515;">"Task2"</span>);

        tsk1.DurationFormat = <span style="color:#2b91af;">TimeUnitType</span>.Day;
        tsk1.Duration = <span style="color:blue;">new</span> <span style="color:#2b91af;">TimeSpan</span>(0, 0, 480, 0);
        tsk1.ManualDuration = tsk1.Duration;
        tsk1.Start = <span style="color:#2b91af;">DateTime</span>.Parse(<span style="color:#a31515;">"18-Aug-2009 8:00 AM"</span>);

        tsk2.DurationFormat = <span style="color:#2b91af;">TimeUnitType</span>.Day;
        tsk2.Duration = <span style="color:blue;">new</span> <span style="color:#2b91af;">TimeSpan</span>(0, 0, 480, 0);
        tsk2.ManualDuration = tsk2.Duration;
        tsk2.Start = <span style="color:#2b91af;">DateTime</span>.Parse(<span style="color:#a31515;">"18-Aug-2009 8:00 AM"</span>);

        <span style="color:#2b91af;">Resource</span> res = <span style="color:blue;">new</span> <span style="color:#2b91af;">Resource</span>();
        <span style="color:#2b91af;">Resource</span> res1 = <span style="color:blue;">new</span> <span style="color:#2b91af;">Resource</span>(<span style="color:#a31515;">"Resource1"</span>);
        <span style="color:#2b91af;">Resource</span> res2 = <span style="color:blue;">new</span> <span style="color:#2b91af;">Resource</span>(<span style="color:#a31515;">"Resource2"</span>);

        <span style="color:#2b91af;">ResourceAssignment</span> ra1 = <span style="color:blue;">new</span> <span style="color:#2b91af;">ResourceAssignment</span>(tsk1, res1);
        ra1.Start = <span style="color:#2b91af;">DateTime</span>.Parse(<span style="color:#a31515;">"18-Aug-2009 8:00 AM"</span>);
        ra1.Finish = <span style="color:#2b91af;">DateTime</span>.Parse(<span style="color:#a31515;">"18-Aug-2009 5:00 PM"</span>);
        ra1.Uid = 1;

        <span style="color:#2b91af;">ResourceAssignment</span> ra2 = <span style="color:blue;">new</span> <span style="color:#2b91af;">ResourceAssignment</span>(tsk2, res2);
        ra2.Start = <span style="color:#2b91af;">DateTime</span>.Parse(<span style="color:#a31515;">"19-Aug-2009 8:00 AM"</span>);
        ra2.Finish = <span style="color:#2b91af;">DateTime</span>.Parse(<span style="color:#a31515;">"19-Aug-2009 5:00 PM"</span>);
        ra2.Uid = 2;

        prj.RootTask = tskRoot;
        prj.RootTask.Children.Add(tsk1);
        prj.RootTask.Children.Add(tsk2);

        <span style="background-color: rgb(255, 255, 0);">prj.Resources.Add(res);</span><span style="color:green;">//1</span>
        prj.Resources.Add(res1);
        prj.Resources.Add(res2);

        <span style="background-color: rgb(255, 255, 0);">prj.AddResourceAssignment(tsk1, res1)</span>;<span style="color:green;">//2</span>

        <span style="color:green;">//prj.ResourceAssignments.Add(ra1); //3</span>
        prj.ResourceAssignments.Add(ra2);

        prj.CalcResourceUids();
        prj.CalcTaskIds();
        prj.CalcTaskUids();
        prj.CalcResourceUids();
        prj.CalcResourceStartFinish();
        prj.CalcResourceIds();
        prj.CalcResourceFields();
        prj.CalcResourceAssignmentUids();
        prj.CalcResourceAssignmentIds();
        prj.UpdateReferences();
        <span style="color:green;">//Save the Project</span>
        prj.Save(<span style="color:#a31515;">"C:\\testres.xml"</span>, Aspose.Tasks.Saving.<span style="color:#2b91af;">SaveFileFormat</span>.XML);<br><br>if comment the line that add empty resource "res" to prj final file is buggy(\\1)<br>if use <span style="background-color: rgb(255, 255, 0);">ResourceAssignments.Add</span> for both resource "res1" & "res2" final file is buggy<br>for desired file you must add first assignment with <span style="background-color: rgb(255, 255, 0);">AddResourceAssignment</span> and the others with <br><span style="background-color: rgb(255, 255, 0);">ResourceAssignments.Add</span> in other case task's duration is 0 or assignment didn't work<br>Thank for your help<br><br></pre><br><a href="http://docs.aspose.com:8082/docs/display/tasksnet/General+Resource+Assignment+Properties"></a>

Hi Pazoki,

I have observed the requirements shared by you and analyzing them on my end. I will share my feedback with you as soon as I will complete my investigation shortly.

Hi Pazoki,


We are sorry for the delayed response.

Aspose.Tasks provides simplified features now for adding tasks, resources and resource assignment. You may please give a try to the following sample code using latest version Aspose.Tasks for .NET 6.6.0 which performs the required tasks and let us know your feedback.

Project prj = new Project();
prj.StartDate = DateTime.Parse(“18-Aug-2009 8:00 AM”);
Task tsk1 = prj.AddTask(“Task1”);
tsk1.ConstraintType = ConstraintType.StartNoEarlierThan;
tsk1.Start = DateTime.Parse(“18-Aug-2009 8:00 AM”);
Task tsk2 = prj.AddTask(“Task2”);
tsk2.ConstraintType = ConstraintType.StartNoEarlierThan;
tsk2.Start = DateTime.Parse(“19-Aug-2009 8:00 AM”);
Resource res1 = prj.AddResource(“Resource1”);
ResourceAssignment ra1 = prj.AddResourceAssignment(tsk1, res1);
ResourceAssignment ra2 = prj.AddResourceAssignment(tsk2, res1);
//Save the Project
prj.Save(“Project.Xml”, Aspose.Tasks.Saving.SaveFileFormat.XML);

Please feel free to write us back if you have any other query in this regard.

Hi
Thank for your help,It works :smiley:
if we use Addxxxx functions is it necessary to call Calcxxxx after them?because if I call one of them final file was buggy

Hi Pazoki,


We are glad to know that your issue is resolved.

Now there is no need to call Calcxxxx functions as Addxxxx functions do it themselves. Please feel free to write us back if you have any other query related to Aspose.Tasks.