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

Demonstrates

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

 // Examples:The following two examples perform the same task. 
 // Both examples display a message showing the tabs that contain the letter 'a' in their label.

 // Xrm.Page.ui.tabs.forEach() example 1
 // This example uses an anonymous function defined within the argument.
SDK.TabCollectionSamples.listTabs = function () {
 var message = "The following tabs contains the letter 'a' in their label:\n";

 Xrm.Page.ui.tabs.forEach(function (tab, index) {
  var tabLabel = tab.getLabel();

  if (tabLabel.toLowerCase().indexOf('a') != -1) {
   message += "  \u2219 " + tabLabel + "\n";
  }
 });

 alert(message);
};

 // Xrm.Page.ui.tabs.forEach() example 2
 // This example passes a reference to a function defined outside the argument.
SDK.TabCollectionSamples.listTabs_Example2 = function () {
 window.message = "The following tabs contains the letter 'a' in their label:\n";

 Xrm.Page.ui.tabs.forEach(SDK.TabCollectionSamples.addTabToMessage);

 alert(window.message);
 window.message = null;
};

SDK.TabCollectionSamples.addTabToMessage = function (tab, index) {
 var tabLabel = tab.getLabel();

 if (tabLabel.toLowerCase().indexOf('a') != -1) {
  window.message += "  \u2219 " + tabLabel + "\n";
 }
};

 // Xrm.Page.ui.tabs.get() example
 // Xrm.Page.ui.tabs.get(number) example
// Example: The SDK.TabCollectionSamples.getFirstTab function gets the first tab by using two different overloads of the get() method, 
 // and compares them to ensure they are the same.
SDK.TabCollectionSamples.getFirstTab = function () {
 var allTabs = Xrm.Page.ui.tabs.get();
 var firstTabByIndex = Xrm.Page.ui.tabs.get(0);

 if (allTabs[0] == firstTabByIndex) {
  alert("The first tab has the label '" + firstTabByIndex.getLabel() + "'.");
 }
 else {
  alert("An error has occurred:\n\nUnable to determine the label of the first tab.");
 }
};

 // Xrm.Page.ui.tabs.get(string) example
// Example: The SDK.TabCollectionSamples.doesTabExist function alerts the user if there is a tab on the form with the name of the value passed in.
SDK.TabCollectionSamples.doesTabExist = function (tabName) {
 var tab = Xrm.Page.ui.tabs.get(tabName);
 if (tab != null) {
  alert("The tab with the name '" + tabName + "' does exist on the form.  It has a label of '" + tab.getLabel() + "'.");
 }
 else {
  alert("No tab with the name '" + tabName + "' exists on the form.");
 }
};

 // Xrm.Page.ui.tabs.get(function(tab, index)) example
 // Xrm.Page.ui.tabs.getLength() example
// The SDK.TabCollectionSamples.getFirstAndLastTab function displays the label of the first and last tab on the form.
SDK.TabCollectionSamples.getFirstAndLastTab = function () {
 var tabsLength = Xrm.Page.ui.tabs.getLength();

 var message = "The first and last tab on the form are:\n";
 var firstAndLastTab = Xrm.Page.ui.tabs.get(function (tab, index) {
  return index == 0 || index == tabsLength - 1;
 });

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

 alert(message);
};
 //End of TabCollection Samples Functions

See Also

Reference

Xrm.Page.ui.tabs 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.