Navigating Forms with Code: Real-World Uses of getEntityMainFormDescriptor()

In model-driven Power Apps and Dynamics 365, working with multiple forms for the same table is a common scenario, especially when tailoring the experience for different roles, business units, or custom apps.

What if you could programmatically check:

  • What form is being used?
  • What sections or fields exist on it?
  • Whether it’s visible?
  • Whether a form is correctly configured before loading or switching?

That’s where Xrm.Utility.getEntityMainFormDescriptor() comes into play.

Let’s dive into real-world examples where this client API is more than just another tool, it’s a safeguard for great UX.


🧠 What It Does

Xrm.Utility.getEntityMainFormDescriptor(entityName, formId)

This function returns a Promise that resolves to a form descriptor object with the following data:

PropertyDescription
IdForm GUID
LabelForm display label
NameDisplay name of the form
Attributes[]List of all fields (columns) on the form
SectionsSections defined on the form
EntityLogicalNameTable’s logical name
ShowLabelWhether the form label is shown
VisibleWhether the form is visible

✅ Scenario 1: Validate Form Before Navigation

You want to navigate the user to a different form only if that form is enabled and contains certain fields.

function navigateIfFormValid(entityName, targetFormId) {
  Xrm.Utility.getEntityMainFormDescriptor(entityName, targetFormId)
    .then((formDescriptor) => {
      if (formDescriptor.Visible && formDescriptor.Attributes.includes("new_importantfield")) {
        Xrm.Page.ui.formSelector.items.get().forEach(item => {
          if (item.getId() === targetFormId) {
            item.navigate();
          }
        });
      } else {
        Xrm.Navigation.openAlertDialog({ text: "The selected form is not ready or missing required fields." });
      }
    });
}

🛡️ Why this matters: Prevent users from navigating to a misconfigured or hidden form.


✅ Scenario 2: Audit Form Configuration Across Environments

During Dev → Test → Prod deployments, form IDs are often hardcoded but may refer to different versions. This method helps you log what’s in each form.

function auditForm(entityName, formId) {
  Xrm.Utility.getEntityMainFormDescriptor(entityName, formId).then((desc) => {
    console.log(`Form Label: ${desc.Label}`);
    console.log(`Visible: ${desc.Visible}`);
    console.log(`Fields on form: ${desc.Attributes.join(", ")}`);
    console.log(`Sections: ${desc.Sections}`);
  });
}

🧪 Use this during QA or admin toolkits to verify metadata across environments.


✅ Scenario 3: Dynamically Adjust UI Logic Based on Fields Present

Suppose your JavaScript logic depends on fields that may exist only in certain forms. You can now check at runtime.

function toggleFieldIfPresent(entityName, formId, fieldName, executionContext) {
  const formContext = executionContext.getFormContext();

  Xrm.Utility.getEntityMainFormDescriptor(entityName, formId).then((desc) => {
    if (!desc.Attributes.includes(fieldName)) {
      formContext.getControl(fieldName)?.setVisible(false);
    }
  });
}

💡 Great for reusable form scripts that shouldn’t break if a field is absent.


✅ Scenario 4: Show Custom Warnings for Hidden Forms

Use this method to warn system customizers or developers if certain forms are no longer in use but still referenced by legacy scripts.

function warnIfFormHidden(entityName, formId) {
  Xrm.Utility.getEntityMainFormDescriptor(entityName, formId).then((desc) => {
    if (!desc.Visible) {
      console.warn('Warning: Form '${desc.Label}' [${desc.Id}] is hidden but still referenced.');
    }
  });
}

🧩 Use in admin tools or dev-only ribbons for visibility audits.

Leave a comment