1

I'm trying to perform an upload files from Linux to share point using Python. However I tried a lot by googling but nothing help. At last I got a power shell script that is working. So requesting for help to convert the below script to Python 3

Specify tenant admin and site URL
$User = "[email protected]"
$SiteURL = "https://test-my.sharepoint.com/personal/justin_jacob_spidersoftin";


$Folder = "C:\Users\justin.jacob\Desktop\New folder"
$DocLibName = "Documents"

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


$Password  = ConvertTo-SecureString ‘123@123’ -AsPlainText -Force


#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)


$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle("$DocLibName")


$Context.Load($List)



$Context.ExecuteQuery()

#Upload file
Foreach ($File in (dir $Folder -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}
6
  • Powershell is available for Linux... Commented May 24, 2020 at 18:48
  • But I need to convert to python as per my requirement Commented May 24, 2020 at 18:49
  • Why the requirement to use Python? Just use what works - your Powershell script. Commented May 24, 2020 at 19:03
  • It is need to integerate with other python scripts as framework Commented May 24, 2020 at 19:10
  • 2
    So, maybe you can leverage these resources to learn what you need to know to get you to where you want to be. Yet again, Powershell can call Python code and Python can call Powershell code. PowerShell Guide to Python --- PowerShell To Python And Back Commented May 24, 2020 at 22:29

1 Answer 1

4

I understand you want to upload files to sharepoint, you can take a reference of below code:

import os
from config import config
from shareplum import Site
from shareplum import Office365
from shareplum.site import Version

# get data from configuration
username = config['sp_user']
password = config['sp_password']

authcookie = Office365('https://xxx.sharepoint.com', username=username, password=password).GetCookies()

site = Site('https://xxx.sharepoint.com/sites/abc',version=Version.v365, authcookie=authcookie)
spfolder = site.Folder('Shared Documents/testfolder')

for root, dirs, files in os.walk(r"D:\mytestfolder"): 
    for file in files:
        filepath = os.path.join(root, file)
        print(filepath)

        # perform the actual upload
        with open(filepath, 'rb+') as file_input:
            try: 
                spfolder.upload_file(file_input, file)
            except Exception as err: 
                print("Some error occurred: " + str(err))

The code uses following python library:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Baker_Kong_MSFT. May I know is it possible to upload subdirectories like this
Do you want to create sub folder in SP? if so, you can use "folder = site.Folder('Shared Documents/This Folder')" shareplum.readthedocs.io/en/latest/files.html
Or you can use this rest endpoint to add folder: codeproject.com/Questions/1198479/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.