Forum Discussion

DavidAnthony's avatar
DavidAnthony
Helper II
1 year ago
Solved

Save toggle state in custom Power BI visual without using data fields

Hi everyone, I am currently developing a custom Power BI visual, specifically a toggle with an interface rendered entirely in my visual. This toggle does not use any data fields from Power BI, nor d...
  • v-sgandrathi's avatar
    1 year ago

    Hi DavidAnthony,

    Thank you for Using Microsoft Fabric Community Forum.

    To save the toggle state (on/off) in your custom Power BI visual using bookmarks without relying on data fields, you can use persistProperties and enumerateObjectInstances in your visual’s source code.

     

    1.  Use the visual settings (object model) to store the toggle state.

      this.host.persistProperties({
      merge: [{
      objectName: "toggleSettings",
      properties: {
      isToggledOn: this.toggleState
      },
      selector: null
      }]
      });
    2. Define object enumeration for the state:

      public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration {
      const instances: VisualObjectInstance[] = [];
      if (options.objectName === "toggleSettings") {
      instances.push({
      objectName: "toggleSettings",
      properties: {
      isToggledOn: this.toggleState
      },
      selector: null
      });
      }
      return instances;
      }
    3. Declare toggle settings in capabilities.json:

      "objects": {
      "toggleSettings": {
      "displayName": "Toggle Settings",
      "properties": {
      "isToggledOn": {
      "type": { "bool": true }
      }
      }
      }
      }

    4. Restore toggle state in update method:

      this.toggleState = options.properties?.toggleSettings?.toggleState ?? false;

    When the visual’s toggle is switched, the state is persisted in the visual’s object model. When a bookmark is created, Power BI captures this state. Restoring the bookmark restores the toggle’s UI state.

    I hope this helps you build intuitive and bookmark-friendly toggles in your Power BI custom visuals!

     

    This link explains how to use the persistProperties method to save and restore visual settings, which is essential for maintaining UI state across sessions and bookmarks.

    Visual API for Power BI Visuals - Power BI | Microsoft Learn

    This link says how custom visuals can support bookmarks by correctly saving and retrieving the necessary information when required.
    Add bookmark support for Power BI custom visuals - Power BI | Microsoft Learn

     

    Below are some resolved issues related to the use of persistProperties that you may refer to, they might help:

    Solved: Power BI Custom Visual state persistence - Microsoft Fabric Community

    Solved: persistProperties and stored data persistency - Microsoft Fabric Community

     

    I hope my suggestions provided valuable insights. If you have any further questions, don’t hesitate to ask in a follow-up message.
    If this post helped, please mark it as "Accept as Solution" so others can benefit as well.

     

    Best regards,
    Sahasra

    Community Support Team.