Your Microsoft Technology Development and Consulting Experts - Operating since 2000

Location
Australia WideSydney, NSWMelbourne, VicBrisbane, QldPerth, WAAdelaide, SACanberra, ACTNorthern Rivers, NSWWollongong, NSWRichmond, VicDarwin, NT
emailconsult@officeexperts.com.au
email1300 102 810
Office experts logo
Microsoft certified logo
Contact Us

Author: Marcello Brocchi

Reading time: 5min

File Attachments in Power Apps - The Best Way to Do Them

Introduction

If you've ever tried to implement file attachments in Power Apps, you know it can be surprisingly challenging. You would expect attaching files to be a straightforward process, but the traditional approaches often involve complex workarounds and multiple steps for users.

The good news? Microsoft has significantly improved this process with a new action in Power Automate that makes uploading documents to SharePoint document libraries much simpler and more efficient. In this guide, I'll show you:

Prefer to Watch the Video?

Head over to my YouTube channel Chino Does Stuff to watch the video tutorial, there you will see a variety of videos with loads of tricks and tips on the entire Power Platform suite.

Why This Approach is Better

Before diving into the implementation, let's understand why this method is superior to older approaches:

Step 1: Add the File Attachment Control

One quirk of Power Apps is that the file attachment control isn't directly available in the insert menu. Here's how to add it to your app:

  1. Insert a Form control onto your screen
  2. Connect the form to any SharePoint list (it doesn't matter which one)
  3. Find the attachment control in the form
  4. Cut the attachment control (not the entire data card, just the control)
  5. Delete the form
  6. Paste the attachment control directly onto your screen

After pasting, you'll need to fix some errors in the control:

Loading code...

The MaxAttachments and MaxAttachmentSize properties are particularly important to configure based on your requirements.

Step 2: Create the Power Automate Flow

Next, we'll create a flow that receives files from Power Apps and saves them to a SharePoint document library:

Set Up the Document Library

In SharePoint, create a document library to store your attachments. Add a Number column called "ParentId" - this will store the ID of the related record from your list.

Create the Flow

In Power Automate, create a new automated flow with these steps:

  1. Trigger: Use the "Power Apps (V2)" trigger - this is crucial as it supports direct file inputs
Loading code...
  1. Create File in SharePoint: This action saves the file to your document library
  2. Update File Properties: This sets the ParentId property to associate the file with your record
  3. Respond to Power Apps: This completes the flow and returns control to your app
Loading code...

Save your flow with a descriptive name like "Save File to SharePoint".

Step 3: Connect Power Apps to Your Flow

Now, we need to modify our app's save button to handle both the record creation and file uploads:

Loading code...

Let's break down what's happening here:

  1. First, we save our record to the data source using Patch()
  2. We capture the ID of the newly created record in a variable (RecId)
  3. We use ForAll() to loop through each attachment in the FileAttachments control
  4. For each attachment, we call our flow, passing both the record ID and the file details
  5. Finally, we reset our form controls and navigate back to the home screen

How It All Works Together

When a user fills out your form and attaches files:

  1. They click the Save button
  2. The app saves the record data to your SharePoint list
  3. The app gets back the record ID
  4. For each attached file, the app calls your flow
  5. The flow saves each file to the document library and sets the ParentId property
  6. The files are now linked to your record by the ParentId

This entire process happens seamlessly, and the user only needs to click Save once.

Viewing Attached Files

To display files associated with a record, you'll need to:

  1. Create a gallery control on your view screen
  2. Connect it to your document library
  3. Filter the gallery to show only files where ParentId matches your current record's ID
  4. Add an icon or button to open each file

The formula for the gallery's Items property would be something like:

Filter(YourDocumentLibrary, ParentId = CurrentRecord.ID)

Best Practices and Considerations

When implementing file attachments in Power Apps:

Advanced Customisations

This approach can be extended in several ways:

Conclusion

With Microsoft's improved Power Apps V2 trigger in Power Automate, handling file attachments in Power Apps has become significantly easier. This approach provides a clean, user-friendly way to upload files to SharePoint and maintain their relationship with your app's data.

By using a document library instead of list attachments, you gain all the benefits of SharePoint's document management capabilities while providing a seamless experience for your users.

This method eliminates much of the complexity that was previously required for file uploads in Power Apps, making your apps more maintainable and user-friendly.

If you'd like to see more Power Apps development tips and techniques, please head over to the YouTube channel and leave a comment there. I actively monitor and respond to all YouTube comments!

About the Author

Marcello is a lead developer at Powerplatform Experts, an expert in, Computer Science, Power Platform integration and founder of the YouTube channel ChinoDoesStuff. If you have any questions, please feel free to get in contact at via the form below.

Marcello Brocchi

Contact Us

Get in touch with our team for general inquiries and support. We're here to help with any questions you might have about our services.

Request a Quote

Need pricing for a specific project? Fill out our quote form and we'll provide you with a detailed estimate tailored to your needs.