Different Ways to Upload Document on Sharepoint
question
Means to access/upload documents to SharePoint sites/Document Library using Python script
Howdy,
I am new to SharePoint, and trying to upload documents to SharePoint document library. Could you lot please suggest on how to handle uploading documents to SharePoint using Python.
So far, signed upwards for 1 month trail SharePoint business relationship and had setup few sites. I am trying this on my Windows 10 laptop.
Getting "permission denied" mistake while running below Python script.
SP-upload_test.py
============ import requests from requests_ntlm import HttpNtlmAuth fileName = 'C:\\Users\\xxxx\\Documents\\Vissu.pdf' #Enter your SharePoint site and target library sharePointUrl = 'https://xxxx.sharepoint.com' folderUrl = '/sites/MyTeam/Internal' #Sets up the url for requesting a file upload requestUrl = sharePointUrl + '/_api/web/getfolderbyserverrelativeurl(\'' + folderUrl + '\')/Files/add(url=\'' + fileName + '\',overwrite=true)' #Read in the file that we are going to upload file = open(fileName, 'rb') #Setup the required headers for communicating with SharePoint headers = {'Content-Type': 'application/json; odata=verbose', 'take': 'application/json;odata=verbose'} #Execute a request to go the FormDigestValue. This volition be used to authenticate our upload asking r = requests.post(sharePointUrl + "/_api/contextinfo",auth=HttpNtlmAuth('domain\\user','countersign'), headers=headers) print(r.json()) formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue'] #Update headers to use the newly acquired FormDigestValue headers = {'Content-Type': 'awarding/json; odata=verbose', 'take': 'application/json;odata=verbose', 'ten-requestdigest' : formDigestValue} #Execute the request. If y'all run into issues, inspect the contents of uploadResult uploadResult = requests.post(requestUrl,auth=HttpNtlmAuth('domain//user','countersign'), headers=headers, information=file.read()) ================================================================================== print(r.json()) prints below info
{'error': {'lawmaking': '-2147024891, System.UnauthorizedAccessException', 'bulletin': {'lang': 'en-US', 'value': 'Admission denied. You do not have permission to perform this action or access this resource.'}}}
Receiving "Access denied" for both private and public member, function of privacy settings for the site.
Let me know for any other information. Thanks in accelerate!
function-sharepoint-online office-sharepoint-server-development
Hello @nagarna1-7825,
If yous are using SharePoint Online, delight use SharePlum library to upload file into library:
from shareplum import Office365 from shareplum import Site from shareplum.site import Version authcookie = Office365('https://TenantName.sharepoint.com', username='userName@TenantName.onmicrosoft.com', password='******').GetCookies() site = Site('https://TenantName.sharepoint.com/sites/SiteName/', version=Version.v365, authcookie=authcookie); binder = site.Folder('Shared Documents') with open("D:\myfile.txt", style='rb') as file: fileContent = file.read() binder.upload_file(fileContent, "myfile.txt")
Reference:
SharePlum Tutorial
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable due east-mail notifications if you want to receive the related electronic mail notification for this thread.
Hi @Jerryzy-MSFT !
That does not work for me.
When I try:
site = Site('https://TenantName.sharepoint.com/sites/SiteName/', version=Version.v365, authcookie=authcookie)
I go the following error:
'RequestsCookieJar' object is not callable
Any suggestions on this?
Many thanks in accelerate :)
0 Votes 0 ·
@Jerryzy-MSFT ,
Thank you for quick response. Receiving below exception when fetching cookies.
raise Exception('Mistake authenticating against Office 365. Error from Office 365:', bulletin[0].text)
Exception: ('Error authenticating confronting Office 365. Error from Office 365:', 'AADSTS53003: Access has been blocked by Provisional Admission policies. The access policy does not permit token issuance.')
Also, below error when tried On Premise Hallmark
choice 1: site = Site('https://<tenant>.sharepoint.com/sites/MyTeam/', auth=cred)
raise ShareplumRequestError("Shareplum HTTP Post Failed", err)
shareplum.errors.ShareplumRequestError: Shareplum HTTP Post Failed : 403 Customer Mistake: Forbidden for url: https://tenant.sharepoint.com/sites/MyTeam//_vti_bin/lists.asmx
choice 2: site = Site('http://<tenant>.sharepoint.com/sites/MyTeam/', auth=cred)
Traceback (most recent call last):
File "<pyshell#eight>", line one, in <module>
site = Site('http://<tenant>.sharepoint.com/sites/MyTeam/', auth=cred)
File "C:\Users\navee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\shareplum\site.py", line 520, in Site
return Site2007(site_url,
File "C:\Users\navee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\shareplum\site.py", line 95, in init
self.site_info = self.get_site()
File "C:\Users\navee\AppData\Local\Programs\Python\Python38-32\lib\site-packages\shareplum\site.py", line 237, in get_site
data = envelope[0][0][0]
File "src\lxml\etree.pyx", line 1161, in lxml.etree.Chemical element. getitem
IndexError: list alphabetize out of range
Howdy @nagarna1-7825 ,
For SharePoint Online, please cheque if the user account which enabled MFA, then it's non supported, effort to shut MFA, you can cheque the detailed Conditional Admission Policy as official document:
Troubleshooting sign-in issues with Conditional Access
For SharePoint On-Premise(tested in SharePoint 2016 On-Premise), please modify like this:
from shareplum import Site
from requests_ntlm import HttpNtlmAuth
from shareplum.site import Version
cred = HttpNtlmAuth('Contoso2016\\Administrator', 'P@ssw0rd')
site = Site('http://sp2016/sites/dev', Version.v2016, auth=cred)
folder= site.Folder('Shared Documents')
with open("c:\Test.txt",mode='rb') as file:
fileContent=file.read()
folder.upload_file(fileContent,"myfile.txt")
Necessary to specify Version in Site Object, default value is 2007 which has no folder holding.
question details
Related Questions
hernandezsholeake.blogspot.com
Source: https://docs.microsoft.com/answers/questions/142065/ways-to-accessupload-documents-to-sharepoint-sites.html
Hi @nagarna1-7825 ,
Is at that place whatsoever updates ? Did the SharePlum solution work for yous ?
If the solution is working, I propose y'all could accept as reply and then that information technology could aid others in the forum :)
0 Votes 0 ·
No, it has non. Tried all the options but not helpful. Figuring out Provisional Access for SharePoint, every bit the link that was shared was mostly on Azure (Troubleshooting sign-in problems with Provisional Access).
Is information technology possible for a screen share session. If not, please let me know any other alternate options. Thanks!
0 Votes 0 ·