cancel
Showing results for 
Search instead for 
Did you mean: 
JR-BejeweledOne

Power Apps and Cookies, Not the kind you eat!

Do you ever find yourself wishing that you could harness the capabilities of ‘Cookies’ inside Power Apps to remember choices like not seeing a static notification every time you open a specific app?

 

This article will show you how to allow your users to ‘opt-out’ and remember their choice in a Power App.

 

Using a separate data source, a collection and a lookup we can effectively keep a record of everyone who chooses to opt-out.   The Lookup function doesn’t care how many rows are in the data source, making it an ideal option for those using SharePoint, as long as the formula only returns a single row.

 

Start by creating a new data source (SharePoint List, Table etc.) with a single text column to contain the identities of the users who have ‘opted out’ in the app and add it to the app.

  

NOTE: If you think you might want to expand the use of this to multiple apps and only want a single data source to manage it, add a second text column to identify the app each entry belongs to.  The end result should be an individual record per app for a user.   Example: If you have 5 apps using this functionality, you should have no more than 5 entries for a single user, 1 entry per app.

 

 

 1. Add the Office 365 Users Connector (if it hasn't already been added).   You could use the User() function, but please be aware that the information may not match the current user's information in Office 365 or other services.

 

         O365Userconnector.png

 

2. Add a checkbox for the 'Opt-Out' control

 

         OptOutControl.png

 

3.  In the App 'OnStart', add 2 new formulas

 

NOTE: When setting the user variable I recommend using the mail property, as that will be unique, where displayName might not be.  Substitute your information for CollectionName, DataSource, ColumnName and varXXX.   (Add an additional parameter to validate against the application column if you are using this for multiple apps).

 

  • Set(varXXX, Office365Users.MyProfileV2().mail)
  • ClearCollect(CollectionName, LookUp(DataSource, varXXX = ColumnName))
  • ClearCollect(CollectionName, LookUp(DataSource, varXXX = ColumnName And ColumnName = “AppName”

 

 

Example 1Example 1

 

Example 2Example 2

 

4.  Select the control you added in step 2 and in the OnCheck property, set this formula:

 

If(CountRows(CollectionName) = 0, Patch(DataSource, Defaults(DataSource), {Title: varXXX}))

Or

If(CountRows(CollectionName) = 0, Patch(DataSource, Defaults(DataSource), {Title: varXXX, AppName: "AppName"}))

 

NOTE: You may also wish to set an OnUncheck formula to remove the record from the data source if you want to ensure the user can still see the notifications in case they have selected the Opt-Out box by accident. (See Examples 2 & 3).  Don't forget to refresh your tracking collection :

  

Example 1Example 1

 

Example 2Example 2

 

Example 3Example 3

 

5.  Set the ‘Visible’ property on the controls you want to show/hide to this formula or add the red text into an existing

formula.

   

If(
     CountRows(CollectionName) >0, false,
     true
   )

 

 

Example 1Example 1

 

 

Example 2Example 2

 

You can easily test all of this on a blank screen before making major modifications to your app.

 

Simply add a button for the OnStart formulas, a checkbox for the OnCheck and OnUncheck formulas, an icon for the Visibility formula and a label to display CountRows(collectionname).

 

Create, modify, tweak and test, then once it works to your satisfaction, make the changes to your app.