Enforcing file types in Power Apps attachment control

When using SharePoint to create list items with attachments, it does not allow you to enforce a certain file type (eg. PDF only). Even when using Power Apps, the attachment control does not provide you with an option to enforce this. This blog will describe how you can achieve that.

Enforcing a certain file type using Power Apps

As said, the attachment control in Power Apps does not provide you with an option to restrict and/or enforce file types, which means everyone can attach any file type to your list item. In this example I will show you how to only allow PDF files.

When you open your customized form in Power Apps, select your SharePointIntegration entity and choose the OnSave attribute. By default, this will only contain the SubmitForm() function.

We need to change this formula so that the form will only be submitted when there are no other file types than PDF added.

Tip: when first building your form, add a Save icon to your form and build your formula into the OnSelect attribute of that button. When done, hide the save button and copy your function to the OnSave attribute!   

Collect incorrect attachments

First, we need to collect all of the the attachments that don’t have the correct file type into a single collection by using the following formula:

ForAll(
    DataCardValue2.Attachments.Name,
    If(
        Not(".pdf" in Name),
        Collect(
            collIncorrectAttachments,
            {Name: Name}
        )
    )
);

Where DataCardValue2 is our attachment control.

Check if there are incorrect file type attachments

Next, we need to check if there are items inside the collIncorrectAttachments collection. If so, we need to set a variable (that we use later to show a custom error message) to true. If not, we can submit the form to SharePoint. This can be done by using the following formula:

If(
    CountRows(collIncorrectAttachments) > 0,
    Set(
        varAttachmentsIncorrect,
        true
    );
    Clear(collIncorrectAttachments),
    Set(
        varAttachmentsIncorrect,
        false
    );
    Clear(collIncorrectAttachments);
    SubmitForm(SharePointForm1)
)

Breaking this down into smaller pieces:

  • CountRows(collIncorrectAttachments) > 0
    This will check if our incorrect attachments collection contains items
  • Set(varAttachmentsIncorrect, true); Clear(collIncorrectAttachments)
    If the condition is met, this will set our variable to true and empties the incorrect attachments collection.
  • Set(varAttachmentsIncorrect, false); Clear(collIncorrectAttachments); SubmitForm(SharePointForm1)
    If the condition isn’t met, this will set our variable to false, empties the incorrect attachments collection and submits the form to SharePoint.

OnSave function

The entire OnSave function (or OnSelect if you’re using an icon) is as follows:

ForAll(
    DataCardValue2.Attachments.Name,
    If(
        Not(".pdf" in Name),
        Collect(
            collIncorrectAttachments,
            {Name: Name}
        )
    )
);
If(
    CountRows(collIncorrectAttachments) > 0,
    Set(
        varAttachmentsIncorrect,
        true
    );
    Clear(collIncorrectAttachments),
    Set(
        varAttachmentsIncorrect,
        false
    );
    Clear(collIncorrectAttachments);
    SubmitForm(SharePointForm1)
)

Add a custom error message

With the above in place, the form will not be submitted if there are attachments added that are not in PDF format. In that case, we want to give the user some information about why nothing is happening after clicking on Save. To do this, we need to add a new Label with a custom error message and only show in when there are attachments added that are not in PDF format.

You can configure the label as you wish (color, text, position, etc.), but you need to make sure to set the Visible attribute of your label to the value of our varAttachmentsIncorrect variable:

With everything in place, you should now have a PDF enforced attachment control in your SharePoint list!
(if you used the save icon for testing: don’t forget to copy your formula from the icon OnSelect of your icon to the OnSave of your SharePointIntegration and hide the save icon!)

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.