sharepointodatams-projectms-project-server-2013project-online

Setting desired EntityType on custom field creation using sharepoints REST API for Project Online


I've been struggling with the creation Custom Fields for Microsoft Project Web App using Sharepoints REST API. To sum it up, I want to create a couple of custom fields for ease of set up before deploying our product. I manage to create the Custom Fields, although I cannot set them to the correct Entity type.

I've tried a few different routes:

Option 1:

POST https://tenantname.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields
Content-Type: application/json;odata=verbose

{
    "__metadata": {"type": "PS.CustomField"},
    "Description": "A Custom Field",
    "Name": "Custom Field 1",
    "FieldType":21,        
    "EntityType":{
        "__metadata":{
            "type":"PS.EntityType"
        },
        "Name":"Task"
    }
}

This returns a 201, in which the EntityType is set to "Project" instead of "Task".

Option 2:

POST https://tenantname.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields/Add
Content-Type: application/json;odata=verbose

{
    "parameters": {
        "Name": "Custom Field1",
        "Description": "A Custom Field",
        "FieldType": 21,
        "EntityType": {
            "Name": "Task"
        }
    }
}

This one returns an error message stating: "The property 'EntityType' does not exist on type 'PS.CustomFieldCreationInformation'. Make sure to only use property names that are defined by the type."

This confuses me, as per the documentation the CustomFieldCreationInformation should have the property "EntityType". ( https://learn.microsoft.com/en-us/previous-versions/office/project-javascript-api/jj668876(v=office.15) ) (I've also tried different capitalizations of entityType in the payload) If I skip the EntityType property, it returns a 201 with no problem, so the endpoint at least works.

I've generally tried different variations of these calls, such as trying to use EntityType as a primitive value and set it as "Task" directly, I've tried content-type: application/json;odata=nometadata as well as application/atom+xml, and I've tried PUT and MERGE calls after the creation to change the entity type instead, to no avail.

I simply cannot get it to work, and I can't find any documentation on whether I am doing it incorrectly, or if its just not possible.

Thanks.


Solution

  • I managed to find this documentation on GitHub, which states that EntityType is not writeable using REST, but that there's a property called EntityTypeId which is writeable using REST.

    EntityTypeId consists of a GUID, which can be found at https://tenantname.sharepoint.com/sites/pwa/_api/ProjectServer/EntityTypes/TaskEntity for "Task", .../ProjectEntity for "Project" and .../ResourceEntity for "Resource".

    So, after fetching said GUID, we can post this:

    POST https://tenantname.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields/Add
    Content-Type: application/json;odata=verbose
    
    {
        "parameters": {
            "Name": "Custom Field1",
            "Description": "A Custom Field",
            "FieldType": 21,
            "EntityTypeId": "GUID-ACQUIRED-FROM-EARLIER"
        }
    }
    

    I hope this helps someone else in the future!