Closed AGD-Fersa closed 3 days ago
Final solution:
from office365.sharepoint.client_context import ClientContext
import base64
# Connection parameters and data
URL = 'https://your-sharepoint-site-url/' # SharePoint site URL
URL_RELATIVE = 'your-relative-folder-path/' # Relative folder path in SharePoint
USERNAME = 'your-email@example.com' # SharePoint username
PASSWORD = 'your-password' # SharePoint password
data = startTrigger.data # Data received (file and metadata)
FILE = data['file'] # Base64 encoded file
META = data['meta'] # Metadata associated with the file
# Function to establish the SharePoint context
def get_sharepoint_context(url, user, password):
ctx = ClientContext(url).with_user_credentials(user, password)
return ctx
# Function to get the target folder in SharePoint
def get_target_folder(ctx, url_relative):
target_folder = ctx.web.get_folder_by_server_relative_url(url_relative)
return target_folder
# Function to upload a file to SharePoint with metadata
def upload_file_to_sharepoint(url, url_relative, user, password, file, meta):
# Get the SharePoint context and target folder
ctx = get_sharepoint_context(url, user, password)
target_folder = get_target_folder(ctx, url_relative)
# Decode the file and prepare its name
file_content = base64.b64decode(file['base64Data'])
file_name = file['name']
# Upload the file to the specified folder
uploaded_file = target_folder.upload_file(file_name, file_content).execute_query()
# Assign metadata to the uploaded file
file_item = uploaded_file.listItemAllFields
for key in meta:
value = meta[key] # Get the metadata value
file_item.set_property(key, value) # Set the key-value pair in SharePoint
# Save the changes in the metadata
file_item.update()
ctx.execute_query()
# Execute the file and metadata upload function
upload_file_to_sharepoint(URL, URL_RELATIVE, USERNAME, PASSWORD, FILE, META)
{
"file": {
"name": {{ fileInput1.file.name }},
"base64Data": {{ fileInput1.file.uri.substring(fileInput1.file.uri.indexOf("base64,") + 7) }}
}, "meta": {
"NRecord": {{ ID.value }},
"Date": {{ getRecord.data.date[0] }},
"Location": {{ getRecord.data.location[0] }},
"Supplier": {{ getRecord.data.supplier[0] }},
"Batch": {{ getRecord.data.batch[0] }},
"Status": {{ getRecord.data.status[0] }},
"Analysis": {{ getRecord.data.analysis[0] }},
}
}
This is my actual code:
I am currently working on a Retool project. The objective is to enable users to complete a form and upload a file, which will then be sent to SharePoint along with the form metadata. In SharePoint, I have configured several columns in my collection to align with the metadata of the files. It is my objective that, upon uploading a file, the metadata is directly linked to the columns in SharePoint. This will enable users to access SharePoint and filter or sort files based on the metadata of the files. I am attempting to complete this process using Python code within a workflow. While the code is able to successfully upload the files to SharePoint, it does not set the metadata. The workflow receives a JSON similar to the following example, and I have verified in the logs that the data is reaching the Python code, indicating that the issue is not with the data transfer:
I have conducted extensive research, tested and retested the code, and have been unable to resolve the issue. I would appreciate any assistance in resolving this challenge.