You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用SharePoint REST API为Project Online创建自定义字段时设置指定EntityType

解决PWA REST API创建自定义字段时无法设置EntityType的问题

核心解决方案

创建自定义字段时,不要使用EntityType属性,而是通过EntityTypeUid指定目标实体类型的GUID。这是因为PWA REST API在创建自定义字段时,仅接受实体类型的唯一标识符(UID),而非名称或对象。

正确请求示例(方案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,
    "EntityTypeUid": "00000000-0000-0000-0000-000000000001"
}

常用实体类型UID对应表

实体类型UID
Task00000000-0000-0000-0000-000000000001
Project00000000-0000-0000-0000-000000000002
Resource00000000-0000-0000-0000-000000000003
Assignment00000000-0000-0000-0000-000000000004

问题原因分析

  1. 方案1失效原因:EntityType属性在PWA REST API中属于只读属性,创建自定义字段时无法通过该属性指定实体类型,因此即使请求返回201,实际会默认设置为Project。
  2. 方案2报错原因:PS.CustomFieldCreationInformation类型确实没有EntityType属性,你参考的官方文档是针对Project JavaScript API的,REST API的参数定义存在差异,同样需要使用EntityTypeUid。
  3. 无法修改EntityType:自定义字段创建后,EntityType属性不可变更,因此PUT/MERGE操作无法生效。

补充说明

  • 确保FieldType值正确:你使用的21对应文本类型(Text),如果需要其他类型可参考官方文档的字段类型枚举值。
  • 若使用CustomFields/Add方法,同样需要在parameters中传入EntityTypeUid而非EntityType,示例如下:
POST https://tenantname.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields/Add
Content-Type: application/json;odata=verbose

{
    "parameters": {
        "__metadata": {"type": "PS.CustomFieldCreationInformation"},
        "Name": "Custom Field1",
        "Description": "A Custom Field",
        "FieldType": 21,
        "EntityTypeUid": "00000000-0000-0000-0000-000000000001"
    }
}

内容的提问来源于stack exchange,提问作者Scott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 18:37:30