How to filter Attachments to a SharePoint list on a Canvas PowerApps application?

You are a developer of Low-Code solutions on the Microsoft Power Platform using the Microsoft Power Apps tool to create applications that follow a defined business process, you use Microsoft SharePoint for the permanent backup of your data, it probably happened to you that you wanted for a given step of the process to save files from your application to the corresponding SharePoint list but PROBLEM: you can’t display particular files, all the files are displayed at the same time 😭.

Don’t panic, don’t change your need, change your approach 😉 !

So how to group attachments to a SharePoint list by categories on a Canvas PowerApps application?

Let’s start by talking about uploading files to PowerApps

On PowerApps one of the methods to work with files is to use the Attachments field which doesn’t offer us great possibilities other than uploading files and deleting files. The files contained in this field will be attached to an item in the SharePoint list used for the backup. This is how it works for basic use. If you want to avoid displaying certain files in this field, for example, this is one of the limitations of the Attachment field.

How to filter files in the Attachments field of PowerApps?

The application we want to make is the following:

1: The main camp that allows us to upload files

2, 3, 4 : The field that allows us to display files of a particular category

5 : Selection field that allows us to select the category of the file to import

Application objective

1.     Create a PowerApps canvas application

2.     Let’s create a SharePoint list to store our data

The structure of our list (EmployeeList) will be as follows

3.     Create a form on our PowerApps application based on our SharePoint list

4. Add our SharePoint list to the data sources of our application

EmployeeList

Then add our form and to its data source add our created SharePoint list.
Add four cards containing Attachments fields to our application, one of which is for selection and the other three for display by category.

5. Let’s add to our application the category selection field that we specify in the items property.

SortByColumns(
    ["CV";"Personal";"Receipt"];
    "Value";
    Ascending
)

 

6. Next we will define a collection in the OnStart property of the application

This collection must contain our attachments (we will see more in detail 😉 )

ClearCollect(

  colAttachments;

  Defaults(EmployeeList).'Pièces jointes'

)

By default this collection takes an empty row from our SharePoint list so we are unlikely to have any problems.

On our field that allows us to add attachments, let’s add the following settings

7. On the OnAddFile property Add this code:

//Define the collection with principal file picker contol
ClearCollect(
    colAttachments;
    Self.Attachments
);;

//Update the file collection if the current added file dont have the name which start with the value of a filter
UpdateIf(
    colAttachments;
    !(First(
        Split(
            DisplayName;
            "-"
        )
    ).Result in ["CV" ;"Personal";"Receipt"]);
    {
        Name: ddlFilter.Selected.Value & "-" & Name;
        DisplayName: ddlFilter.Selected.Value & "-" & DisplayName
    }
)

In this code we tell PowerApps to add the file if its display name does not start with one of our filters. If it does, then we add the file to the file collection by prefixing its name and display name with the value of the selected filter.

8. On the OnRemoveFile property, do :

//Define the collection with principal file picker contol
ClearCollect(
    colAttachments;
    Self.Attachments
);;
//Remove the file in the collection if the name look like the name with filter value
RemoveIf(
    colAttachments;
    ddlFilter.Selected.Value & "-" & Name = Name
)

In this code we tell PowerApps to remove the file from our collection if its name matches the prefixed name of the selected filter value.

9. The elements of this field are therefore “the collection of colAttachments files”.

//Collection of PJ
colAttachments

Now that everything is done on this field, so let’s configure which ones should display each file category.

10. The configuration to do is to filter the file collection according to a specific category. Like this:

Filter(
    colAttachments;
    (First(
        Split(
            DisplayName;
            "-"
        )
    ).Result = "Personal")
)

Personal” is the name of a defined category.

Filter(
    colAttachments;
    (First(
        Split(
            DisplayName;
            "-"
        )
    ).Result = "CV")
)

CV” is the name of a defined category.

Filter(
    colAttachments;
    (First(
        Split(
            DisplayName;
            "-"
        )
    ).Result = "Receipt")
)

“Receipt” is the name of a defined category.

In addition to the Items property, this field must be made editable to avoid confusion.

The result should be similar to this

Everything is set up, you can just test and enjoy the trick😊

 

Why use a filter like this?

For what reasons can we use this tip🤔❓

  • A SharePoint list has only one column for recording Attachments, however in your business application you need to retrieve files at multiple usage levels which poses the problem that one user can delete the file uploaded by another.
  • You want to email some files uploaded from PowerApps but it’s not when you set up the flow that it becomes difficult to implement
  • You are just a CURIOUS who wants to learn new tricks😁

Leave a Reply

Your email address will not be published.