Skip to content

Create Resources and Link with tasks

ZeeshanShafqat edited this page Jan 24, 2014 · 3 revisions

###VSTO The following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference, then select the COM components tab.
  3. Select Microsoft Project 12.0 Object Library and click OK. This imports the Microsoft.Office.Interop.MSProject namespace at the start of the code.
  4. Use the code from the following example to read tasks and resources.

            //Create an Application object
            Microsoft.Office.Interop.MSProject.Application projectApplication = new MSProject.Application();

            object missingValue = System.Reflection.Missing.Value;
            //Open an MPP file
            projectApplication.FileOpenEx("Project1.mpp",
                missingValue, missingValue, missingValue, missingValue,
                missingValue, missingValue, missingValue, missingValue,
                missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
                missingValue, missingValue, missingValue, missingValue,
                missingValue);
            Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;

            int iRecourceId = 1;
            foreach (Task tsk in project.Tasks)
            {
                string developer = "Developer0" + iRecourceId;
                project.Resources.Add(developer, iRecourceId);
                tsk.Assignments.Add(tsk.ID, iRecourceId, missingValue);
                iRecourceId++;
            }

            projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);

###Aspose.Tasks The following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference, then select the .NET tab.
  3. Select Aspose.Tasks and then click OK. This imports the Aspose.Tasks namespace at the start of the code.
  4. Use the code from the following example to create resources and link them to tasks.

            Project prj = new Project("Project.mpp");

            //Declare ChildTasksCollector class object
            ChildTasksCollector collector = new ChildTasksCollector();

            //Use TaskUtils to get all children tasks in RootTask
            TaskUtils.Apply(prj.RootTask, collector, 0);

            //Define Resources
            ArrayList resources = new ArrayList();
            for (int i = 1; i <= 5; i++)
            {
                string devloper = "Developer0" + i;
                //Create resource
                Resource rec = new Resource(devloper);
                rec.Type = ResourceType.Work;

                //Add resource to project
                prj.Resources.Add(rec);

                //define assignment
                ResourceAssignment ra = new ResourceAssignment((Aspose.Tasks.Task)collector.Tasks[i], rec);
                prj.ResourceAssignments.Add(ra);
            }

            prj.CalcResourceUids();
            prj.CalcResourceIds();
            prj.CalcResourceFields();
            prj.CalcResourceAssignmentUids();
            prj.CalcResourceAssignmentIds();

            prj.Save("Project1_CSharp_Aspose.mpp", Aspose.Tasks.Saving.SaveFileFormat.MPP);
                        

Download

Clone this wiki locally