DEV Community

Cover image for Better Alternatives to localStorage in Dynamics 365 CE
Nikhil Sarpatwari
Nikhil Sarpatwari

Posted on

Better Alternatives to localStorage in Dynamics 365 CE

When working with model-driven apps or custom scripts in Dynamics 365 CE, you might be tempted to use localStorage to pass data between forms. It's quick—but also risky.

Why localStorage Can Be Problematic in Dynamics CE

Using localStorage in a Dynamics 365 CE context comes with several drawbacks:

  • Globally scoped: Shared across all tabs and users within the same domain.
  • Not secure: Vulnerable to XSS attacks—data is readable from browser dev tools.
  • Not lifecycle-aware: Doesn’t respect form/session boundaries in Dynamics.
  • Lacks cleanup: Data can persist longer than needed, causing logic conflicts.

Better Alternatives in Dynamics 365 CE

Here are more reliable and secure ways to pass data between forms:

1. formParameters with Xrm.Navigation.openForm

If you're opening a form programmatically (e.g., from a ribbon or custom button), use formParameters:

Xrm.Navigation.openForm({
  entityName: "contact",
  formParameters: {
    sourceOrderId: "12345"
  }
});
Enter fullscreen mode Exit fullscreen mode

Then, in the target form’s script, retrieve the value:

const formContext = executionContext.getFormContext();
const params = Xrm.Utility.getGlobalContext().getQueryStringParameters();
const sourceOrderId = params["sourceOrderId"];
Enter fullscreen mode Exit fullscreen mode

This keeps data scoped to the form session and avoids pollution of global storage.

2. Using Hidden Fields

If you're navigating between related records, you can store values temporarily in hidden fields:

  • Pre-populate the field via plugin/script or form logic.
  • Retrieve it on the next form using the standard getAttribute().getValue() method.

This method is native to Dynamics CE and supports workflows, Power Automate, and auditing.

3. Cross-Form Navigation via Ribbon Rules or URL Parameters

For navigation scenarios where you construct the URL manually (not recommended unless necessary), you can embed parameters:

const url = `/main.aspx?etn=contact&pagetype=entityrecord&extraqs=sourceOrderId%3D12345`;
Xrm.Navigation.openUrl(url);
Enter fullscreen mode Exit fullscreen mode

Then, read sourceOrderId from the query string using:

const params = Xrm.Utility.getGlobalContext().getQueryStringParameters();
const sourceOrderId = params["sourceOrderId"];
Enter fullscreen mode Exit fullscreen mode

Conclusion

While localStorage is easy to use, it's not designed for the Dynamics 365 CE ecosystem. Prefer built-in methods like formParameters or hidden fields to maintain security, user context, and platform consistency.

Top comments (0)