Free Support Forum - aspose.com

How to create, write and read in custom field in the task?

Hello,
is it possible to create a custom extended attribute for the task?

I’ve seen these topics:
https://forum.aspose.com/t/aspose-tasks-add-new-table-field
https://forum.aspose.com/t/add-a-extendedattribute-and-make-it-visible-in-msproject
https://forum.aspose.com/t/custom-fields-in-tasks
https://forum.aspose.com/t/how-to-read-write-custom-attribute-in-mpp-file

I need to create a read-only field, to identify the record in my system (Id).

Could you generate a sample code?

Thank you

1 Like

@FabioMartins,

Thank you for writing to Aspose support team again. I have checked the documents but could not find any feature which can be used to create read only Extended Attribute for a task. We also couldn’t find how to achieve the same using Microsoft Project (MSP). Could you please help us identify how can one achieve the same using MSP? This will help us investigate the possible implementation of the same using Aspose.Tasks API.

Hi @kashif.iqbal,
thanks for the feedback. I also do not know this functionality in the MSP, I do not think this definition will be possible.

I’m testing the custom attributes. Is it possible to find a task by the value of the attribute?

    License license = new License();
    license.SetLicense("Aspose.Tasks.lic");
    Project project = new Project("Template.mpp");

    // Customize table by adding text attribute field
    TableField attrField = new TableField();
    attrField.Field = Field.TaskText10;
    attrField.Width = 20;
    attrField.Title = "New Id";

    Aspose.Tasks.Table table = project.Tables.ToList()[0];
    table.TableFields.Insert(table.TableFields.Count, attrField);

    Task task = project.RootTask.Children.Add("New Activity 1");
    ExtendedAttribute attr = new ExtendedAttribute();
    attr.FieldId = ((int)ExtendedAttributeTask.Text10).ToString();
    attr.Value = "98484";
    task.ExtendedAttributes.Add(attr);

    Task task2 = project.RootTask.Children.Add("New Activity 2");
    ExtendedAttribute attr2 = new ExtendedAttribute();
    attr2.FieldId = ((int)ExtendedAttributeTask.Text10).ToString();
    attr2.Value = "98483";
    task2.ExtendedAttributes.Add(attr2);

    /*Test Set new value - Linq */
    task2.ExtendedAttributes.Where(x => x.FieldId ==
                                        ((int)ExtendedAttributeTask.Text10).ToString()).FirstOrDefault().Value = "98485";

    /*Test Get values - Linq */
    Console.WriteLine("Tarefa: " + task.ExtendedAttributes.Where(x => x.FieldId ==
                                        ((int)ExtendedAttributeTask.Text10).ToString()).FirstOrDefault().Value.ToString());

    Console.WriteLine("Tarefa: " + task2.ExtendedAttributes.Where(x => x.FieldId ==
                            ((int)ExtendedAttributeTask.Text10).ToString()).FirstOrDefault().Value.ToString());

    /*Locate Task - New Id - Linq */
    //var filteredTask = project.SelectAllChildTasks().Where(x => x.Get(Tsk.Text10) == "98485").FirstOrDefault();

    /*Save*/
    MPPSaveOptions mppSaveOptions = new MPPSaveOptions();
    mppSaveOptions.WriteViewData = true;
    project.Save("Project1.mpp", mppSaveOptions);
    project.Save("Project1.mpp", Aspose.Tasks.Saving.SaveFileFormat.MPP);

@FabioMartins,

I have also checked and found that it is not possible to create read only field in MSP and hence same is the case with Aspose.Tasks API. Therefor it is not possible to create read only field using Aspose.Tasks.

You may please give a try to the following sample code which uses the output mpp file created by the above sample code and displays task names for those having extended attribute value say “98484”.

Project project1 = new Project(@"Project1.mpp");
string TargetValue = "98484";
foreach (Task tsk in project1.RootTask.Children)
{
    var str = (int)Aspose.Tasks.Field.TaskText10;
    var str2 = tsk.ExtendedAttributes[0].FieldId;
    var value = (from data in tsk.ExtendedAttributes
                    where data.AttributeDefinition.FieldId == ((int)Aspose.Tasks.Field.TaskText10).ToString()
                    select data.Value).FirstOrDefault();
    if(value != null)
    {
        if(value == TargetValue)
            Console.WriteLine(tsk.Get(Tsk.Name));
    }
}

It worked!
Thank you so much!

@FabioMartins,

You are welcome.