Python Office365 Rest API - Error with get_folder_by_server_relative_url .add function

119 views Asked by At

I have had a script that worked fine for as long as I can remember, however, recently it has errored out with the below:

TypeError   Traceback (most recent call last)
Cell In[19], line 53
     50 info.url = target_file
     51 info.overwrite = True
---> 53 result_file = context.web.get_folder_by_server_relative_url(target_folder).files.add(info)
     54 context.execute_query()
     55 print('File {0} has been uploaded successfully'.format(result_file.properties['ServerRelativeUrl']))

TypeError: add() missing 1 required positional argument: 'content'

I cannot figure out how to resolve this or which positional argument might be missing. Any help is highly appreciated as I have been trying to figure this out all day to no avail :(

import logging
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
from office365.sharepoint.files.file_creation_information import FileCreationInformation
import os

###### Logging setup ####################################################
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
#########################################################################

# Site settings that won't change
site_root = '/Sites/SampleSite'
site_url = 'https://REDACTED.sharepoint.com' + site_root

# Specify the file to upload
path = "./SampleFile1.xlsx"
target_folder = "Shared%20Documents/UserFiles"
target_file = "SampleFile1.xlsx"

# SharePoint app-only principal - credentials    
client_id     = 'REDACTED'
client_secret = 'REDACTED'



context_auth = AuthenticationContext(site_url)
print('check')
if context_auth.acquire_token_for_app(client_id, client_secret):

        print('check')
        # Looks like we have a valid access token.  Download a file
        context = ClientContext(site_url, context_auth)
        #print(context)
        with open(path, 'rb') as content_file:
             file_content = content_file.read()

        info = FileCreationInformation()
        info.content = file_content
        info.url = target_file
        info.overwrite = True

        result_file = context.web.get_folder_by_server_relative_url(target_folder).files.add(info)
        context.execute_query()
        print('File {0} has been uploaded successfully'.format(result_file.properties['ServerRelativeUrl']))


else:
    # No token or error was returned
    logger.critical("Authentication failed")
    try:
        logger.critical(context_auth.get_last_error())
    except:
        logger.critical("Reason for failure is unknown")
1

There are 1 answers

1
Ahmad On

You are reading the file content and setting it in the info.content property but, it the library wants you to pass the content as a parameter to the add method

result_file = context.web.get_folder_by_server_relative_url(target_folder).files.add(target_file, file_content, True)