0

I am struggeling to copy a file from one sharepoint site to another. Lets say i want to copy from: https://test.sharepoint.com/Site1/A/ to : https://test.sharepoint.com/Site2/B/

i have not yet found a solution or helpfull thing online for my problem. Perhaps anyone here can point me in the right direction, or help me out.

I believe the copyto only works in the same site, but i want to copy the file to another site...

New contributor
mat st is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

0

For those looking for an anwser to this, i found it:

def copy_between_sites(username, password, site_url, origin_url, destination_url):
    # copy a file from one location in sharepoint to another location, can be in a different site

    origin = str(site_url + origin_url).replace(' ', '%20')
    destination = str(site_url + destination_url).replace(' ', '%20')
    destination = destination.rstrip(destination[-1])
    auth = AuthenticationContext(site_url)

    if auth.acquire_token_for_user(username, password):

        request = ClientContext(site_url, auth)
        options = RequestOptions("{0}/sites/<your site>/_api/site/CreateCopyJobs".format(site_url))
        options.method = HttpMethod.Post
        options.data = {
        "exportObjectUris": [
            origin
        ],
        "destinationUri": destination,
        "options": {
            "IgnoreVersionHistory": True,
            "IsMoveMode": False,
            "BypassSharedLock": True
                    }
        }
        options.headers = {
            "X-RequestDigest": str(getToken(username=username_shrpt, password=password_shrpt, site_url=origin))
        }
        data = request.execute_request_direct(options)
        output = json.loads(data.content)
    else:
        print(auth.get_last_error())
    return

def getToken(username, password, site_url):
    # get the digest value from the site, for write access

    contextinfo_api = site_url + "/_api/contextinfo"
    auth = AuthenticationContext(site_url)

    if auth.acquire_token_for_user(username,password):

        request = ClientContext(site_url, auth)
        options = RequestOptions(contextinfo_api)
        options.method = HttpMethod.Post

        data = request.execute_request_direct(options)
        output = json.loads(data.content)

        digest_value = output['d']['GetContextWebInformation']['FormDigestValue']

    else:
        print(auth.get_last_error())

    return digest_value
New contributor
mat st is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

mat st is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.