Add a new SharePoint document library called Product with the following columns and types:
- CustomerName (single-line text)
- ProductName(single-line text)
- SoldDate (Date only)
- ShippedLocation(single-line text)
Inserting Attachments Field
In the Submit a Product app a salesperson needs to add a file to an attachments field and upload it to SharePoint. Open Power Apps and Create a new app from blank. Then insert a few labels and icons to make it look like the screenshot below.
The attachments field cannot be inserted from the menu so we must use a workaround. Go to the data tab on the left menu and add any existing SharePoint list from your SharePoint site. Do not add the Product SharePoint document library we created earlier. We cannot use it to generate an attachments field.
Create a new Edit Form with your SharePoint list as the data source. The attachments control should automatically appear when we create the form. If it does not, you can add the attachments field using Edit fields on the right-side menu.
Cut the attachments control and paste it outside the form. Delete the form.
Use the following properties in the attachments control:
Items: Blank() DisplayMode: DisplayMode.Edit MaxAttachments: 1
Our app now has an attachments field we can use to upload the product documents. We can now delete connection to the SharePoint list. It is no longer needed.
Make a Flow to Upload Documents To a SharePoint Library
Documents cannot be uploaded to a SharePoint document library directly from Power Apps. We will need to create a Power Automate flow to do it instead. Open the Power Automate action from the top menu and select Create a new flow.
Choose the Power Apps button template.
Name the flow UploadFileToDocument library and click Save.
We want to use the PowerApps (V2) trigger because it supports files as an input type whereas the PowerApps trigger does not. Delete the PowerApps trigger and add PowerApps(V2) in its place.
Select the File input type. Name the input File Content. Make File Content a required input.
Add a SharePoint action to the flow: Create File. Use your SharePoint site address and document library folder path as inputs. The File Content field should reference the file found in our flow trigger.
File Name requires a flow expression. Open the flow expression editor then copy + paste this code into the File Name field.
triggerBody()['file']['name']
We are now done creating the flow to upload a document.
Connect The ‘Upload To SharePoint’ Flow In Power Apps
Go back to PowerApps and add a button. Select the OnSelect property and click on the UploadFileToDocumentLibrary field in the Power Automate menu.
The flow will appear as a connection in the data tab.
Remove any code from the Upload button’s OnSelect property and replace it with this code. A file must be passed as an object to Power Automate. The contentBytes field holds a reference to the attachment file and name stores the filename and extension.
UploadFileToDocumentLibrary.Run( { contentBytes: First(att_SubmitContract_AttachFile.Attachments).Value, name: First(att_SubmitContract_AttachFile.Attachments).Name } );
Note: if you are receiving an error when passing the file to the flow make sure you have set the flow input to be a required field.
We’re done creating the ‘upload a document to a SharePoint library’ functionality. Test your app to make sure its working.
Comments