REVISED: Retrieve all of the field names in a Tab

Revised 4/16/2012: Added checks for control type to prevent exceptions.

 

I ran into a need to build a list of all of the fields found in all of the sections of a tab, within Dynamics CRM 2011.  Here is the JavaScript to do that:

function getFieldListFromTab(tabId)
{
    var fieldList = new Array();

    var tab = Xrm.Page.ui.tabs.get(tabId);

    tab.sections.forEach(function (section, sectionIndex)
    {
        section.controls.forEach(function (control, controlIndex)
        {
            switch (control.getControlType())
            {
                case "standard":
                case "lookup":
                case "optionset":
                    var attribute = control.getAttribute();

                    if (attribute != null)
                    {
                        fieldList.push(attribute.getName());
                    }
                    break;
            }
        });
    });

    return fieldList;
}

The field names are placed in a JavaScript Array.

Add this function to one of the web resources which has been linked to an Entity Form, and you can use it like this:

var fieldList = getFieldListFromTab(0);

alert(fieldList.length);

 

You can pass in the tab number, which is zero-based, or pass in the tab name.

In the example above, we are retrieving all of the fields in the first tab, then displaying the number of field names added to the array ( the length property ).

Leave a Reply 1 comment