SaveData, LoadData, and ClearData functions

Applies to: Canvas apps

Saves and reloads a collection from the app host's storage.

Note

These functions can now be used when playing an app in a web browser as an experimental feature. This feature is disabled by default. To enable, navigate to Settings > Upcoming features > Experimental > Enabled SaveData, LoadData, ClearData on web player." and turn the switch on. To submit feedback regarding this experimental feature, go to Power Apps community forum.

Description

The SaveData function stores a collection for later use under a name.

The LoadData function reloads a collection by name that was previously saved with SaveData. You can't use this function to load a collection from another source.

The ClearData function clears the storage under a specific name or clears all storage associated with the app if no name is provided.

Note

  • The name shared between SaveData, LoadData, and ClearData is a key, not a file name. It need not be complex as names are unique to each app and there is no danger of name conflict. The name must not contain any of these characters: *".?:\<>|/.
  • SaveData is limited to 1 MB of data for Power Apps running in Teams and in a web browser. There is no fixed limit for Power Apps running in a mobile player but there are practical limits discussed below.
  • Don't use SaveData to store sensitive data in the web since it'll be stored in plain text.

Use these functions to improve app-startup performance by:

  • Caching data in the App.OnStart formula on a first run.
  • Reloading the local cache on next runs.

You can also use these functions to add simple offline capabilities to your app.

You can't use these functions inside a browser when:

  • Authoring the app in Power Apps Studio.

To test your app, run it in Power Apps Mobile on an iPhone or Android device.

These functions are limited by the amount of available app memory as they operate on an in-memory collection. Available memory can vary depending on factors such as:

  • The device and operating system.
  • The memory that the Power Apps player uses.
  • Complexity of the app with screens and controls.

Test your app with expected scenarios on the type of devices you expect the app to run when storing large data. Expect to have between 30 MB and 70 MB of available memory generally.

These functions depend on the collection being implicitly defined with Collect or ClearCollect. You don't need to call Collect or ClearCollect to load data into the collection for defining it. It's a common case when using LoadData after a previous SaveData. All that is needed is the presence of these functions in a formula to implicitly define the structure of the collection. For more information, see creating and removing variables.

The loaded data will be appended to the collection. Use the Clear function before calling LoadData if you want to start with an empty collection.

Data security

Consider carefully the isolation and encryption of data stored with SaveData and decide if it's appropriate for your needs, especially if devices are shared by multiple users.

Data stored with SaveData is isolated from other Power Apps by the Power Apps players. Data is stored based on the app's App ID, automatically isolating the SaveData name space between Power Apps.

The operating system and browser is responsible for isolating data between Power Apps and other apps on a device and with websites. For example, the operating system is responsible for isolating data stored in Microsoft Outlook from data stored in Power Apps, and also isolating that data from websites such as Bing.com or PowerApps.com. The operating system's built in app sandbox facilities are used for SaveData storage which is usually not accessible to or hidden from the user.

When using the same app, the operating system and browser is also responsible for isolating the data between different operating system level users. For example, if two different users share a computer and use two different Windows login credentials, the operating system is responsible for isolating data between the two Windows users.

Data may or may not be isolated between different Power Apps users if the operating system user is the same. Not every Power Apps player treats this the same way. For example, while logged in as the same Windows user, in the Power Apps player, the user signs out of Power Apps and signs in as a different Power Apps user. Data stored in an app before the change of Power Apps user, may be accessible to the second Power Apps user within the same app. The data may also be removed and the first Power Apps user may no longer be able to access it. The behavior varies between Power Apps players.

The operating system may also encrypt the data or you can use a mobile device management tool such as Microsoft Intune. Data stored when playing an app in a web browser is not encrypted.

Syntax

SaveData( Collection, Name )
LoadData( Collection, Name [, IgnoreNonexistentFile ])

  • Collection - Required. Collection to be stored or loaded.
  • Name - Required. Name of the storage. The name must be same to save and load same set of data. The name space isn't shared with other apps. Names must not contain any of these characters: *".?:\<>|/.
  • IgnoreNonexistentFile - Optional. A Boolean value indicating what to do if the file doesn't already exist. Use false (default) to return an error and true to suppress the error.

ClearData( [Name] )

  • Name - Optional. Name of the storage previously saved with SaveData. If Name is not provided, all storage associated with the app is cleared.

Examples

Formula Description Result
SaveData( LocalCache, "MyCache" ) Save the LocalCache collection to the user's device under the name "MyCache", suitable for LoadData to retrieve later. Data is saved to the app host under the name "MyCache".
LoadData( LocalCache, "MyCache" ) Loads the LocalCache collection from the user's device under the name "MyCache", previously stored with a call to SaveData. Data is loaded from the app host under the name "MyCache".
ClearData( "MyCache" ) Clears the storage under the name "MyCache". Any data stored under this name will no longer be available through LoadData. Data is removed from the app host under the name "MyCache".
ClearData() Clear all storage associated with this app. Data stored by other apps is not affected. All data is removed from the app host.

Simple offline example

Following simple example captures and stores the names and pictures of everyday items while offline. It stores the information in the device's local storage for later use. This allows the app to be closed or the device to restart without losing data.

Note

This example uses a camera control to capture images. Since SaveData is limited to 1 MB of data when running in Teams or a web browser, this example will not work with more than a few images. Also, depending on the camera, it may not work with even one image. Use a device to work through this full example, or remove the camera control and picture part of this example to run in Teams or in a web browser.

  1. Create a blank canvas app with a tablet layout. For more details, read creating an app from a template and select Tablet layout under Blank app.

  2. Add a Text input control and a Camera control and arrange them roughly as shown:

    A text input and camera control added to a blank screen.

  3. Add a Button control.

  4. Double-click the button control to change the button text to Add Item (or modify the Text property).

  5. Set the OnSelect property of the button control to this formula that will add an item to our collection:

    Collect( MyItems, { Item: TextInput1.Text, Picture: Camera1.Photo } )
    

    A button control added with the text "Add Item" and the OnSelect property set

  6. Add another Button control.

  7. Double-click the button control to change the button text to Save Data (or modify the Text property).

  8. Set the OnSelect property of the button control to this formula in order to save our collection to the local device:

    SaveData( MyItems, "LocalSavedItems" )
    

    A button control added with the text "Save Data" and the OnSelect property set

    It's tempting to test the button as it doesn't affect anything. But you'll only see an error as you're authoring in a web browser. Save the app first and open on a device before you follow the next steps to test this formula:

  9. Add a third Button control.

  10. Double-click the button control to change the button text to Load Data (or modify the Text property).

  11. Set the OnSelect property of the button control to this formula in order to load our collection from the local device:

    LoadData( MyItems, "LocalSavedItems" )
    

    A button control added with the text "Load Data" and the OnSelect property set

  12. Add a Gallery control with a Vertical layout that includes a picture and text areas:

    Gallery variety selection, "Vertical" selected with image and text areas

  13. When prompted, select the MyItems collection as the data source for this gallery. This will set the Items property of the Gallery control:

    Gallery selection of data source. The image control in the gallery template should default its Image property to ThisItem.Picture and the label controls should both default their Text properties to ThisItem.Item. Check these formulas if after adding items in the following steps you don't see anything in the gallery.

  14. Position the control to the right of the other controls:

    Gallery repositioned to the right of the screen.

  15. Save your app. If it's the first time it has been saved, there's no need to publish it. If it's not the first time, publish the app after you save.

  16. Open your app on a device such as a phone or tablet. SaveData and LoadData can't be used in Studio or in a web browser. Refresh your app list if you don't see your app immediately, it can take a few seconds for the app to appear on your device. Signing out and back in to your account can help too.

    App running with no items added. Once your app has been downloaded, you can disconnect from the network and run the app offline.

  17. Enter the name and take a picture of an item.

  18. Select the Add Item button. Repeat adding items a couple of times to load up your collection.

    App running with three items added.

  19. Select the Save Data button. This will save the data in your collection to your local device.

  20. Close the app. Your collection in memory will be lost including all item names and pictures, but they'll still be there in the device's storage.

  21. Launch the app again. The collection in memory will again show as empty in the gallery.

    App again running with no items added.

  22. Select the Load Data button. The collection will be repopulated from the stored data on your device and your items will be back in the gallery. The collection was empty before this button calls the LoadData function; there was no need to call Collect or ClearCollect before loading the data from storage.

    App running with three items restored after calling the LoadData function.

  23. Select the Load Data button again. The stored data will be appended to the end of the collection and a scroll bar will appear on the gallery. If you would like to replace rather than append, use the Clear function first to clear out the collection before calling the LoadData function.

    App running with six items restored after calling the LoadData function twice.

More advanced offline example

For a detailed example, see the article on simple offline capabilities.