cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
New Member

Filter Gallery by Current User

Hi everyone, 

 

I'm trying to figure out how to filter the gallery so that the current user only sees its own data entries.

 

I've read many posts in the forum but none was able to provide me the correct solution.

 

This is what I did so far:

 

Set(VarUser; User())                                                                                                                     //declared in App

 

 

SortByColumns(                                                                                                                          //in Items in Gallery
      Search(
          Filter(
              [@MonthlyStatement];
             "EmployeeName" = VarUser.FullName);
             TextSearchBox1.Text;
             "crb06_MonthlyStatementID");
             "crb06_MonthlyStatementID";
             If(SortDescending1; Descending; Ascending)
)

 

This shows me no results at all even though the entry was successful.

 

This is the code without the filter and it's working properly:

SortByColumns(                                                                                                                          //in Items in Gallery
      Search(
              [@MonthlyStatement];
             TextSearchBox1.Text;
             "crb06_MonthlyStatementID");
             "crb06_MonthlyStatementID";
             If(SortDescending1; Descending; Ascending)
)

 

My table from which I'm retrieving the data looks like this:

 
MonthlyStatementIDEmployeeNameProject
0001Jack WickData security

 

 

The data is connected via Dataverse.

 

Any suggestion on what I could try?

 

Thanks!

 
2 ACCEPTED SOLUTIONS

Accepted Solutions
Super User II
Super User II

@tim_sch 

The solution that works for me is to create a global variable in the OnStart of the app.

Set(useremail, User().Email) or Set(useremail;User().Email)

 Then the Items property of the gallery

 

Filter(
        AddColumns(
                     Orders, "UsersEmail",'Created By'.'Primary Email'
         ), UsersEmail = User().Email
)

 

This takes advantage of the metadata stored in a Dataverse table for the individual who created the record in the Orders table.  In your case:

With(
     {wMonthlyStatement:Filter(
                               AddColumns(
                                          [@MonthlyStatement];"UsersEmail";'Created By'.'Primary Email'
                               ); UsersEmail = User().Email
                         )
      };

  SortByColumns(                                                                                                                  
                Search(
                        wMonthlyStatement;
                        TextSearchBox1.Text;
                        "crb06_MonthlyStatementID" 
                );
                  "crb06_MonthlyStatementID";
                  If(SortDescending1; Descending; Ascending
                  )
   )
)

View solution in original post

Super User II
Super User II

Yep,

You need to use

Lookup([@monthlystatement],ID= Browsegallery1.Selected.ID)

Since you changed the datasource in the gallery it is no longer identical to the datasource in the form so you need a more specific item property that matches the datasource.

View solution in original post

7 REPLIES 7
Super User II
Super User II

@tim_sch 

The solution that works for me is to create a global variable in the OnStart of the app.

Set(useremail, User().Email) or Set(useremail;User().Email)

 Then the Items property of the gallery

 

Filter(
        AddColumns(
                     Orders, "UsersEmail",'Created By'.'Primary Email'
         ), UsersEmail = User().Email
)

 

This takes advantage of the metadata stored in a Dataverse table for the individual who created the record in the Orders table.  In your case:

With(
     {wMonthlyStatement:Filter(
                               AddColumns(
                                          [@MonthlyStatement];"UsersEmail";'Created By'.'Primary Email'
                               ); UsersEmail = User().Email
                         )
      };

  SortByColumns(                                                                                                                  
                Search(
                        wMonthlyStatement;
                        TextSearchBox1.Text;
                        "crb06_MonthlyStatementID" 
                );
                  "crb06_MonthlyStatementID";
                  If(SortDescending1; Descending; Ascending
                  )
   )
)

View solution in original post

New Member

@Drrickryp 

 

Thanks for the reply. This worked.

Now I still have two questions though:

 

- Why did we set the global variable usermail and then didn't use it in the filter but used?

UsersEmail = User().Email

 
- Now if I want to see the monthly statements that I created, Power Apps displays an error that the formula 

BrowseGallery1.Selected

 is invalid because a "DataSource" compaitble values was expected. The name "BrowseGallery1" is correct.

Do you know how I can fix this?

Super User II
Super User II

Yep,

You need to use

Lookup([@monthlystatement],ID= Browsegallery1.Selected.ID)

Since you changed the datasource in the gallery it is no longer identical to the datasource in the form so you need a more specific item property that matches the datasource.

View solution in original post

Super User II
Super User II

@tim_sch 

With regard to the usersEmail I pasted an older version but should have used the variable instead. The app will work much more efficiently if the variable userEmail replaces User().Email.

New Member

@Drrickryp   this worked perfectly as well.

Nonetheless, if I actually try to use the global variable useremail then it doesn't work because it says that "This formula has side effects and cannot be evaluated" and it has created an internal error evaluating the expression.

Set( UserEmail; User().Email )

 

If I use the other version, i.e. no global variable and therefore

 

UsersEmail = User().Email

 

it works with no error.

 

Super User II
Super User II

@tim_sch 

Very odd.  I wonder if you Set(xxx;User().Email) in the OnStart and in the formula 

AddColumns(
                                          [@MonthlyStatement];"UsersEmail";'Created By'.'Primary Email'
                               ); UsersEmail = xxx

 whether that would make a difference.  Setting a global variable for a single value should reduce the communication between the device and the server every time the formula is used. 

@Drrickryp 

I did exactly that. I even tried to just create the variable 

Set(varUser; User())

and then in the gallery view use

UsersEmail = varUser.Email

 but that didn't work either. 

For now my app is not very complex, so I assume it's not going to be a big deal but it's still very strange.

Helpful resources

Announcements
New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

Power Apps Community Call

Power Apps Community Call: February

Did you miss the call? Check out the Power Apps Community Call here.

Top Solution Authors
Top Kudoed Authors
Users online (72,032)