Data,  Functionality

Patching a Collection to SharePoint with an attachment on the records

This is something that there is no “standard” way of doing as saving an attachment directly from Power Apps requires an attachment control with either SubmitForm() or Patch(Form.Updates), however often the requirement is for records to be accumulated in a Collection (including possible offline saving) and then bulk updated to SharePoint.

The workaround described below only works with a single attachment per record. Nested galleries do not seem to work on this.

Firstly you have a Collection with an Attachment on each (or some of) the record/s. Insert a Gallery (you can hide it later) with the Items of the Collection and any other fields you want to Patch on Labels (though this is not strictly necessary). 

Now add a Text Label with

First(ThisItem.Attachments).DisplayName

and an Image Control with

First(ThisItem.Attachments).Value

The “hack” here is that the Image control converts the BLOB (Binary Large Onject) storage medium of Attachments to a format (Base64) able to be sent to the ultimate destination/format for an Attachment, which is actually in a hidden SharePoint Library. NOTE: The file does not need to be an Image type for this to work, so do not be concerned with blank images in the Gallery.

Now all you need to do is the Patch

Patch(
   SPListName,
   ForAll(
      GalleryName.AllItems As _Data,
      {
         Field1: _Data.Field1,
         Field2: _Data.Field2,
         Attachments: Table(
            {
               DisplayName: _Data.TextLabel.Text,
               AbsoluteUri: Blank(),
               Id: _Data.ImageControl.Image,
               Value: _Data.ImageControl.Image
            }
         )
      }
   )
)

This will send the Collection and the first attachment to the SP List as new records.

Leave a Reply

Your email address will not be published. Required fields are marked *