Sample: SDK.AttributesCollectionSamples.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.AttributesCollectionSamples.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 listRequiredFields function must be called using SDK.AttributesCollectionSamples.listRequiredFields.

Demonstrates

This library contains examples of the following Xrm.Page.data.entity.attributes Collection 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.AttributesCollectionSamples = {};

 // Examples:The following two examples perform the same task. 
 // Both examples display a message showing the required fields in the form.

 // Xrm.Page.data.entity.attributes.forEach() example
 // This example uses an anonymous function defined within the argument.
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);
};
 // Xrm.Page.data.entity.attributes.forEach() example 2
 // This example passes a reference to a function defined outside the argument.
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";
 }
};
 // Xrm.Page.data.entity.attributes.forEach() example 3
 // This example 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;
};
 // Xrm.Page.data.entity.attributes.get() example
 // Xrm.Page.data.entity.attributes.get(string) example
 // Xrm.Page.data.entity.attributes.get(number) example
 // Xrm.Page.data.entity.attributes.getLength example
// Example: 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);
};
 // Xrm.Page.data.entity.attributes.get(function(attribute, index)) example 4
// Example: 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";
};
 //End of Attributes Samples Functions

See Also

Reference

Xrm.Page.data.entity.attributes Collection

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.