Writing Updated Extended Attribute Definitions and Values to MPP

Skip to end of metadata
Go to start of metadata
This page is no longer maintained. For an updated page, please visit Working with Extended Attributes of a Project
Aspose.Tasks supports updating extended attribute data in a Microsoft Project MPP file and save it back.

Updating Extended Attribute Definitions and Values

The example code below adds new extended attributes of the Resource and Task types to the source MPP file. The steps involved in this activity are:

  1. Create an instance of the Project Reader.
  2. Read the source MPP file.
  3. Define a new extended attribute and update its values.
  4. Save the project using the Project Writer.

Programming Samples

The following example shows setting the extended attributes of a resource.

C#
string path = "";
string newFile = path + "Blank2010.mpp";
string resultFile = path + "OutputMPP.mpp";

Project project = new Project(newFile);

#region task attributes
// Add new text3 extended attribute and one text value
ExtendedAttributeDefinition taskTextAttr = new ExtendedAttributeDefinition();
taskTextAttr.Alias = "New text3 attribute";
taskTextAttr.FieldName = "Text3";
taskTextAttr.ElementType = ElementType.Task;
taskTextAttr.CfType = CustomFieldType.Text;
taskTextAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Text3).ToString();
taskTextAttr.LookupUid = Guid.NewGuid().ToString();
project.ExtendedAttributes.Add(taskTextAttr);

Value textVal = new Value();
textVal.Id = 1;
textVal.Description = "Text value descr";
textVal.Val = "Text value1";
taskTextAttr.ValueList.Add(textVal);

// Add new cost1 extended attribute and two cost values
ExtendedAttributeDefinition taskCostAttr = new ExtendedAttributeDefinition();
taskCostAttr.Alias = "New cost1 attribute";
taskCostAttr.FieldName = "Cost1";
taskCostAttr.ElementType = ElementType.Task;
taskCostAttr.CfType = CustomFieldType.Cost;
taskCostAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Cost1).ToString();
taskCostAttr.LookupUid = Guid.NewGuid().ToString();
project.ExtendedAttributes.Add(taskCostAttr);

Value costVal1 = new Value();
costVal1.Id = 2;
costVal1.Description = "Cost value 1 descr";
costVal1.Val = "99900";

Value costVal2 = new Value();
costVal2.Id = 3;
costVal2.Description = "Cost value 2 descr";
costVal2.Val = "11100";

taskCostAttr.ValueList.Add(costVal1);
taskCostAttr.ValueList.Add(costVal2);

// Add new task and assign attribute value
Aspose.Tasks.Task task = project.RootTask.Children.Add("New task");

ExtendedAttribute taskAttr = new ExtendedAttribute();
taskAttr.AttributeDefinition = taskCostAttr;
taskAttr.Value = "99900";
taskAttr.FieldId = taskCostAttr.FieldId;
task.ExtendedAttributes.Add(taskAttr);

ExtendedAttributeDefinition taskStartAttr = new ExtendedAttributeDefinition();
taskStartAttr.Alias = "New start 7 attribute";
taskStartAttr.CfType = CustomFieldType.Start;
taskStartAttr.ElementType = ElementType.Task;
taskStartAttr.FieldName = "Start7";
taskStartAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Start7).ToString();
taskStartAttr.LookupUid = Guid.NewGuid().ToString();
Value startVal = new Value();
startVal.Val = DateTime.Now.ToString();
startVal.Description = "Start 7 value description";

taskStartAttr.ValueList.Add(startVal);

project.ExtendedAttributes.Add(taskStartAttr);

ExtendedAttributeDefinition taskFinishAttr = new ExtendedAttributeDefinition();
taskFinishAttr.Alias = "New finish 4 attribute";
taskFinishAttr.CfType = CustomFieldType.Finish;
taskFinishAttr.ElementType = ElementType.Task;
taskFinishAttr.FieldName = "Finish4";
taskFinishAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Finish4).ToString();
taskFinishAttr.LookupUid = Guid.NewGuid().ToString();
Value finishVal = new Value();
finishVal.Val = DateTime.Now.ToString();
finishVal.Description = "Finish 4 value description";

taskFinishAttr.ValueList.Add(finishVal);

project.ExtendedAttributes.Add(taskFinishAttr);

ExtendedAttributeDefinition numberAttr = new ExtendedAttributeDefinition();
numberAttr.Alias = "New number attribute";
numberAttr.FieldName = "Number20";
numberAttr.CfType = CustomFieldType.Number;
numberAttr.ElementType = ElementType.Task;
numberAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Number20).ToString();
numberAttr.LookupUid = Guid.NewGuid().ToString();
Value val1 = new Value();
val1.Val = "1";
val1.Description = "Number 1 value";
Value val2 = new Value();
val2.Val = "2";
val2.Description = "Number 2 value";
Value val3 = new Value();
val3.Val = "3";
val3.Description = "Number 3 value";

numberAttr.ValueList.Add(val1);
numberAttr.ValueList.Add(val2);
numberAttr.ValueList.Add(val3);

project.ExtendedAttributes.Add(numberAttr);

#endregion

ExtendedAttributeDefinition rscStartAttr = new ExtendedAttributeDefinition();
rscStartAttr.Alias = "New start5 attribute";
rscStartAttr.FieldName = "Start5";
rscStartAttr.ElementType = ElementType.Resource;
rscStartAttr.CfType = CustomFieldType.Start;
rscStartAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Start5).ToString(); ;
rscStartAttr.LookupUid = Guid.NewGuid().ToString();
Value startVal2 = new Value();
startVal2.Id = 4;
startVal2.Val = DateTime.Now.ToString();
startVal2.Description = "this is start5 value descr";

rscStartAttr.ValueList.Add(startVal2);

project.ExtendedAttributes.Add(rscStartAttr);


//Save the project as MPP project file
project.Save(resultFile, Aspose.Tasks.Saving.SaveFileFormat.MPP);

VB.NET
Dim path As String = “”
Dim newFile As String = path & Convert.ToString("Blank2010.mpp")
Dim resultFile As String = path & Convert.ToString("OutputMPP.mpp")

Dim project As New Project(newFile)

' Add new text3 extended attribute and one text value
Dim taskTextAttr As New ExtendedAttributeDefinition()
taskTextAttr.[Alias] = "New text3 attribute"
taskTextAttr.FieldName = "Text3"
taskTextAttr.ElementType = ElementType.Task
taskTextAttr.CfType = CustomFieldType.Text
taskTextAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Text3).ToString()
taskTextAttr.LookupUid = Guid.NewGuid().ToString()
project.ExtendedAttributes.Add(taskTextAttr)

Dim textVal As New Value()
textVal.Id = 1
textVal.Description = "Text value descr"
textVal.Val = "Text value1"
taskTextAttr.ValueList.Add(textVal)

' Add new cost1 extended attribute and two cost values
Dim taskCostAttr As New ExtendedAttributeDefinition()
taskCostAttr.[Alias] = "New cost1 attribute"
taskCostAttr.FieldName = "Cost1"
taskCostAttr.ElementType = ElementType.Task
taskCostAttr.CfType = CustomFieldType.Cost
taskCostAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Cost1).ToString()
taskCostAttr.LookupUid = Guid.NewGuid().ToString()
project.ExtendedAttributes.Add(taskCostAttr)

Dim costVal1 As New Value()
costVal1.Id = 2
costVal1.Description = "Cost value 1 descr"
costVal1.Val = "99900"

Dim costVal2 As New Value()
costVal2.Id = 3
costVal2.Description = "Cost value 2 descr"
costVal2.Val = "11100"

taskCostAttr.ValueList.Add(costVal1)
taskCostAttr.ValueList.Add(costVal2)

' Add new task and assign attribute value
Dim task As Aspose.Tasks.Task = project.RootTask.Children.Add("New task")

Dim taskAttr As New ExtendedAttribute()
taskAttr.AttributeDefinition = taskCostAttr
taskAttr.Value = "99900"
taskAttr.FieldId = taskCostAttr.FieldId
task.ExtendedAttributes.Add(taskAttr)

Dim taskStartAttr As New ExtendedAttributeDefinition()
taskStartAttr.[Alias] = "New start 7 attribute"
taskStartAttr.CfType = CustomFieldType.Start
taskStartAttr.ElementType = ElementType.Task
taskStartAttr.FieldName = "Start7"
taskStartAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Start7).ToString()
taskStartAttr.LookupUid = Guid.NewGuid().ToString()
Dim startVal As New Value()
startVal.Val = DateTime.Now.ToString()
startVal.Description = "Start 7 value description"

taskStartAttr.ValueList.Add(startVal)

project.ExtendedAttributes.Add(taskStartAttr)

Dim taskFinishAttr As New ExtendedAttributeDefinition()
taskFinishAttr.[Alias] = "New finish 4 attribute"
taskFinishAttr.CfType = CustomFieldType.Finish
taskFinishAttr.ElementType = ElementType.Task
taskFinishAttr.FieldName = "Finish4"
taskFinishAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Finish4).ToString()
taskFinishAttr.LookupUid = Guid.NewGuid().ToString()
Dim finishVal As New Value()
finishVal.Val = DateTime.Now.ToString()
finishVal.Description = "Finish 4 value description"

taskFinishAttr.ValueList.Add(finishVal)

project.ExtendedAttributes.Add(taskFinishAttr)

Dim numberAttr As New ExtendedAttributeDefinition()
numberAttr.[Alias] = "New number attribute"
numberAttr.FieldName = "Number20"
numberAttr.CfType = CustomFieldType.Number
numberAttr.ElementType = ElementType.Task
numberAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Number20).ToString()
numberAttr.LookupUid = Guid.NewGuid().ToString()
Dim val1 As New Value()
val1.Val = "1"
val1.Description = "Number 1 value"
Dim val2 As New Value()
val2.Val = "2"
val2.Description = "Number 2 value"
Dim val3 As New Value()
val3.Val = "3"
val3.Description = "Number 3 value"

numberAttr.ValueList.Add(val1)
numberAttr.ValueList.Add(val2)
numberAttr.ValueList.Add(val3)

project.ExtendedAttributes.Add(numberAttr)


Dim rscStartAttr As New ExtendedAttributeDefinition()
rscStartAttr.[Alias] = "New start5 attribute"
rscStartAttr.FieldName = "Start5"
rscStartAttr.ElementType = ElementType.Resource
rscStartAttr.CfType = CustomFieldType.Start
rscStartAttr.FieldId = Convert.ToInt32(ExtendedAttributeTask.Start5).ToString()


rscStartAttr.LookupUid = Guid.NewGuid().ToString()
Dim startVal2 As New Value()
startVal2.Id = 4
startVal2.Val = DateTime.Now.ToString()
startVal2.Description = "this is start5 value descr"

rscStartAttr.ValueList.Add(startVal2)

project.ExtendedAttributes.Add(rscStartAttr)


'Save the project as MPP project file
project.Save(resultFile, Aspose.Tasks.Saving.SaveFileFormat.MPP)
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.