Xrm.Page Object Model

In addition to triggering code to run after specific events occur, Microsoft Dynamics CRM provides an object model of form elements that can be utilized to manipulate data, change form object properties, or analyze the current state of the form or data within the form. This object model is called Xrm.Page and in addition to the event handlers are the primary tool used to develop custom business logic at the client-side.

Some benefits of using the Xrm.Page model are:

  1. Change the appearance or manipulate form objects like data controls (text boxes, checkboxes, etc.), tabs, sections, etc.
  2. Identify elements to show/hide.
  3. Specify names of objects from which to retrieve/store data values.
  4. Provide namespaces to properties so the system allows multiple controls per field or multiple forms per entity.

The Xrm.Page model provides a namespace container for following objects:

Xrm.Page object model

 

Context

Context provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string by event handler registered on Form.

The context object is used in forms by using the Xrm.Page.context syntax. There are several functions that are accessible through the context object, such as getUserId. The following code will return the current user’s UserId.

Xrm.Page.context.getUserId();

Data

Data provides access to the data on Entity Form. Values can get or set fields on CRM form after data validation or data manipulation.

The following code will return the value of the email address field:

Xrm.Page.data.entity.attributes.get(“emailaddress1”);

Or,  a shortcut for this code is:

Xrm.Page.getAttribute(“emailaddress1”);

The following code will set the value of the email address field:

Xrm.Page.getAttribute(“emailaddress1”).setValue(“joecrm@powerobjects.com”);

UI

UI provides methods to retrieve information about the user interface, collections for several sub components of the form. It provides access to each control present on the form.

The following code will close the current form:

Xrm.Page.ui.close();

This code will get the value of the associated label of the email address field.

Xrm.Page.ui.controls.get(“emailaddress1”).getLabel();

The CRM Xrm.Page Object Model is very broad. Further documentation regarding its capabilities can be found in the Microsoft Dynamics CRM SDK or using the following resources:

Using Javascript with Microsoft Dynamics CRM: http://msdn.microsoft.com/en-us/library/hh771584.aspx