如何从表单Tab中获取所有控件?已尝试代码无效求解决方案
Troubleshooting & Fix for Fetching Controls in a Specific Dynamics 365 Form Tab
Hey there! Let's break down why your current code isn't working and walk through solutions to get you the controls in your target tab.
First, Let's Diagnose Potential Issues with Your Original Code
Here are the most likely culprits:
- Deprecated
Xrm.PageAPI: In modern Dynamics 365 (unified interface),Xrm.Pageis no longer recommended—you should useformContextinstead. If your script runs in a modern form,Xrm.Pagemight not behave as expected. - Incorrect Tab Name Matching: Tab names in Dynamics are case-sensitive. Double-check that
"tab_2"exactly matches the tab's technical name (not the display name) from the form designer. - Loop Syntax for Tabs: Using
for (var i in tabs)can accidentally iterate over array prototype properties. It's safer to use a standardforloop orforEachfor array iteration. - Missing Control Types: Your
switchstatement only checks for "standard", "lookup", and "optionset"—you might be missing common types like "boolean", "multiselectoptionset", or "datetime". - Syntax Error: Your code has an extra closing
}at the end, which could cause it to fail silently.
Fixed Code Using formContext (Recommended for Modern Forms)
This version uses the current recommended API, fixes loop issues, and includes more control types:
function getControlsInSpecificTab(executionContext) { var formContext = executionContext.getFormContext(); var targetTabName = "tab_2"; var fieldList = []; // Directly fetch the target tab (more efficient than looping all tabs) var targetTab = formContext.ui.tabs.get(targetTabName); if (!targetTab) { console.log("Target tab not found: " + targetTabName); return fieldList; } // Iterate through all sections in the tab targetTab.sections.forEach(function(section) { // Iterate through all controls in the section section.controls.forEach(function(control) { var controlType = control.getControlType(); // Include common control types you might need var validControlTypes = ["standard", "lookup", "optionset", "boolean", "multiselectoptionset", "datetime", "decimal"]; if (validControlTypes.includes(controlType)) { var attribute = control.getAttribute(); if (attribute) { fieldList.push(attribute.getName()); } } }); }); console.log("Controls in tab " + targetTabName + ":", fieldList); return fieldList; }
Key Improvements:
- Uses
formContext(passed via the execution context from your script registration) - Directly fetches the target tab instead of looping all tabs
- Includes additional common control types
- Adds error checking if the tab doesn't exist
- Uses modern array methods for cleaner, more reliable code
If You Need to Support Legacy Forms (Xrm.Page)
If you're working in an older form where formContext isn't available, here's a fixed version of your original code:
function getControlsInSpecificTab() { var targetTabName = "tab_2"; var fieldList = []; var tabs = Xrm.Page.ui.tabs.get(); // Use a standard for loop to avoid prototype property issues for (var i = 0; i < tabs.length; i++) { var tab = tabs[i]; if (tab.getName() === targetTabName) { tab.sections.forEach(function(section) { section.controls.forEach(function(control) { switch (control.getControlType()) { case "standard": case "lookup": case "optionset": case "boolean": case "multiselectoptionset": var attribute = control.getAttribute(); if (attribute) { fieldList.push(attribute.getName()); } break; } }); }); break; // Exit loop once target tab is found } } console.log("Controls in tab " + targetTabName + ":", fieldList); return fieldList; }
Step-by-Step Troubleshooting Checklist
If the fixed code still doesn't work, try these steps:
- Verify the Tab's Technical Name: Go to the form designer, select the tab, and check the "Name" field in the right-hand properties pane—this is the value you need to use (not the display name like "Contact Details").
- Check Console for Errors: Press F12 to open browser dev tools, go to the Console tab, and run your function. Look for errors like "formContext is undefined" or "tab not found"—these will point you to the issue.
- Test in Console: Run small snippets to isolate problems, e.g.:
formContext.ui.tabs.get("tab_2")(orXrm.Page.ui.tabs.get("tab_2")) to confirm the tab exists.formContext.ui.tabs.get("tab_2").sections.get()to check if sections are being retrieved.
- Ensure Script Registration is Correct: For modern forms, make sure your script is registered to pass the execution context (check the "Pass execution context as first parameter" box when adding the script event).
内容的提问来源于stack exchange,提问作者Damkulul
相关产品推荐
相关产品推荐

