使用 Office365 在 Sharepoint 中复制文件-REST-Python-Client

Copy file in Sharepoint using Office365-REST-Python-Client

我正在使用这个基于 this github link 的好工具。

如标题所示,如何将文件从一个 SharePoint 文件夹复制到另一个文件夹? 我在官方文档中找不到任何示例。

谢谢!

SharePoint 有一个 CreateCopyJobs API,您可以利用它来复制或移动文件。有关详细信息,请参阅链接的博客。

https://blog.velingeorgiev.com/createcopyjobs-api-copy-move-SharePoint-files-folders

可以构造 REST 查询来实现此目的,执行类似于以下的操作。

import json
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
from office365.runtime.http.http_method import HttpMethod
site_url = "https://{your-tenant-prefix}.sharepoint.com"


client = ClientContext("site_url").with_credentials(UserCredential("{username}", "{password}"))
request = RequestOptions("{0}/sites/_api/site/CreateCopyJobs".format(site_url))
request.method = HttpMethod.Post
request.data = { 
    "exportObjectUris":[ 
    "https://{your-tenant-prefix}.sharepoint.com/sites/site1/Shared%20Documents/Test.docx"
    ],
    "destinationUri":"https://{your-tenant-prefix}.sharepoint.com/sites/site2/Shared%20Documents",
    "options":{ 
    "IgnoreVersionHistory":true,
    "IsMoveMode":false
    }
}
response = client.execute_request_direct(request)

response.raise_for_status()
output = json.loads(response.content)
output = output['d']