Sample: SDK.UISamples.js

[Applies to: Microsoft Dynamics CRM 2011]

This sample code is for Microsoft Dynamics CRM 2011, and can be found in the following location in the SDK download: SDK\SampleCode\JS\FormScripts\SDK.UISamples.js

Requirements

This sample code represents a JScript library that can be added as a Web resource. Before a function in a JScript library can be used as a form event handler, the JScript library must be added to the form libraries available for that form. The full name of the function must be used in the form event handler. For example, the getCurrentControl function must be called using SDK.UISamples.getCurrentControl.

Demonstrates

This library contains examples of the following Xrm.Page.ui methods:

Example

//If the SDK namespace object is not defined, create it.
if (typeof (SDK) == "undefined")
{ SDK = {}; }
// Create Namespace container for functions in this library;
SDK.UISamples = {};

 // Xrm.Page.ui.getCurrentControl() example
// Example: The SDK.UISamples.getCurrentControl function alerts the user with the name of the attribute that currently has focus on the form.
SDK.UISamples.getCurrentControl = function () {
 var currentControl = Xrm.Page.ui.getCurrentControl();
 if (currentControl == null) {
  alert("No controls currently have focus.");
 }
 else {
  alert("The control for the '" + currentControl.getLabel() + "' attribute currently has focus.");
 }
};

 // Xrm.Page.ui.getFormType() example
// Example: The SDK.UISamples.getFormType function alerts the user with a different message depending on the current form type.
SDK.UISamples.getFormType = function () {

 var FORM_TYPE_CREATE = 1;
 var FORM_TYPE_UPDATE = 2;
 var FORM_TYPE_READ_ONLY = 3;
 var FORM_TYPE_DISABLED = 4;
 var FORM_TYPE_QUICK_CREATE = 5;
 var FORM_TYPE_BULK_EDIT = 6;
 var FORM_TYPE_READ_OPTIMIZED = 11;

 var formType = Xrm.Page.ui.getFormType();
 if (formType == FORM_TYPE_CREATE) {
  alert("This record has not yet been created.");
 }
 else {
  alert("This record exists in the database.");
 }
};

 // Xrm.Page.ui.getViewPortHeight() example
 // Xrm.Page.ui.getViewPortWidth() example
// Example: The SDK.UISamples.showViewPortSize function displays the current view port width and height to the user an alert.
SDK.UISamples.showViewPortSize = function () {
 var viewPortHeight = Xrm.Page.ui.getViewPortHeight();
 var viewPortWidth = Xrm.Page.ui.getViewPortWidth();
 alert("Width: " + viewPortWidth + ", Height: " + viewPortHeight);
};


 // Xrm.Page.ui.controls.forEach(function(control, index)) example
// Example: The SDK.UISamples.getMultipleControlAttributes function alerts the user of any attributes that have multiple controls on the form.
SDK.UISamples.getMultipleControlAttributes = function () {
 var attributesOnForm = [];
 var multipleControlAttributes = [];

 Xrm.Page.ui.controls.forEach(function (control, index) {
  if (SDK.UISamples.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.UISamples.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.UISamples.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.UISamples.doesControlHaveAttribute = function (control) {
 var controlType = control.getControlType();
 return controlType != "iframe" && controlType != "webresource" && controlType != "subgrid";
};

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

 return false;
};

 // Xrm.Page.ui.controls.get() example
// Example: The SDK.UISamples.getFirstControlAttribute function alerts the user with the attribute label of the first control on the form.
SDK.UISamples.getFirstControlAttribute = function () {
 var firstControl = Xrm.Page.ui.controls.get()[0];

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

 // Xrm.Page.ui.controls.get(delegate function(control, index)) example
// Example: The SDK.UISamples.getAllLookupAttributes function alerts the user with the attribute name for every lookup control on the form.
SDK.UISamples.getAllLookupAttributes = function () {
 var message = "The following lookup attributes are on the form:\n\n";

 var lookupControls = Xrm.Page.ui.controls.get(SDK.UISamples.isLookup);
 for (var i in lookupControls) {
  message += "- " + lookupControls[i].getLabel() + "\n";
 }

 alert(message);
};

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

 // Xrm.Page.ui.controls.get(number) example
 // Xrm.Page.ui.controls.get(index) example
// Example: The SDK.UISamples.getFirstControl 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.UISamples.getFirstControl = function () {
 var firstControlByPosition = Xrm.Page.ui.controls.get(0);
 var firstControlByName = Xrm.Page.ui.controls.get(firstControlByPosition.getName());

 if (firstControlByName == firstControlByPosition) {
  alert("The first control on the form is '" + firstControlByPosition.getLabel() + "'.");
 }
 else {
  alert("An error has occurred:\n\nUnable to determine the label of the first control on the form.");
 }

};

 // Xrm.Page.ui.controls.getLength() example
// Example: The SDK.UISamples.getControlCount function alerts the user of how many controls are on the current form.
SDK.UISamples.getControlCount = function () {
 var controlsLength = Xrm.Page.ui.controls.getLength();

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


 /*
 This generic function can be used to bind the visibility of any section to the
 value of a boolean attribute. It requires the following:
 1. It must only be added to the OnChange event of a Boolean attribute
 2. When configuring the event handler, you must pass the execution context as the first parameter.
 3. When configuring the event handler, you must specify the name of the section in the 
 comma separated list of parameters that will be passed to the function.
 For example, if you want to hide the section with the name "marketing information", you must enter
 the following information in the field: "marketing information"  (using quotation marks).

 Note: The section name value can be edited in the application. 
 If this value is changed after event handler has been configured the function will not work.
 */

SDK.UISamples.bindSectionVisibilityToBooleanAttribute = function (eContext, sectionName) {
 Xrm.Page.ui.tabs.forEach(function (tab, index) {
  tab.sections.forEach(function (section, index) {
   if (section.getName() == sectionName)
   { section.setVisible(eContext.getEventSource().getValue()) }
  });
 });
};

 //End of UI Samples Functions

See Also

Reference

Xrm.Page.ui

Concepts

Write Code for Microsoft Dynamics CRM Forms
Use JavaScript with Microsoft Dynamics CRM

Other Resources

Xrm.Page Sample Libraries

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.