有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用MS Graph/REST在SharePoint中创建新的内容类型?

我试图使用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,并且总是从内容类型继承。这里描述了同样的问题:How to create site content type with id using REST API。这似乎是REST API的一个缺陷

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

谢谢


共 (1) 个答案

  1. # 1 楼答案

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

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

    作为基本项,有许多预定义的选项(内容类型)可供使用。注意:您还可以将新创建的内容类型作为基本类型。我的建议是你打一个GET请求电话给 https://graph.microsoft.com/beta/sites/{siteId}/contentTypes端点,然后查看结果

    从您将得到的结果中,您应该看到所有可以用作父引用的内容类型。 为了更好地理解内容类型,我建议您在这个链接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" 
    }