Power Apps Patch Attachments to SharePoint List [With Conditions]

I recently worked on a client task where I needed to patch the attachments from a Power Apps modern form to a SharePoint list. Additionally, some validations and conditions exist when updating any file or image on the list.

In this article, I will tell you how to patch attachments to the SharePoint list attachment column based on some conditions.

Power Apps Patch Attachments to SharePoint List

A SharePoint list called Unit Test contains four columns with different data types. Such as:

ColumnData type
TitleBy default single line of text
StatusChoice [Pass, Fail, N/A]
Test NotesMulti-line text
AttachmentAttachments

I have added some items, except the Test Notes and Attachments, to this list. I want to update these two fields using Power Apps.

Patch Attachments to SharePoint List in Power Apps

In Power Apps, there is a gallery control and a Modern form. When the user selects any specific item, the right side of the form displays the details of the particular item. Also, user can update their record by using the save button. The save button won’t be visible until the edit icon is clickable.

Patch Attachments to SharePoint List in PowerApps

Also, some conditions must be checked while attaching the files/videos and clicking the save button. Such as:

Case – 1:

If Status = Fail, then the Attachments field will be mandatory.

Case – 2:

If Status = Fail, the User must fill in the Test Notes, which should take only 10 characters.

Case – 3:

If Status ❌ Fail, then the attachment field will not be mandatory, and the test notes validation will also not be applicable here. That means it will simply take your value and update the specific item in the SharePoint list.

Case – 1 & Case – 2:

Here, in the screenshot below, you can see that whenever the user selects the Status as Fail and fills in the Test Notes, it shows the validation error under the notes and the attachments field mandatory.

Patching Attachments to SharePoint List in PowerApps

The validation error is not shown whenever the user fills the Test Notes with 10 characters. Also, he/she can upload any file/video in the attachments. If no file is there and tried to update the item, then the item will not update, and also, it’ll show the validation error for attaching the file.

Once the file has been attached and clicked on save, it’ll display a success popup message on the screen.

Patching Attachments to SharePoint List in Power Apps

Go to the SharePoint list and refresh it once. The item has been updated along with the Test notes and Attachments below.

Patch Attachment to SharePoint List in Power Apps

Case – 3:

Suppose I choose the Status as Pass or any other values apart from the Fail; then that time, it won’t show any validation error, and the attachments will not be mandatory. So we can update the items without any conditions.

Update Attachments to SharePoint List in Power Apps

You can see the item has been updated in the SharePoint list.

Update Attachments to SharePoint List in PowerApps

Patch Attachments to SharePoint List in Power Apps

Case – 1: [Solution]

If Status = Fail, then the Attachments field will be mandatory.

Select the Attachment Datacard and set its Required property to the code below:

If(
    !IsBlank(DataCardValue12.Selected.Value),
    If(
        DataCardValue12.Selected.Value = "Fail",
        true,
        false
    ),
    false
)

Where,

DataCardValue12 = Status field name

Patch Attachment to SharePoint List in PowerApps

Case – 2: [Solution]

If Status = Fail, the User must fill in the Test Notes, which should take only 10 characters.

Select the Test Notes field and set its Value property to the code below:

LookUp('Unit Test',ID=Gallery1.Selected.ID,'Test Notes')

Where,

Gallery1 = Gallery control

Power Apps Update Attachments to SharePoint List

To validate the Test Notes field, write the code below on its OnChange property as:

OnChange = UpdateContext(
    {
        HasFirstNameInput: true,
        // Track that the user has started typing
        IsFirstNameInvalid: If(
            DataCardValue12.Selected.Value = "Fail",
            Len(Self.Value) < 10
        )
    }
)

HasFirstNameInput, IsFirstNameInvalid = Context variable name

Update Attachment to SharePoint List in Power Apps

To display the error message under the Test Notes field, select the error message field and set its Text property to the formula below:

Text = If(IsFirstNameInvalid,"Please enter Test Notes on failure and attach screenshot or video"," ")
How to Update Attachment to SharePoint List in Power Apps

Next, select the error message field and apply the variables on its Visible property:

Visible = HasFirstNameInput && IsFirstNameInvalid
How to Update Attachment to SharePoint List in PowerApps

Now it’s time to update all the SharePoint list’s field values and the attachments. Additionally, only the Save button will be visible once the user clicks the Edit icon; otherwise, it’ll always be in hidden mode.

To do this, we will create a Context variable on the Edit icon’s OnSelect property as:

OnSelect = UpdateContext({ varSubmit:true})

Where,

varSubmit = Variable Name

Update Attachment to SharePoint List in PowerApps

Insert a Save button and set its OnSelect property as:

OnSelect = If(
    DataCardValue12.Selected.Value <> "Fail",
    Patch(
        'Unit Test',
        LookUp(
            'Unit Test',
            ID = Gallery1.Selected.ID
        ),
        {
            'Test Notes': DataCardValue11.Value,
            Status: {Value: DataCardValue12.Selected.Value}
        },
        Form1.Updates
    );
    UpdateContext(
        {
            varSubmit: false,
            varSavePopUp: true
        }
    );
    ,
    If(
        !IsBlank(DataCardValue11.Value),
        If(
            DataCardValue12.Selected.Value = "Fail" && !IsFirstNameInvalid,
            If(
                DataCardValue12.Selected.Value = "Fail" && !IsAttached,
                Patch(
                    'Unit Test',
                    LookUp(
                        'Unit Test',
                        ID = Gallery1.Selected.ID
                    ),
                    {
                        'Test Notes': DataCardValue11.Value,
                        Status: {Value: DataCardValue12.Selected.Value}
                    },
                    Form1.Updates
                );
                UpdateContext(
                    {
                        varSubmit: false,
                        varSavePopUp: true
                    }
                );
                ,
                Notify(
                    "Please attach files!",
                    NotificationType.Error
                )
            ),
            Notify(
                "Please provide Test Notes on failure more than 10 characters!",
                NotificationType.Error
            )
        ),
        Notify(
            "Please provide Test Notes!",
            NotificationType.Error
        )
    )
);
Refresh('Unit Test');

Where,

  • DataCardValue12 = Status field name
  • DataCardValue11 = Test Notes field name
  • Form1 = Edit form name
  • varSavePopUp = This is the variable I created for a success pop-up message. If the code executes fine, the values will update in the SharePoint list, and the user can see a pop-up message.
Power Apps Patch Attachments to SharePoint List

Finally, save, publish, and preview the app. Test this, including the status, test notes, and attachment value. All the field values will be updated in the SharePoint list.

I hope this article finds you very helpful. This way, we can patch attachments to the SharePoint list in Power Apps, including specific conditions.

Moreover, you may like some more Power Apps articles:

>
Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial

FREE Power Platform Tutorial PDF

Download 120 Page FREE PDF on Microsoft Power Platform Tutorial. Learn Now…