How to check if an Extended Attribute exists

Hi,


I am new to the API and want to know how to check if an Extended Attribute already exists in a project file? Is there any such way to find? If yes, I would like to read the value of existing attribute.

Hi Robert,

Thank you for contacting Aspose support team.

Following sample code creates a text3 extended attribute for a task and reads back this text3 attribute from the project. Please give it a try and share the feedback.

Project project = new Project();
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 text3 extended attribute
ExtendedAttributeDefinition taskTextAttr = new ExtendedAttributeDefinition();
taskTextAttr.Alias = "New text3 attribute";
taskTextAttr.FieldId = ExtendedAttributeTask.Text3.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
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();
// add a reference to lookup table value
taskAttrText.ValueGuid = textValue2.FieldGuid;
task.ExtendedAttributes.Add(taskAttrText);
ChildTasksCollector collector = new ChildTasksCollector(); // Create a ChildTasksCollector instance
TaskUtils.Apply(project.RootTask, collector, 0); // Collect all the tasks from RootTask using TaskUtils
foreach (Task tsk in collector.Tasks)
{
    if (tsk.Get(Tsk.Id) == 1)
    {
        ExtendedAttribute extAttrib = tsk.ExtendedAttributes.Where(ext => ext.AttributeDefinition.FieldName == "Text3").FirstOrDefault();
        OutlineCodeDefinition OutlineDef = project.OutlineCodes.Where(def => def.Guid == extAttrib.AttributeDefinition.LookupUid).FirstOrDefault();
        OutlineValue OutlineVal = OutlineDef.Values.Where(val => val.FieldGuid == extAttrib.ValueGuid).FirstOrDefault();
        Console.WriteLine("Value Id = {0}, \nValue = {1}\nDescription = {2}", OutlineVal.ValueId, OutlineVal.Value, OutlineVal.Description);
    }
}