January 12, 2021

Allow users to upload only specific extension files in Power Apps Attachment control

Introduction:

In this blog, we will learn how to allow users to upload only specific extension files in Power Apps Form Attachment control. The example here we have carried in this blog is to allow users to upload only the ".pdf" extension files.

Resolution:

Below are the steps we are going to work on here!

1.     Set restriction on uploading other extension files in Attachment control and notify the user when they upload any extension file other than ".pdf".

2.     Restrict the user from saving until resolving file errors.

Step 1: Set restriction on uploading other extension files in Attachment control

Place the below code at OnAddFile property of the Attachment control:

 If(  
   CountRows(  
     Filter(  
       RenameColumns(  
         DataCardValue4.Attachments,  
         "Name",  
         "NewName"  
       ),  
       Last(  
         Split(  
           NewName,  
           "."  
         )  
       ).Result <> "pdf"  
     )  
   ) > 0,  
   Notify(  
     "Only PDF files can be attached",  
     NotificationType.Error  
   );  
   ,  
   SubmitForm(Form1);  
   ResetForm(Form1);  
   EditForm(Form1);  
   )  

After placing this, check by adding a different extension file in the attachment control. You will be able to see this type of error at the top of the app.


Now as we have set the restriction message, let’s restrict the user from saving the form until all the attachments in the control are ".pdf".

Step2: Restrict user from saving until resolving file errors

Place the below code in the OnSelect property of the save button or in the OnSave property of SharePointIntegration:

  If(  
   CountRows(  
     Filter(  
       RenameColumns(  
         DataCardValue4.Attachments,  
         "Name",  
         "NewName"  
       ),  
       Last(  
         Split(  
           NewName,  
           "."  
         )  
       ).Result <> "pdf"  
     )  
   ) > 0,  
   Notify(  
     "Only PDF files can be attached",  
     NotificationType.Error  
   );  
   ,  
   SubmitForm(Form1);  
   ResetForm(Form1);  
   EditForm(Form1);  
   )  

Now add a different file extension in the control and click on save. It will show the below error and won’t let the user save the item.

Conclusion

This is how we can restrict users to upload only ".pdf" extension files in the Power Apps - Form Attachment Control.

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment