【问题标题】:How to create multiple items at once in Sharepoint using REST API?如何使用 REST API 在 Sharepoint 中一次创建多个项目?
【发布时间】:2020-11-06 12:57:28
【问题描述】:

我尝试使用这个身体:

{
    "value": [
        {
            "fields": {
                "field1": "1",
                "field2": "11",
                "field3": "111"
            }
        },
        {
            "fields": {
                "field1": "2",
                "field2": "12",
                "field3": "112"
            }
        }
    ]
}

我得到了Status Code 201,尽管它只创建了一个带有空数据的项目。

我的代码:

import requests

url = "https://graph.microsoft.com/v1.0/sites/<mysite>/lists/<mylist>/items?expand=fields"

payload = "{\r\n    \"value\": [\r\n        {\r\n            \"fields\": {\r\n                \"UserId\": \"1\",\r\n                \"ListId\": \"11\",\r\n                \"Fixture_x0020_Id\": \"111\"\r\n            }\r\n        },\r\n        {\r\n            \"fields\": {\r\n                \"UserId\": \"2\",\r\n                \"ListId\": \"12\",\r\n                \"Fixture_x0020_Id\": \"112\"\r\n            }\r\n        }\r\n    ]\r\n}"
headers = {
  'Authorization': 'Bearer $accessToken',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

在 Microsoft 官方文档中找不到与批量插入相关的内容。 Official Documentation

【问题讨论】:

标签: rest sharepoint microsoft-graph-api


【解决方案1】:

它使用批处理请求工作:

https://graph.microsoft.com/v1.0/$batch发送POST请求

Official Documentation - Combine multiple requests in one HTTP call using JSON batching

curl --location --request POST 'https://graph.microsoft.com/v1.0/$batch' \
--header 'Authorization: Bearer $access_token' \
--header 'Content-Type: application/json' \
--data-raw '{
    "requests": [
        {
            "id": "1",
            "method": "POST",
            "url": "/sites/<mysite>/lists/<mylist>/items",
            "body": {
                "fields": {
                    "UserId": "1",
                    "ListId": "11",
                    "Fixture_x0020_Id": "111"
                }
            },
            "headers": {
                "Content-Type": "application/json"
              }
        },
        {
            "id": "2",
            "method": "POST",
            "url": "/sites/<mysite>/lists/<mylist>/items",
            "body": {
                "fields": {
                    "UserId": "133",
                    "ListId": "1123",
                    "Fixture_x0020_Id": "1242411"
                }
            },
            "headers": {
                "Content-Type": "application/json"
              }
        },
      ...,
      {
            "id": "4",
            "method": "POST",
            "url": "/sites/<mysite>/lists/<mylist>/items",
            "body": {
                "fields": {
                    "UserId": "1DSDS",
                    "ListId": "1464651",
                    "Fixture_x0020_Id": "1JGMJGMG11"
                }
            },
            "headers": {
                "Content-Type": "application/json"
              }
        }
    ]
}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-24
    • 2014-05-30
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多