Restrict SharePoint list item attachments by file type using a custom form

Have you ever wanted to restrict the attachments on a SharePoint Online list by file type? If so you probably found that using SharePoint only there is no such setting.

However, it is possible to use a custom list form built with the help of the Power Apps list forms to achieve this result, as in the example below:

Check how to create the form and the steps to customise it:

Create the custom form with Power Apps

Navigate to the list you want to customise, and select the option to customise the form with Power Apps:

Then add any additional field to your form (if not all fields were added automatically). You can reorder the fields here too:

Edit the attachment card/controls

For the formulas to be clearer, rename the controls inside the attachment control from the standard pattern:

  • DataCardValue** to attControl
  • ErrorMessage** to lblErrorAttachment

Also, update the height of the control attControl to 100 pixels only.

Next, unlock the attachment card so it’s possible to update any formula on its controls:

Select the control lblErrorAttachment and update its ‘Text’ formula to:

Coalesce(
    Parent.Error,
    With(
        {
//Simply add extra allowed file extensions here in this array
            allowedFileExtensions: [
                "pdf",
                "docx"
            ]
        },
        If(
            IsBlank(
                LookUp(                   
                        attControl.Attachments,
//split file name, last item in the result array is the file extension
                    !(Last(Split(
                        Name,
                        "."
                    )).Result in allowedFileExtensions)
                )
            ),
            "",
            "Please upload only files using the following extensions: "&
            With({strList:Concat(allowedFileExtensions,Value&", ")},Left(strList,Len(strList)-2))
        )
    )
)

This will tweak the error message on the attachment card to show either the default error message or the custom message in case at least one attachment file has an extension that is not in the allowed list. In the example above we are allowing only pdf and docx files, but you can tweak the above formula and change the allowedFileExtensions array value to match the exact extensions list needed.

But this formula itself does not prevent the item to be submitted, we also need to edit the OnSave event formula for the SharePoint Integration.

Editing the OnSave event formula

The OnSave event formula is triggered any time the ‘Save‘ button in the SharePoint list form is clicked. By default it only submits the custom form, however, we can add additional logic to prevent the form with invalid attachments to be submitted.

Select the SharePointIntegration component, and edit the OnSave formula:

The following formula will validate if there is any error shown on the attachment card, and if so, notifies the user of the error. Otherwise submits the form normally:

If(
    !IsBlank(lblErrorAttachment.Text),
    Notify(
        lblErrorAttachment.Text,
        NotificationType.Error
    ),
    SubmitForm(SharePointForm1)
)

Now save and publish the form to be able to check it in the SharePoint list.

Final Result

The custom form will alert the user if any file with an extension not in the allowed list is added, and won’t allow the item to be saved in that case. If only files with allowed extensions are added, no alert is shown and the item can be saved normally:

Considerations

This approach will limit the file extensions when using SharePoint UI to manage the list items. However, bear in mind it will validate this when data is submitted through the SharePoint UI only, custom coded solutions would still be able to add attachments without the custom form restrictions.

2 comments

Leave a Reply

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