Reputation: 2116
I'm using this nice tool, based at this github link.
As in the title, how can I copy a file from one SharePoint folder to another? I cannot find any example in the official documentation.
thank you!
Upvotes: 0
Views: 3763
Reputation: 71
Before v 2.4.2, the following codes can be used to copy the file called Test.txt from one folder to another.
source_file_url = '/sites/<site name>/Shared Documents/<source folder name>/Test.txt'
source_file = client_context.web.get_file_by_server_relative_url(source_file_url)
target_file_url = '/sites/<site name>/Shared Documents/<target folder name>/Test.txt'
source_file.copyto(target_file_url, True).execute_query()
From v 2.4.2 onwards, things are different but an example can be found here.
Upvotes: 0
Reputation: 186
SharePoint has a CreateCopyJobs
API which you can leverage to copy or move the files. See the linked blog for more details.
https://blog.velingeorgiev.com/createcopyjobs-api-copy-move-SharePoint-files-folders
It is possible to construct the REST queries to achieve this, doing something similar to the below.
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']
Upvotes: 1