Python Power Automate - SharePoint

SharePoint lists and files driven through flows, plus reacting to SharePoint changes.

SharePoint is one of the systems flows connect to most often, and Power Automate ships with hundreds of ready SharePoint actions. Your Python services drive them through flows - a flow with an HTTP request trigger performs the SharePoint work, and the service sends it the data. In the other direction, flows watching SharePoint call your services when something changes.

Creating list items

The flow wraps a "Create item" action, the service sends the item's fields.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class RegisterSupportTicket(Service):

    input = 'customer', 'subject', 'priority'

    def handle(self):

        # Get the connection by its Dashboard name
        conn = self.microsoft.power_platform['My Power Automate']

        # The flow creates an item in the support tickets list
        conn.trigger('flow-create-ticket-item', {
            'customer': self.request.input.customer,
            'subject': self.request.input.subject,
            'priority': self.request.input.priority,
        })

        self.response.payload = {'status': 'created'}

Reading list items

A flow that ends with a Response action can hand SharePoint data back to your service - here, a "Get items" action whose results the flow returns.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class GetOpenTickets(Service):

    def handle(self):

        conn = self.microsoft.power_platform['My Power Automate']

        # The flow queries the list and responds with the matching items
        result = conn.trigger('flow-get-open-tickets', {
            'status': 'Open',
        })

        self.response.payload = {'tickets': result['items']}

Uploading files

For files, the flow wraps a "Create file" action - the service sends the file name and its content, e.g. a report the service generated.

# -*- coding: utf-8 -*-

# stdlib
from base64 import b64encode
from datetime import date

# Zato
from zato.server.service import Service

class PublishDailyReport(Service):

    def handle(self):

        conn = self.microsoft.power_platform['My Power Automate']

        # Generate the report ..
        report = self.invoke('reports.build-daily-summary', {})

        # .. encode it for transport ..
        content = b64encode(report['csv'].encode('utf-8')).decode('ascii')

        # .. and let the flow store it in the document library.
        conn.trigger('flow-upload-report', {
            'file_name': f'daily-summary-{date.today().isoformat()}.csv',
            'content': content,
        })

Reacting to SharePoint changes

The reverse direction needs no triggering at all - build a flow with the "When an item is created or modified" SharePoint trigger, and have its action call a Zato REST channel. The service below is what the channel invokes.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class OnContractUploaded(Service):
    """ Invoked by a flow whenever a new contract lands in the SharePoint library.
    """
    input = 'file_name', 'uploaded_by'

    def handle(self):

        file_name = self.request.input.file_name
        uploaded_by = self.request.input.uploaded_by

        self.logger.info('New contract: %s (from %s)', file_name, uploaded_by)

        # Continue the process - extract data, update the CRM, and so on
        self.invoke('contracts.process-new', {'file_name': file_name})

More resources

Learn more