Programming

Uploading files to SharePoint using Python – Part 1

Python SharePoint

In this multi part article, we are going to setup a Python application that will upload documents to a SharePoint Document Library. We will be using an open source Python module named Shareplum to help simplify some parts of this. 

Introduction

This tutorial is going to be broken into 3 parts. In part 1 we will go through the steps to create a new virtual environment, install the dependencies and authenticating with SharePoint. Part 2 we will work on uploading documents and adding metadata to an uploaded document. Then in part 3 will put all this together and iterate over a folder containing multiple files and add metadata to each file bases on an index file or the name of the file. 

I will be working with Visual Studio Code in a Windows environment for this tutorial. All the step include here will work with any IDE, however, some may need to be completed in a different manner.

Installation

Creating the virtual environment:

From the integrated terminal in VS Code, run the following command. “myenv” will be the name of your new virtual environment. 

python -m venv c:\path\to\myenv

Activating the virtual environment:

Now you will need to activate the virtual environment. Alternatively, your IDE may know to switch to the venv interrupter, but we are going to just activate it manually. To do this you will need to open the new folder that the above command created in VS Code. Then run the below command. 

Script\activate

You should now see that the name of your venv is in ( ) on the left-hand side of your terminal prompt. 

Last thing will be to change the current interrupter to the interrupter within your venv. To do this you will press crtl+shift+p. Then search for “Python: Select interpreter”, select “enter interrupter form path” and finally select “find”. Now you will need to brows to the scripts folder inside the venv folder and select python.exe. 

Join Amazon Prime – Watch Thousands of Movies & TV Shows Anytime – Start Free Trial Now

Updating pip:

Run the following command in the terminal.

python -m pip install –upgrade pip

Installing modules:

Run the following code to install pylint, shareplum, argpars, configparser and pathlib. Argpars, configparser and pathlib will be used in part 3. 

python -m pip install pylint, shareplum, argpars, configparser, pathlib

Authentication

The connection to SharePoint is going to be done with the Shareplum library. This authentication is going to require a username and password that has owner promotions to the document library you are upload to.

Two methods from the Shareplum module (Site and Office365) and one from the site module called “Version” will be used.

from shareplum import Site, Office365
from shareplum.site import Version

We will then create a new “Auth” class. We are doing this so that we can authenticate from multiple scrips latter in this tutorial. Within this class we are going to have three functions.

  1. __init__, this will initialize our variables
  2. setUp, this will create our config payload
  3. signIn, this will create a authcookie and us this cookie to create a connection to the SharePoint site we are wanting to upload to.

 

 # create new class for login into SharePoint
class Auth:
    # initeate the valuables
    def __init__(self, username, password, siteURL, baseURL):
        self.username = username #username to include user@domain.com
        self.password = password #password for the user
        self.siteURL = siteURL #this is the url for the SharePoint site where the document library is located that you will be uploading to
        self.baseURL = baseURL #this is the url for you SharePoint, i.e.. domain.sharepoint.com
        self.setUp()
    # build the sign dict
    def setUp(self):
        config = {
            'username': self.username,
            'password': self.password,
            'siteURL': self.siteURL,
            'baseURL': self.baseURL
        } 
        self.signIn(config)

    #Build the signin and execute the signin     
    def signIn(self, config):
        authcookie = Office365(config['baseURL'], username=config['username'], password=config['password']).GetCookies()
        self.site = Site(config['siteURL'],version=Version.v365,authcookie=authcookie)
        return self.site

We are now ready to test the code. To do so we are going to add some code that we will later remove. first call the “Auth” class and pass it four arguments “username, password, siteURL, baseURL”. Then save it in the variable named “login”. Next call the “List” class from the “site” module with the “login” object “login.site.List()”. Finally print the content of list_data. 

login = Auth('user@domain.com', ‘password', 'https://domain.sharepoint.com/sites/TEST', 'https://domain.sharepoint.com')

list_data = login.site.List('Python File Upload')
print(list_data)

Now you can run the authentication.py file. If all was successful you will see a of dictionaries containing all the files currently in the document library.

2 thoughts on “Uploading files to SharePoint using Python – Part 1

Leave a Reply