I am trying to upload a temp XML file that I created into Sharepoint using Python Script. Though I don't see any error when I run the code however the file was not uploaded into Sharepoint folder. Can you help me out.
# create XML attachment as temp file
tempdir = tempfile.mkdtemp()
path = os.path.join(tempdir)
temp_file = path + f'/XML_REPORT_{str(uuid.uuid4())}.xml'
with open(temp_file, 'wb') as fp:
fp.write(tree_string)
print('temp file has been created')
base_path = 'https://company_name.sharepoint.com'
site_name = 'Testing'
doc_library = 'Shared%20Documents/General'
# Obtain auth cookie
authcookie = Office365(base_path, username="[email protected]", password="abc123").GetCookies()
session = requests.Session()
session.cookies = authcookie
session.headers.update({'user-agent': 'python_bite/v1'})
session.headers.update({'accept': 'application/json;odata=verbose'})
with open(temp_file, 'rb') as content_file:
try:
response = session.post(
url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='"
+ temp_file + "',overwrite=true)",data=content_file)
print('file added successfully')
except Exception as err:
print("Some error occurred: " + str(err))
This is the url of my sharepoint https://company_name.sharepoint.com/sites/Testing/Shared%20Documents/Forms/AllItems.aspx/?id=%2Fsites%2FTesting%2FShared%20Documents%2FGeneral&viewid=6357a30d%2D8562%2D4a01%2Dade1%2Dc1c413193931 I've already created a folder in it named General...
Thank you for answering my question.
According to my research and testing, please refer to the following document to upload file to SharePoint: https://github.com/iamlu-coding/python-upload2sharepoint
Hope it can help you. Thanks for your understanding.