Sending Reports to Sharepoint

To send reports up to a Teams or Sharepoint site, you'll first need to set up an Entra (previously Azure AD) application that has the correct permissions to write to the Sharepoint/Teams folder.

Create a JSON configuration file to store your API data, such as…

{
	"tennant": "<Office365 Tennant ID>",
	"client": "<Application Client ID>",
	"thumbprint": "<API Thumbprint ID>",
	"key": "<Path To Certificate KeyFile>",
	"url": "<URL to Sharepoint Base Site> (ie. https://mycompany.sharepoint.com)",
	"site": "<URL to Specific Sharepoint Site> (ie sites/Alarms)",
	"folder": "<Base Folder for Uploaded Files> (ie. Shared Documents/General/...)"
}

Then, you'll need the following script in your publish.py file.

import os
import sys
import json
 
from office365.sharepoint.client_context import ClientContext
 
os.chdir(os.path.dirname(__file__))
 
#Open the configuration file 
f = open(os.path.dirname(__file__) + "/o365.json",'r')
content = f.read()
f.close()
 
cfg = json.loads(content)
 
#Read details from the configuration
tennant = cfg['tennant']
clientid = cfg['client']
thumbprint = cfg['thumbprint']
key = cfg['key']
 
baseurl = cfg['url']
basesite = cfg['site'] # every share point has a home.
siteurl = baseurl + basesite 
 
#Creates a 'dummy' path to be used later
remotepath = cfg['folder'] + "sample.aspx" 
 
#Connect to the Sharepoint site
ctx = ClientContext.from_url(siteurl).with_client_certificate(tennant, clientid, thumbprint, key)
 
#Read the list of files to be uploaded
f = open(sys.argv[1],'r')
dta = f.read()
f.close()
 
#Split the remote path to find where we're supposed to  put content
dir, name = os.path.split(remotepath)
 
#Split the list of files by newlines, and send each individual file
lines = dta.split("\n")
for l in lines:
    if l != "":
        #Read the file to be uploaded (make sure it's opened as binary!)
        with open(l, 'rb') as content_file:
            file_content = content_file.read()
 
        #Split the file and directory names
        sdir,sname = os.path.split(l)        
 
        #Upload the file to Sharepoint
        file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(sname, file_content).execute_query()