In today’s digital age, efficient file management and sharing are crucial for businesses and organizations. SharePoint, a powerful platform by Microsoft, allows users to store, organize, share, and access information from virtually any device. Automating the process of uploading files using Python can save time and reduce errors, especially when dealing with large volumes of data. This guide will show you how to programmatically write local files and folders to SharePoint using Python and security tokens for enhanced security.

Prerequisites

Before you start, ensure you have the following:

  1. Python installed: Download and install the latest version of Python from python.org.
  2. SharePoint account: Access to a SharePoint site with the necessary permissions to upload files.
  3. Azure App Registration: Register an app in Azure AD to obtain client ID, client secret, and tenant ID.

Required Python packages: Install requests and Office365-REST-Python-Client packages using pip:

pip install requests

pip install Office365-REST-Python-Client

Step-by-Step Guide to Writing Local Files and Folders to SharePoint Using Python and Security Tokens

1. Register an App in Azure AD

  1. Go to the Azure portal and register a new application.
  2. Note the Application (client) ID and Directory (tenant) ID.
  3. Create a client secret and note the value (this will be used as your client secret).

2. Set Up Authentication with Security Tokens

To interact with SharePoint, you’ll need to authenticate your application using OAuth tokens. Use the Office365-REST-Python-Client to handle authentication.

from office365.runtime.auth.client_credential import ClientCredential

from office365.sharepoint.client_context import ClientContext

# Replace with your SharePoint site and credentials

site_url = ‘https://yoursharepointsite.sharepoint.com/sites/yoursite’

client_id = ‘your-client-id’

client_secret = ‘your-client-secret’

tenant_id = ‘your-tenant-id’

client_credentials = ClientCredential(client_id, client_secret)

ctx = ClientContext(site_url).with_credentials(client_credentials)

3. Obtain the Access Token

To obtain an access token, use the requests library to authenticate with Azure AD.

import requests

def get_access_token(tenant_id, client_id, client_secret):

    url = f’https://accounts.accesscontrol.windows.net/{tenant_id}/tokens/OAuth/2′

    payload = {

        ‘grant_type’: ‘client_credentials’,

        ‘client_id’: client_id,

        ‘client_secret’: client_secret,

        ‘resource’: ‘https://yoursharepointsite.sharepoint.com’

    }

    response = requests.post(url, data=payload)

    response.raise_for_status()

    return response.json().get(‘access_token’)

access_token = get_access_token(tenant_id, client_id, client_secret)

headers = {‘Authorization’: f’Bearer {access_token}’}

4. Upload a Single File

To upload a single file using the access token:

import os

def upload_file_to_sharepoint(ctx, local_file_path, sharepoint_folder_url):

    with open(local_file_path, ‘rb’) as file_content:

        target_folder = ctx.web.get_folder_by_server_relative_url(sharepoint_folder_url)

        name = os.path.basename(local_file_path)

        target_file = target_folder.upload_file(name, file_content.read())

        ctx.execute_query()

        print(f’File {name} has been uploaded to {sharepoint_folder_url}’)

# Replace with the path to your local file and SharePoint folder URL

local_file_path = ‘path/to/your/localfile.txt’

sharepoint_folder_url = ‘/sites/yoursite/Shared Documents/YourFolder’

upload_file_to_sharepoint(ctx, local_file_path, sharepoint_folder_url)

5. Upload Multiple Files in a Folder

To upload all files in a local folder to SharePoint:

def upload_folder_to_sharepoint(ctx, local_folder_path, sharepoint_folder_url):

    for root, _, files in os.walk(local_folder_path):

        for file_name in files:

            local_file_path = os.path.join(root, file_name)

            upload_file_to_sharepoint(ctx, local_file_path, sharepoint_folder_url)

# Replace with the path to your local folder and SharePoint folder URL

local_folder_path = ‘path/to/your/localfolder’

sharepoint_folder_url = ‘/sites/yoursite/Shared Documents/YourFolder’

upload_folder_to_sharepoint(ctx, local_folder_path, sharepoint_folder_url)

6. Handle Large Files

For large files, it’s better to upload them in chunks to avoid timeouts and errors:

def upload_large_file_to_sharepoint(ctx, local_file_path, sharepoint_folder_url):

    file_size = os.path.getsize(local_file_path)

    chunk_size = 10000000  # 10 MB

    upload_session = None

    with open(local_file_path, ‘rb’) as file_content:

        file_name = os.path.basename(local_file_path)

        target_folder = ctx.web.get_folder_by_server_relative_url(sharepoint_folder_url)

        

        start_byte = 0

        while start_byte < file_size:

            end_byte = min(start_byte + chunk_size, file_size)

            file_content.seek(start_byte)

            chunk_data = file_content.read(chunk_size)

            if start_byte == 0:

                upload_session = target_folder.files.create_upload_session(file_name, chunk_data)

            else:

                upload_session = upload_session.continue_upload(chunk_data)

            start_byte = end_byte

        upload_session.finalize_upload()

        ctx.execute_query()

        print(f’Large file {file_name} has been uploaded in chunks to {sharepoint_folder_url}’)

# Replace with the path to your large local file and SharePoint folder URL

local_large_file_path = ‘path/to/your/largefile.zip’

sharepoint_folder_url = ‘/sites/yoursite/Shared Documents/YourFolder’

upload_large_file_to_sharepoint(ctx, local_large_file_path, sharepoint_folder_url)

7. Set Permissions (Optional)

If you need to set specific permissions for the uploaded files:

from office365.sharepoint.permissions.permission_kind import PermissionKind

def set_file_permissions(ctx, file_url, user_email):

    file = ctx.web.get_file_by_server_relative_url(file_url)

    user = ctx.web.ensure_user(user_email).execute_query()

    

    role_def = ctx.web.role_definitions.get_by_type(PermissionKind.Edit)

    role_bindings = ctx.web.role_definitions.get_by_id(role_def.id)

    

    file.list_item_all_fields.role_assignments.add(user.principal_id, role_bindings)

    ctx.execute_query()

    print(f’Permissions set for {user_email} on {file_url}’)

# Replace with the file URL and user email

file_url = ‘/sites/yoursite/Shared Documents/YourFolder/yourfile.txt’

user_email = ‘user@domain.com’

set_file_permissions(ctx, file_url, user_email)

Conclusion

Uploading local files and folders to SharePoint programmatically using Python and security tokens can significantly streamline your workflow and improve productivity while ensuring secure access. By following these steps, you can automate the upload process and take full advantage of SharePoint’s powerful features.