Xrm.Page.data.entity.attributes Collection

[Applies to: Microsoft Dynamics CRM 2011]

Xrm.Page.data.entity.attributes is a collection of all the entity attributes present in the form. This collection provides methods to retrieve or perform actions on each of the attributes in the page.

Examples shown are in the Sample: SDK.AttributesCollectionSamples.js library.

Methods

The following table lists the methods of Xrm.Page.data.entity.attributes. These are the standard set of methods available for all collections in Xrm.Page.

Method Description

forEach

Applies the action contained within a delegate function.

get

Returns one or more attributes depending on the arguments passed.

getLength

Returns the number of items in the collection.

forEach

Applies the action contained within a delegate function.

Xrm.Page.data.entity.attributes.forEach(delegate function(attribute, index))
  • Arguments
    Delegate function with parameters for attribute and index.

Examples:

The SDK.AttributesCollectionSamples.listrequiredfields function uses an anonymous function defined within the argument to display a message showing the required fields in the form.

SDK.AttributesCollectionSamples.listRequiredFields = function () {
 var message = "The following fields are required:\n";

 Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
  if (attribute.getRequiredLevel() == "required") {
   message += "  \u2219 " + attribute.getName() + "\n";
  }
 });

 alert(message);
};

The SDK.AttributesCollectionSamples.listrequiredfields_example2 function passes a reference to a function defined outside the argument to display a message showing the required fields in the form.

SDK.AttributesCollectionSamples.listRequiredFields_Example2 = function () {
 window.message = "The following fields are required:\n";
 Xrm.Page.data.entity.attributes.forEach(SDK.AttributesCollectionSamples.addRequiredFieldToMessage);
 alert(window.message);
 window.message = null;
};

SDK.AttributesCollectionSamples.addRequiredFieldToMessage = function (attribute, index) {
 if (attribute.getRequiredLevel() == "required") {
  window.message += "  \u2219 " + attribute.getName() + "\n";
 }
};

The SDK.AttributesCollectionSamples.showfields function opens a new window to display information about each attribute. It uses the forEach method to iterate through each attribute.

SDK.AttributesCollectionSamples.showFields = function () {
 var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>List Fields</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.AttributesCollectionSamples.listFields();
 html += "</body></html>";

 var myWindow = window.open("", "_blank", "height=400,width=450,scrollbars=1,resizable=1", false);
 myWindow.document.open();
 myWindow.document.write(html);
 myWindow.document.close();
};

SDK.AttributesCollectionSamples.listFields = function () {
 var html = "<table summary='This table displays information about attributes on the page. '><thead><tr><th scope='col'>Index</th><th scope='col'>Name</th><th scope='col'>Type</th><th scope='col'>Format</th></tr></thead><tbody>";
 Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
  html += "<tr><td>" + index +
         "</td><td>" + attribute.getName() +
         "</td><td>" + attribute.getAttributeType() +
         "</td><td>" + attribute.getFormat() +
         "</td></tr>";
 });

 html += "</tbody></table>";
 return html;
};

get

Returns one or more attributes depending on the arguments passed.

Xrm.Page.data.entity.attributes.get([String][Number][delegate function(attribute, index)])

Tip

You can use the Xrm.Page.getAttribute() shortcut to access this method directly.

  • Arguments

    • None

      • Return Value All the attributes.
        Type: Array
    • String

      • Return Value The attribute where the name matches the argument.
        Type: Object
    • Number

      • Return Value The attribute where the index matches the number
        Type: Object
    • delegate function(attribute, index)

      • Return Value Any attributes that cause the delegate function to return true.
        Type: Array

Examples

The SDK.AttributesCollectionSamples.showFirstAndLastAttributeInfo function will show the user the name of the first and last attribute on the form, along with the total number of attributes on the form.

SDK.AttributesCollectionSamples.showFirstAndLastAttributeInfo = function () {
 var allAttributes = Xrm.Page.data.entity.attributes.get();
 var attributesLength = Xrm.Page.data.entity.attributes.getLength();

 var firstAttributeByName = Xrm.Page.data.entity.attributes.get(allAttributes[0].getName());
 var lastAttributeByIndex = Xrm.Page.data.entity.attributes.get(attributesLength - 1);

 var message = "The first attribute on the form is '" + firstAttributeByName.getName() + "'.\n";
 message += "The last attribute on the form is '" + lastAttributeByIndex.getName() + "'.\n";
 message += "There are a total of " + attributesLength + " attributes on the form.";

 alert(message);
};

The SDK.AttributesCollectionSamples.showOptionSets function opens a new window to display information about OptionSet attributes. It uses the get method with a delegate function called isOptionSet to filter the attributes so that only attributes of type optionset are returned.

SDK.AttributesCollectionSamples.showOptionSets = function () {
 var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>List OptionSets</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.AttributesCollectionSamples.getOptionSetFields();
 html += "</body></html>";
 var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false);
 myWindow.document.open();
 myWindow.document.write(html);
 myWindow.document.close();
};

SDK.AttributesCollectionSamples.getOptionSetFields = function () {
 var html = "<table summary='This table displays the name of optionset attributes on the page.'><thead><tr><th scope='col'>Name</th></tr></thead><tbody>";
 var optionSets = Xrm.Page.data.entity.attributes.get(SDK.AttributesCollectionSamples.isOptionSet)
 for (var i in optionSets) {
  html += "<tr><td>" + optionSets[i].getName() + "</td></tr>";
 }
 html += "</tbody></table>";
 return html;
};

SDK.AttributesCollectionSamples.isOptionSet = function (attribute, index) {
 return attribute.getAttributeType() == "optionset";
};

getLength

Returns the number of items in the collection.

Xrm.Page.data.entity.attributes.getLength()
  • Return Value
    Type: Number

See Also

Tasks

Sample: SDK.AttributesCollectionSamples.js

Reference

Xrm.Page Reference
Xrm.Page.data.entity
Xrm.Page.data.entity Attribute Methods

Concepts

Form Scripting Quick Reference
Write Code for Microsoft Dynamics CRM Forms
Use the Xrm.Page Object Model

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