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

Demonstrates

This library contains examples of the following Xrm.Page.ui.formSelector item 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.FormItemSamples = {};

    // formitem.getId() example
    // formitem.getLabel() example
// Example: The SDK.FormItemSamples.getFormInformation function displays the current form item's id and label to the user
SDK.FormItemSamples.getFormInformation = function () {
 var item = Xrm.Page.ui.formSelector.getCurrentItem();
 if (item != null) {
  var itemId = item.getId();
  var itemLabel = item.getLabel();

  alert("The current form item has the following properties:\n\n" +
        "  \u2219 Label: '" + itemLabel + "'\n  \u2219 Id: '" + itemId + "'");
 }
 else {
  alert("The getFormInformation function only applies to forms with multiple form items.");
 }
};

    // formitem.navigate() example
// Example: The SDK.FormItemSamples.showFormItems function displays a page in a new window showing the form items with a button to navigate to the form item.
SDK.FormItemSamples.showFormItems = function () {
 var items = Xrm.Page.ui.formSelector.items.get();
 if (items.length > 1) {
  var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Show Form Items</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 += "<script type=\"text/javascript\" >";
  html += "function navigate(itemId) { ";
  html += "window.opener.Xrm.Page.ui.formSelector.items.get(itemId).navigate(); ";
  html += "window.close();";
  html += "}";
  html += "</script></head><body>";
  html += SDK.FormItemSamples.getFormItems();
  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();
 }
 else {
  alert("There is only one form item currently available.");
 }
};

SDK.FormItemSamples.getFormItems = function () {
 var html = "<table summary='This table displays a row for each form item containing a button to navigate to that form.'><thead><tr><th scope='col'>Form Item Label</th><th></th>" +
    "</tr></thead><tbody>";
 var items = Xrm.Page.ui.formSelector.items.get();
 var currentFormId = Xrm.Page.ui.formSelector.getCurrentItem().getId();
 for (var i in items) {
  var item = items[i];
  var itemId = item.getId();
  if (itemId != currentFormId) {
   var itemLabel = item.getLabel();
   html += "<tr><td>" + itemLabel +
            "</td><td><input type=\"button\" onclick=\"navigate('" + itemId +
            "');\" value='Navigate to " + itemLabel + "' /></td></tr>";
  }

 }

 html += "</tbody></table>";
 return html;
};
    //End of FormItem Samples Functions

See Also

Reference

Xrm.Page.ui.formSelector item methods

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.