Utility Features Available in Aspose.Tasks for .NET
Calculate Critical Path
The critical path is the tasks, or task, that ultimately determine when your project will end. Keeping track of the critical path and the resources assigned to them helps keep the project on time. Microsoft Project calculates the critical path based on tasks that have no slack, have specific date constraints (must start on, must finish on, as soon as possible or as late as possible) or have the same, or later, end date as the project. Aspose.Tasks allows you to calculate the critical path.
The Project class provides the CriticalPath property which is used to retrieve the collection of tasks that comprise the project’s critical path. The following piece of code calculates and displays the tasks in the critical path.
1Project project = new Project("New Project.mpp");
2project.CalculationMode = CalculationMode.Automatic;
3
4Task subtask1 = project.RootTask.Children.Add("1");
5Task subtask2 = project.RootTask.Children.Add("2");
6Task subtask3 = project.RootTask.Children.Add("3");
7project.TaskLinks.Add(subtask1, subtask2, TaskLinkType.FinishToStart);
8
9// Display the critical path now
10foreach (Task task in project.CriticalPath)
11{
12 Console.WriteLine(task.Get(Tsk.Name));
13}
Printing the TaskWritingException
A project consists of a number of tasks and Aspose.Tasks for .NET API allows adding, removing and updating task information. If there’s a problem when writing tasks, use TasksWritingException to catch them.
Aspose.Tasks for .NET supports printing a message when there’s an exception in writing a task. This is done with TaskWritingException, in a similar way to how TaskReadingException is used.
The log message is contained in the public property TasksWritingException.LogText, as shown in the following code example.
1try
2{
3 Project project = new Project("New Project.mpp");
4 Console.Write("This example will only work if you apply a valid Aspose License. You can purchase full license or get 30 day temporary license from http://www.aspose.com/purchase/default.aspx.");
5 project.Save("output.mpp", SaveFileFormat.MPP);
6}
7catch (TasksWritingException ex)
8{
9 Console.WriteLine(ex.LogText);
10}
11catch (NotSupportedException ex)
12{
13 Console.WriteLine(ex.Message + "\nThis example will only work if you apply a valid Aspose License. You can purchase full license or get 30 day temporary license from http://www.aspose.com/purchase/default.aspx.");
14}
Working with Filter Data from MPP Files
Aspose.Tasks allows developers to read information about filters applied to MPP file data. This article shows how to retrieve filter definition and filter criteria data from a Microsoft Project MPP file.
Reading Filter Definition Data
1// Instantiate project and access task filters
2Project project = new Project("ReadFilterDefinitionData.mpp");
3List<Filter> taskFilters = project.TaskFilters.ToList();
4Console.WriteLine("Task Filters Count: " + taskFilters.Count);
5Console.WriteLine("All Tasks: " + taskFilters[0].Name);
6Console.WriteLine("Task Item: " + taskFilters[0].FilterType);
7Console.WriteLine("Task Filters Show In Menu: " + taskFilters[0].ShowInMenu);
8Console.WriteLine("Task filter ShowRelatedSummaryRows: " + taskFilters[0].ShowRelatedSummaryRows);
9
10// Access resource filters
11List<Filter> filters = project.ResourceFilters.ToList();
12Console.WriteLine("Project.ResourceFilters count: " + filters.Count);
13Console.WriteLine("Resource Filter Item Type: Item.ResourceType: " + filters[0].FilterType);
14Console.WriteLine("Resource filter ShowInMenu" + filters[0].ShowInMenu);
15Console.WriteLine("Resource filter ShowRelatedSummaryRows: " + filters[0].ShowRelatedSummaryRows);
Reading Filter Criteria Data
1Project project = new Project("New Project.mpp");
2
3Filter filter = project.TaskFilters.ToList()[1];
4Console.WriteLine(filter.Criteria.CriteriaRows.Count);
5Console.WriteLine(filter.Criteria.Operation.ToString());
6
7FilterCriteria criteria1 = filter.Criteria.CriteriaRows[0];
8Console.WriteLine(criteria1.Test.ToString());
9Console.WriteLine(criteria1.Field.ToString());
10Console.WriteLine(criteria1.Values[0].ToString());
11
12FilterCriteria criteria2 = filter.Criteria.CriteriaRows[1];
13Console.WriteLine(criteria2.Operation.ToString());
14Console.WriteLine(criteria2.CriteriaRows.Count);
15
16FilterCriteria criteria21 = criteria2.CriteriaRows[0];
17Console.WriteLine(criteria21.Test.ToString());
18Console.WriteLine(criteria21.Field.ToString());
19Console.WriteLine(criteria21.Values[0].ToString());
20
21FilterCriteria criteria22 = criteria2.CriteriaRows[1];
22Console.WriteLine(criteria22.Test.ToString());
23Console.WriteLine(criteria22.Field.ToString());
24Console.WriteLine(criteria22.Values[0].ToString());
25Console.WriteLine(filter.Criteria);
Reading Group Definition Data
A Microsoft Project data file may contain data in groups. Aspose.Tasks for .NET provides the facility to read the group definition data as shown in the below example.
1Project project = new Project("New Project.mpp");
2
3Console.WriteLine("Task Groups Count: " + project.TaskGroups.Count);
4
5Group taskGroup = project.TaskGroups.ToList()[1];
6Console.WriteLine("Group Name:", taskGroup.Name);
7Console.WriteLine("Group Criteria count: " + taskGroup.GroupCriteria.Count);
8Console.WriteLine("************* Retrieving Task Group's Criterion information *************");
9
10GroupCriterion criterion = taskGroup.GroupCriteria.ToList()[0];
11Console.WriteLine("Criterion Field: " + criterion.Field.ToString());
12Console.WriteLine("Criterion GroupOn: " + criterion.GroupOn.ToString());
13Console.WriteLine("Criterion Cell Color: " + criterion.CellColor);
14Console.WriteLine("Criterion Pattern: " + criterion.Pattern.ToString());
15
16if (taskGroup == criterion.ParentGroup)
17{
18 Console.WriteLine("Parent Group is equal to task Group.");
19}
20
21Console.WriteLine("*********** Retrieving Criterion's Font Information ***********");
22Console.WriteLine("Font Name: " + criterion.Font.Name);
23Console.WriteLine("Font Size: " + criterion.Font.Size);
24Console.WriteLine("Font Style: " + criterion.Font.Style);
25Console.WriteLine("Ascending/Descending: " + criterion.Ascending);
Reading Table Data from MPP File
The Aspose.Tasks for .NET API supports reading table data from Microsoft Project data files. Project.Tables implements the ICollection interface to provide access to an MPP file’s table data. The feature is supported for all versions of Microsoft Project data files, including MPP 2003, 2007, 2010 and 2013.
The below example shows how to retrieve table data from a Microsoft Project MPP file. The values of table properties such as width, title, title alignment and data alignment are displayed here for demonstration.
1Project project = new Project("New Project.mpp");
2
3// Access table
4Table task1 = project.Tables.ToList()[0];
5Console.WriteLine("Table Fields Count" + task1.TableFields.Count);
6
7// Display all table fields information
8foreach (TableField tableField in task1.TableFields)
9{
10 Console.WriteLine("Field width: " + tableField.Width);
11 Console.WriteLine("Field Title: " + tableField.Title);
12 Console.WriteLine("Field Title Alignment: " + tableField.AlignTitle.ToString());
13 Console.WriteLine("Field Align Data: " + tableField.AlignData.ToString());
14}
Extracting Embedded Objects from Task or Review form
Microsoft Project data files (MPP/XML) may contain embedded objects such as documents, excel sheets, PDF, images etc. in Task or Resource views. Aspose.Tasks for .NET API provides the capability to extract these from a project’s Task or Resource view as shown in the below topics.
Extracting Embedded Objects
Embedded objects (those which were created from the file by selecting a file path) are packed into OLE Package inside MPP file. To extract the original file you can use Content and FullPath properties of an instance of the OleObject class.
1Project project = new Project("ExtractEmbeddedObjects.mpp");
2OleObject ole = project.OleObjects.ToList()[0];
3// We have to check this property because it can be null if the embedded object was created inside the ms project application Or, alternatively, you can use this check: if (ole.FileFormat == "Package")
4if (!string.IsNullOrEmpty(ole.FullPath))
5{
6 using (FileStream stream = new FileStream("out.ole", FileMode.Create))
7 {
8 stream.Write(ole.Content, 0, ole.Content.Length);
9 }
10}
Embedded objects which were created inside Microsoft Project application can be extracted this way:
1IDictionary<string, string> fileFormatExt = new Dictionary<string, string>();
2fileFormatExt.Add("RTF", "_rtfFile_out.rtf");
3fileFormatExt.Add("MSWordDoc", "_wordFile_out.docx");
4fileFormatExt.Add("ExcelML12", "_excelFile_out.xlsx");
5Project project = new Project("New Project.mpp");
6foreach (OleObject oleObject in project.OleObjects)
7{
8 if (!string.IsNullOrEmpty(oleObject.FileFormat) && fileFormatExt.ContainsKey(oleObject.FileFormat))
9 {
10 using (FileStream stream = new FileStream("EmbeddedContent_" + fileFormatExt[oleObject.FileFormat], FileMode.Create))
11 {
12 stream.Write(oleObject.Content, 0, oleObject.Content.Length);
13 }
14 }
15}