Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 27

Offline-Enabled Canvas Apps with Collections

Recommended resources

Hello! Welcome back to your advanced Power Platform course.

In our last lesson, you learned how to create reusable UI components with component libraries. This is a cornerstone of efficient and maintainable app development, much like building shared class libraries in .NET.

Today, we shift from the user interface to data resilience. A polished UI is only useful if the app works when and where your users need it—and that often means in areas with poor or no internet connectivity. This lesson addresses that critical requirement. You will learn how to implement offline capabilities in a Canvas App using collections, LoadData/SaveData, and data synchronization patterns.

This skill is essential for building professional-grade applications for field service, inspections, or any scenario where users are on the move. Your experience with data structures and application state management in traditional development will be a great asset here as we explore the specific Power Fx functions and patterns used to achieve this.

1. The Core of Offline: Collections, SaveData, and LoadData

At the heart of Power Apps' offline functionality are three key elements:

  • Collections: In-memory tables that store your data within the app. You can think of these as analogous to a List<T> in C# or an array of objects in JavaScript. All offline data is staged in collections.
  • SaveData(CollectionName, "FileName"): This function takes a collection and saves it to a file in the local storage of the device running the app.
  • LoadData(CollectionName, "FileName", [IgnoreNonexistentFile]): This function reads a local file and loads its contents back into a collection.

A crucial point for development and testing: these functions are designed for the Power Apps mobile player (on iOS/Android) and the Power Apps for Windows player. They do not work in the browser-based App Studio by default.

Enabling SaveData, LoadData, and ClearData for Offline Canvas Apps
For testing purposes in a browser, you can enable an experimental feature. In your app's **Settings > Upcoming features > Experimental**, you can turn on 'Enable SaveData, LoadData, ClearData on web player'. Be aware that this data is stored unencrypted in the browser's cache.

Let's watch a video that introduces these concepts and shows how to use the fundamental functions.

PowerApps Offline Mode

Shane Young provides an excellent, developer-focused introduction to the core offline concepts. Pay close attention to his emphasis on using collections for everything and the importance of saving data immediately after modifying a collection.

Please watch the following segments: 06:55 - 09:05: This covers the role of collections, explains that offline functions only work in a player, and shows how to get the Windows player for testing. 09:50 - 14:04: This is a practical walkthrough of using Collect, SaveData, and LoadData with buttons to manually manage local data. 18:52 - 21:52: This demonstrates a critical best practice: calling SaveData immediately after any action that modifies your collection (Collect, Patch, Remove). Shane shows how this makes your app resilient to crashes, which is a key professional consideration.

As Shane emphasizes, the most robust pattern is to always call SaveData immediately after any function that alters your local collection. This ensures that even if the app crashes a moment later, the user's changes are persisted to the device.


2. A Standard Implementation Pattern

Now that you understand the core functions, let's assemble them into a standard pattern for loading, saving, and displaying data based on network connectivity. The logic generally follows this flow:

  1. On App Load: Check if the device is online using the Connection.Connected property.
    • If Online: Load fresh data from the data source (e.g., Dataverse) into a local collection. Then, use SaveData to create a local cache of this fresh data.
    • If Offline: Use LoadData to populate the collection from the local cache file.
  2. Saving a Record: When a user submits a form:
    • If Online: Patch the data directly to the data source.
    • If Offline: Collect or Patch the data into a local collection, and then immediately use SaveData to update the local cache file.

The following video demonstrates this entire flow in a practical example.

Develop Offline Capable Canvas PowerApps for Windows and Mobile Device

This video from Dhruvin Shah walks through building a simple offline-capable app from scratch. It's a great demonstration of the standard implementation pattern.

Watch the following sections to see the pattern in action: 05:43 - 08:00: Observe how the 'Save' button logic uses an If(Connection.Connected, ...) statement to decide whether to Patch to the data source or Collect to an offline collection and then call SaveData. 08:41 - 10:26: See how the screen's OnVisible property is used to load data. It checks for connectivity and either loads fresh data (online) or loads from the cache using LoadData (offline).


3. The Synchronization Process

You've successfully saved data offline. The final piece of the puzzle is synchronizing it back to the server when the user is back online. This is typically handled with a manual "Sync" button that becomes active only when Connection.Connected is true.

The sync logic involves:

  1. Iterating through the collection of offline records using the ForAll function.
  2. Inside the loop, using Patch to send each record to the data source.
  3. After the loop completes successfully, clearing the local data to prevent duplicate submissions. This involves Clear() on the collection and, ideally, ClearData("FileName") to remove the local file.

Let's see how this is implemented.

Develop Offline Capable Canvas PowerApps for Windows and Mobile Device

Returning to Dhruvin Shah's tutorial, let's focus on the implementation of the sync button.

Watch from 10:52 to 13:35. Pay close attention to the formula on the sync icon's OnSelect property. It demonstrates the ForAll loop to Patch records, followed by Clear and ClearData to clean up the local cache.

Test your understanding!

You've implemented the sync logic as shown in the video. A user reports that after they sync, go offline again, and then re-open the app, the records they just synced are gone from their view until they go online again. What might be causing this, and how would you fix it?

Show answer

The likely cause is that the synchronization logic only pushes data to the server but doesn't refresh the local cache from the server. After the ForAll loop successfully patches the data, the local collection and file are cleared. If the user goes offline again, the app loads from an empty cache.

The Fix: After successfully patching and clearing the offline changes, you should immediately re-fetch the data from the source into your main local collection and then use SaveData to update the cache. This ensures the local cache reflects the latest state from the server. The Canvas Apps Offline article below shows this exact pattern in its sync code block.


4. Professional Patterns for Robustness

The standard pattern is great for simple "create" scenarios. However, your background in enterprise development tells you that real-world apps need to handle creates, updates, and deletes, often in a multi-user environment. Here are two advanced patterns that provide greater robustness.

Pattern 1: Handling New Records with an OfflineID

When a user creates a new record offline, it doesn't have a primary key from your data source (e.g., the Dataverse GUID). To manage this, you can add a custom text column to your table (e.g., crXXX_OfflineID).

When creating a record:

  • Use the GUID() function in Power Fx to generate a unique ID.
  • Store this in your local collection and the crXXX_OfflineID column.
  • When syncing, you can use the Coalesce function to handle both creates and updates in a single Patch statement. It attempts to LookUp the record by its primary key; if it fails (i.e., it's a new record), it uses Defaults() to create it.

This pattern is well-documented in the following tutorial.

Power Apps Offline Mode: A Step-By-Step Tutorial

This step-by-step tutorial from Matthew Devaney uses a SharePoint list but the principle is identical for Dataverse. It introduces the crucial concept of using a self-generated GUID to manage new records created offline.

Please review the following sections: "Setup The SharePoint List": Note the creation and purpose of the OfflineID column. "Adding Records In Power Apps Offline Mode": Focus on how a GUID() is generated and used with NewForm and how Coalesce is used in the Patch function to handle both new and existing records gracefully.

Pattern 2: Tracking Individual Changes

A more sophisticated approach, especially for complex apps, is to maintain a separate collection that tracks only the changes. Instead of saving the entire dataset every time, you Collect a record into a dedicated OfflineChanges collection. Each item in this collection should include metadata about the operation.

Canvas Apps Offline: Leveraging SaveData and LoadData for ...

This article from Chris Worth outlines a robust pattern for tracking and synchronizing offline changes. This is similar to command queuing patterns in other software architectures.

Read the sections "Tracking Offline Changes" and "Synchronization". Notice how the OfflineChanges collection stores not just the data, but also the ChangeType (Create, Update, Delete). Examine the Sync button logic. It uses a ForAll loop combined with a Switch statement to process each change according to its type. This is a highly robust and extensible pattern.

This pattern provides a clean, explicit way to manage your data synchronization logic and is highly recommended for applications with complex offline requirements.


Conclusion

You have now learned the essential techniques for building resilient, offline-capable Canvas Apps. This is a non-negotiable skill for many enterprise mobile solutions and a key topic for the PL-400 exam.

Key Takeaways:

  • The Core Trio: SaveData, LoadData, and Collections are the foundation of all offline functionality in Canvas Apps.
  • Save Immediately: To prevent data loss from app crashes or closures, always use SaveData immediately after any operation that modifies a collection holding user data.
  • Check Connectivity: Use Connection.Connected to determine whether to operate against the live data source or the local cache.
  • Sync Manually: A user-initiated sync button provides the most reliable and transparent experience for pushing offline changes back to the server.
  • Use Robust Patterns: For production apps, adopt advanced patterns like using a temporary OfflineID for new records and tracking individual changes in a dedicated queue for reliable C/U/D (Create/Update/Delete) operations.

Up Next:

In the next lesson, we will explore another powerful integration technique: integrating Canvas Apps within Model-driven Apps using embedding and context passing. You'll learn how to combine the rich, flexible UI of a Canvas App with the structured, data-driven forms of a Model-driven App.