Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Xrm.Page.ui tab Section Methods and Controls Collection

[Applies to: Microsoft Dynamics CRM 2011]

A section contains methods to manage how it appears as well as accessing the tab that contains the section. A section also provides access to the controls within it through a Controls Collection.

Examples shown below are in the Sample: SDK.SectionSamples.js library.

The following table lists all the methods of Sections.

 

Method Description

getLabel

Returns the label for the section.

getName

Returns the name of the section.

getParent

Returns the tab containing the section.

getVisible

Returns a value that indicates whether the section is visible.

setLabel

Sets the label for the section.

setVisible

Sets a value that indicates whether the section is visible.

getLabel

Returns the label for the section.

sectionObj.getLabel()
Return Value
Type: String

Example:The SDK.SectionSamples.showSectionLabels function loops through all of the sections in the first tab and displays their label.


SDK.SectionSamples.showSectionLabels = function () {
 var firstTabSections = Xrm.Page.ui.tabs.get(0).sections.get();

 var message = "The following sections are in the first tab on the form:\n\n";
 for (var i in firstTabSections) {
  var sectionLabel = firstTabSections[i].getLabel();
  if (sectionLabel == null) {
   sectionLabel = "No Section Label Defined";
  }

  message += "  \u2219 " + sectionLabel + "\n";
 }

 alert(message);
};

Methods

getName

Returns the name of the section.

sectionObj.getName()
Return Value
Type: String

Methods

getParent

Returns the tab containing the section.

sectionObj.getParent()
Return Value
Type: Object

Example:The SDK.SectionSamples.showSectionParents function will display a page in a new window showing the tab label values for sections on the page.


SDK.SectionSamples.showSectionParents = function () {
 var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Show Sections Parents</title>";
 html += "<style type=\"text/css\">body { font-family:Calibri;}";
 html += "table {border:1px solid gray; border-collapse:collapse;}";
 html += "th {text-align:left; border:1px solid gray;}";
 html += "td {border:1px solid gray;}</style>";
 html += "</head><body>";
 html += SDK.SectionSamples.getSectionsParents();
 html += "</body></html>";
 var theWindow = window.open("", "_blank", "height=400,width=450,scrollbars=1,resizable=1", false);
 theWindow.document.open();
 theWindow.document.write(html);
 theWindow.document.close();
};

SDK.SectionSamples.getSectionsParents = function () {
 var html = "<table summary='This table displays a list of sections and their parent tab label.'><thead><tr><th scope='col'>Section Label</th><th scope='col'>Tab Label</th>" +
    "</tr></thead><tbody>";
 var tabs = Xrm.Page.ui.tabs.get();
 for (var i in tabs) {
  var tab = tabs[i];

  tab.sections.forEach(function (section, index) {
   var sectionLabel = section.getLabel();
   if (sectionLabel == null) {
    sectionLabel = "No Section Label Defined";
   }

   var tabLabel = section.getParent().getLabel();

   html += "<tr><td>" + sectionLabel + "</td><td>" + tabLabel + "</td></tr>";
  });
 }
 html += "</tbody></table>";
 return html;
};

Methods

getVisible

Returns a value that indicates whether the section is visible.

sectionObj.getVisible()
Return Value
Type: Boolean

Example:The SDK.SectionSamples.toggleVisibleSections function hides or reveals all sections on the form each time the event occurs.


SDK.SectionSamples.toggleVisibleSections = function () {
 var tabs = Xrm.Page.ui.tabs.get();

 for (var i in tabs) {
  var tab = tabs[i];
  tab.sections.forEach(function (section, index) {
   if (section.getVisible()) {
    section.setVisible(false);
   }
   else {
    section.setVisible(true);
   }
  });
 }
};

Methods

setLabel

Sets the label for the section.

sectionObj.setLabel(string)
Arguments
String: The label text to set.

Example:The SDK.SectionSamples.toggleSectionLabels function will show or hide a specified prefix for all section labels on the form. If a section does not have a label, it will be ignored.


SDK.SectionSamples.toggleSectionLabels = function (prefix) {
 var tabs = Xrm.Page.ui.tabs.get();

 for (var i in tabs) {
  var tab = tabs[i];
  tab.sections.forEach(function (section, index) {
   var currentLabel = section.getLabel();
   if (currentLabel != null) {
    if (currentLabel.substring(0, prefix.length) == prefix) {
     var newLabel = currentLabel.substring(prefix.length);
     section.setLabel(newLabel);
    }
    else {
     section.setLabel(prefix + currentLabel);
    }
   }
  });
 }
};

Methods

setVisible

Sets a value that indicates whether the section is visible.

sectionObj.setVisible(Boolean)
Arguments
Boolean

Example: See the SDK.SectionSamples.toggleVisibleSections function under getVisible above.

Methods

The section controls collection provides access to the controls within a section. This collection implements the same methods as the Xrm.Page.ui.controls Collection except that the scope is limited to controls within the section.

Example:The SDK.SectionSamples.getMultipleControlAttributes function alerts the user of any attributes that have multiple controls on the form.


SDK.SectionSamples.getMultipleControlAttributes = function () {
 var attributesOnForm = [];
 var multipleControlAttributes = [];

 var tabs = Xrm.Page.ui.tabs.get();
 for (var i in tabs) {
  var tab = tabs[i];
  var sections = tab.sections.get();

  for (var j in sections) {
   var section = sections[j];

   section.controls.forEach(function (control, index) {
    if (SDK.SectionSamples.doesControlHaveAttribute(control)) {

     var attribute = control.getAttribute();
     if (attribute != null) {
      var attributeName = attribute.getName();

      // Check if the attribute has already been added to the attributesOnForm collection
      if (SDK.SectionSamples.arrayContainsValue(attributesOnForm, attributeName)) {
       // Check if the attribute has already been added to the
       // multipleControlAttributes collection.  This would happen
       // if an attribute has 3+ controls on the form.
       if (SDK.SectionSamples.arrayContainsValue(multipleControlAttributes, attributeName) == false) {
        multipleControlAttributes.push(attributeName);
       }
      }
      else {
       attributesOnForm.push(attributeName);
      }
     }
    }
   });
  }
 }


 var message = "";
 if (multipleControlAttributes.length > 0) {
  message = "The following attributes have multiple controls on the form:\n\n- ";
  message += multipleControlAttributes.join("\n- ");
 }
 else {
  message = "There are no attributes on the form with multiple controls";
 }

 alert(message);
};

SDK.SectionSamples.doesControlHaveAttribute = function (control) {
 var controlType = control.getControlType();
 return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid" && controlType != "notes";
};

SDK.SectionSamples.arrayContainsValue = function (array, value) {
 for (var i in array) {
  if (array[i] == value)
   return true;
 }

 return false;
};

Example:The SDK.SectionSamples.getFirstControlAttribute function alerts the user with the attribute label of the first control on the form.


SDK.SectionSamples.getFirstControlAttribute = function () {
 var firstControl = Xrm.Page.ui.tabs.get()[0].sections.get()[0].controls.get()[0];

 alert("The attribute label of the first control on the form is '" + firstControl.getLabel() + "'.");
};

Example:The SDK.SectionSamples.getAllLookupAttributes function alerts the user with the attribute name for every lookup control on the form.


SDK.SectionSamples.getAllLookupAttributes = function () {
 var message = "The following lookup attributes are on the form:\n\n";

 var lookupControls = [];

 var tabs = Xrm.Page.ui.tabs.get();
 for (var i in tabs) {

  var tab = tabs[i];
  var sections = tab.sections.get();

  for (var j in sections) {
   var section = sections[j];

   var sectionLookupControls = section.controls.get(SDK.SectionSamples.isLookup);
   for (var k in sectionLookupControls) {
    lookupControls.push(sectionLookupControls[k]);
   }
  }
 }

 for (var i in lookupControls) {
  message += "- " + lookupControls[i].getLabel() + "\n";
 }

 alert(message);
};

SDK.SectionSamples.isLookup = function (control, index) {
 return control.getControlType() == "lookup";
};

Example:The SDK.SectionSamples.getFirstControlDemo function gets the first control on the form, and then using the attribute name from the control, gets the control by name, and compares the two.


SDK.SectionSamples.getFirstControlDemo = function () {
 var firstControlByPosition = Xrm.Page.ui.tabs.get()[0].sections.get()[0].controls.get(0);
 var firstControlByName = Xrm.Page.ui.tabs.get()[0].sections.get()[0].controls.get(firstControlByPosition.getName());

 if (firstControlByName == firstControlByPosition) {
  alert("Getting the first control on the form by position and name returns the same control.");
 }
 else {
  alert("Getting the first control on the form by position and name does not return the same control.");
 }
};

Example:The SDK.SectionSamples.getControlCount function alerts the user of how many controls are on the current form.


SDK.SectionSamples.getControlCount = function () {
 var controlsLength = 0;

 var tabs = Xrm.Page.ui.tabs.get();
 for (var i in tabs) {

  var tab = tabs[i];
  var sections = tab.sections.get();

  for (var j in sections) {
   var section = sections[j];

   controlsLength += section.controls.getLength();
  }
 }

 alert("This form has " + controlsLength + " controls on it.");
};

Methods

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.