【问题标题】:How to create a new content type in SharePoint using MS Graph / REST?如何使用 MS Graph / REST 在 SharePoint 中创建新的内容类型?
【发布时间】:2020-09-02 15:29:16
【问题描述】:

我尝试使用 MS Graph Explorer 将新的内容类型添加到列表中:

请求:

POST https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/contenttypes

与正文:

{
  "description": "MyCustomContentType's description",
  "group": "List Content Types",
  "hidden": false,
  "id": "0x010300B8123BA6FE3D6045BF4F6DF992B6ABE7",
  "name": "MyCustomContentType",
  "parentId": "0x0103",
  "readOnly": false,
  "sealed": false
}

回复:

{
    "error": {
        "code": "itemNotFound",
        "message": "The specified site content type was not found",
        "innerError": {
            "request-id": "1ac12fed-eaf3-4d03-a3c4-b44ddacada72",
            "date": "2020-05-16T17:12:11"
        }
    }
}

还尝试在 Java 代码中使用 Graph API sdk

IGraphServiceClient graphClient = GraphServiceClient.builder()
        .authenticationProvider(authenticationProvider)
        .buildClient();

ContentType contentType = new ContentType();
contentType.name = "MyCustomContentType";
contentType.description = "MyCustomContentType's description";
contentType.group = "List Content Types";
contentType.hidden = false;
contentType.parentId = "0x0103";
contentType.id = "0x010300B8123BA6FE3D6045BF4F6DF992B6ABE7";
contentType.readOnly = false;
contentType.sealed = false;

contentType = graphClient.sites(siteId)
        .lists(listId)
        .contentTypes()
        .buildRequest()
        .post(contentType);

结果是一样的……

我还尝试使用 REST API 将内容类型添加到列表中,但遇到了另一个问题:创建了内容类型但它忽略了传递的 id 并且总是从 Item 内容继承类型。此处描述的相同问题:How to create site content type with id using REST API。这似乎是 REST API 的错误。

是否可以使用 MS Graph 或 REST API 在 SharePoint 中创建内容类型?也许还有其他方法可以使用 Java 创建它?

谢谢!

【问题讨论】:

  • 我还被 REST 方法中的 ID 错误困扰,现在无法再通过 CSOM 执行此操作,因为它只支持旧版身份验证,并且逐渐被禁用。还没有尝试过图表,但看看你的例子,我想问题可能是你试图直接在列表上创建内容类型。而是尝试在站点级别 (/sites/{site-id}/contenttypes) 创建它,然后再进行一次调用以将站点内容类型与列表相关联。

标签: java microsoft-graph-api microsoft-graph-sdks contenttype sharepoint-rest-api


【解决方案1】:

我的回答会有点长,因为我会尽量解释清楚。

首先,内容类型的创建请求必须作为 POST 请求发送到sites/{siteId}/contenttypes 端点。 主体应该是一个包含属性的对象:名称、描述、组和基础。 前三个属性(名称、描述和组)是不言自明的,而 Base 属性只是父 Content-Type 的引用对象。 在 Base 属性中,您应该指定父内容类型的 Id,还可以选择指定名称 这是官方 MS 文档的链接:Create Content Type - MS

作为基本项目,您可以使用许多预定义的选项(内容类型)。请注意,您还可以将新创建​​的 Content-Types 作为基本类型。我的建议是您向 https://graph.microsoft.com/beta/sites/{siteId}/contentTypes端点,然后查看结果。

从您将获得的结果中,您应该看到所有可以用作父引用的 Content-Type。 为了更好地理解内容类型,我建议您阅读此链接中的内容类型 Id-s MS Docs - Content Type Ids

总结一下,你的请求应该是这样的:

POST: https://graph.microsoft.com/beta/sites/{siteId}/contentTypes

{
    "name": "Your Content Type Name",
    "description": "Description for your content type",
    "base": {
        "name": "Name of the parent content type",
        "id": "0x0120D520" //Id of the parent content type
    },
    "group": "Your Content Type Group" 
}

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 2020-03-28
    • 2021-11-03
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多