Sample: SDK.NavItemsCollectionSamples.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.NavItemsCollectionSamples.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 listNavItems function must be called using SDK.NavItemsCollectionSamples.listNavItems().

Demonstrates

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

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

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

 Xrm.Page.ui.navigation.items.forEach(function (item, index) {
  var itemLabel = item.getLabel();

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

 alert(message);
};

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

 Xrm.Page.ui.navigation.items.forEach(SDK.NavItemsCollectionSamples.addNavItemToMessage);

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

SDK.NavItemsCollectionSamples.addNavItemToMessage = function (item, index) {
 var itemLabel = item.getLabel();

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

 // Xrm.Page.ui.navigation.items.get() example
// Example: The SDK.NavItemsCollectionSamples.listNavItemsWithTheLetterS function displays a message showing the navigation items that contain the letter 's' in their label.
SDK.NavItemsCollectionSamples.listNavItemsWithTheLetterS = function () {
 var message = "The following navigation items contains the letter 's' in their label:\n";
 var items = Xrm.Page.ui.navigation.items.get();

 for (var i in items) {
  var item = items[i];
  var itemLabel = item.getLabel();
  if (itemLabel.toLowerCase().indexOf('s') != -1) {
   message += "  \u2219 " + itemLabel + "\n";
  }
 }

 alert(message);
};

 // Xrm.Page.ui.navigation.items.get(number) example
// Example: The SDK.NavItemsCollectionSamples.listFirstActivityItem function displays the first navigation item's label.
SDK.NavItemsCollectionSamples.listFirstActivityItem = function () {
 var firstItem = Xrm.Page.ui.navigation.items.get(0);
 if (firstItem != null) {
  alert("The first navigation item is '" + firstItem.getLabel() + "'.");
 }
 else {
  alert("There are not currently any navigation items on the form.");
 }
};

 // Xrm.Page.ui.navigation.items.get(string) example
// Example: The SDK.NavItemsCollectionSamples.doesActivityItemExist function displays a message stating if there is a navigation item with an id of 'navActivities'.
SDK.NavItemsCollectionSamples.doesActivityItemExist = function () {
 var activityItem = Xrm.Page.ui.navigation.items.get("navActivities");
 if (activityItem != null) {
  alert("The '" + activityItem.getLabel() + "' item does exist in this form's navigation area.");
 }
 else {
  alert("The 'Activities' item does not exist in this form's navigation area.");
 }
};

 // Xrm.Page.ui.navigation.items.get(function(item, index)) example
// Example: The SDK.NavItemsCollectionSamples.listEveryOtherNavItem function displays a message listing every other navigation item.
SDK.NavItemsCollectionSamples.listEveryOtherNavItem = function () {
 var message = "The following list is the label of every other navigation item on the form:\n";
 var items = Xrm.Page.ui.navigation.items.get(function (item, index) {
  return index % 2 == 0;
 });

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

 alert(message);
};

 // Xrm.Page.ui.navigation.items.getLength() example
// Example: The SDK.NavItemsCollectionSamples.showNavItemsCount function displays the count of the navigation items on the form.
SDK.NavItemsCollectionSamples.showNavItemsCount = function () {
 var items = Xrm.Page.ui.navigation.items.get();
 var count = Xrm.Page.ui.navigation.items.getLength();

 if (items.length == count) {
  alert("There are " + count + " navigation item(s) on the form.");
 }
 else {
  alert("An error has occurred:\n\nUnable to determine how many navigation items are on the form.");
 }
};
 //End of NavigationItemsCollection Samples Functions

See Also

Reference

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