1

我尝试使用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 创建它?

谢谢!

4

1 回答 1

1

我的回答会有点长,因为我会尽可能清楚地解释一切。

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

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

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

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

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-07-05T17:07:26.807 回答