Simplest Code for adding Extended Attribute to a Project

Hi,


Can anyone please guide how to create a simple Extended Attribute to a Project using this API? Any recommendations?

Hi Robert,


Thank you for writing to Aspose Support team.

Aspose.Tasks lets you create Extended attributes in a project and assign it to a Task or Resource. Following is an example of how to create an Extended Attribute definition, create an Extended attribute from it and add it to a task. Please try it at your end and let us know if you need any further information in this regard.

Sample Code:

Project proj = new Project(“Blank2010.mpp”);

ExtendedAttributeDefinition myNumber1 = null;

if (proj.ExtendedAttributes.GetById(Convert.ToInt32(ExtendedAttributeTask.Number1.ToString(“D”))) == null)
{
myNumber1 = new ExtendedAttributeDefinition();
myNumber1.Alias = “MyValue”;
myNumber1.CfType = CustomFieldType.Number;
myNumber1.FieldId = ExtendedAttributeTask.Number1.ToString(“D”);
proj.ExtendedAttributes.Add(myNumber1);
}
else
myNumber1 = proj.ExtendedAttributes.GetById(Convert.ToInt32(ExtendedAttributeTask.Number1.ToString(“D”)));

ExtendedAttribute Number1Task = myNumber1.CreateExtendedAttribute();
Number1Task.Value = “20.55”;

Aspose.Tasks.Task tsk = proj.RootTask.Children.Add(“Task 1”);

tsk.ExtendedAttributes.Add(Number1Task);

proj.Save(“Task-ExtAttr.mpp”, SaveFileFormat.MPP);

Hi,


Thanks.

How can I generate an extended attribute with Lookup value? I don’t see any such example in documentation.

Hi Robert,


For an extended attribute of Lookup type, you need to define OutlineCodes that will be used to generate an extended attribute of this type. Please have a look at it for your reference and let us know your feedback.

String path = “\”;
Project project = new Project(@“Blank2010.mpp”);
OutlineCodeDefinition textOutline = new OutlineCodeDefinition();
project.OutlineCodes.Add(textOutline);

// add a mask for text field
OutlineMask mask = new OutlineMask();
mask.Type = MaskType.Characters;
textOutline.Masks.Add(mask);

OutlineValue textValue = new OutlineValue();

textValue.Value = “Text value 1”;
textValue.ValueId = 1;
textValue.Type = OutlineValueType.Text;
textValue.Description = “Text value descr 1”;
textValue.FieldGuid = Guid.NewGuid().ToString().ToUpper();
textOutline.Values.Add(textValue);

OutlineValue textValue2 = new OutlineValue();

textValue2.Value = “Text value 2”;
textValue2.ValueId = 2;
textValue2.Type = OutlineValueType.Text;
textValue2.Description = “Text value descr 2”;
textValue2.FieldGuid = Guid.NewGuid().ToString().ToUpper();
textOutline.Values.Add(textValue2);

// Add new text27 extended attribute
ExtendedAttributeDefinition taskTextAttr = new ExtendedAttributeDefinition();
taskTextAttr.ElementType = ElementType.Null;
taskTextAttr.Alias = “New text27 attribute”;
taskTextAttr.FieldId = ExtendedAttributeTask.Text27.ToString(“D”);
// add a reference to lookup table
taskTextAttr.LookupUid = textOutline.Guid;

taskTextAttr.CfType = CustomFieldType.Text;
project.ExtendedAttributes.Add(taskTextAttr);

// Add new task and assign attribute value
Aspose.Tasks.Task task = project.RootTask.Children.Add(“New task”);
task.Set(Tsk.Start, new DateTime(2016, 7, 4, 8, 0, 0));
task.Set(Tsk.Duration, project.GetDuration(8, TimeUnitType.Hour));

ExtendedAttribute taskAttrText = taskTextAttr.CreateExtendedAttribute(textValue);
task.ExtendedAttributes.Add(taskAttrText);

project.Save(path + “SavedProject.mpp”, SaveFileFormat.MPP);