Understanding Client API object model in Dynamics 365.

Hello Guys,

In our previous blog we have seen Business Rule. Today we are going to explore Client API Model.



Do follow us to know more about Dynamics and Power Platform. Also if you like our blog then please comment and share this blog with your friends.

The client API object model for model driven apps provides you objects and methods that you can use to apply custom business logic in model driven app using JavaScript. These includes:
  • Gets or sets attribute value.
  • Show and hide user interface elements.
  • Reference multiple controls per attribute.
  • Access multiple forms per entity.
  • Manipulate form navigation items.
  • Interact with the business process flow control.
At the root of the Client API object model are the following context and Xrm object:

ExecutionContext :-  It defines the event context in which your code executes. The execution context is passed when an event occurs on a form or grid, which you can use it in your event handler to perform various tasks such as determine Form Context or Grid Context or manage the save event.

The execution context is an optional parameter that can be passed to a JavaScript library function through an event handler.

Use the pass execution context as first parameter option in the handler properties dialog. Specify the name of the function to pass the event execution context. The execution context is the first parameter passed to as function.

FormContext :-  The client API form context provides a reference to the form or to an item on the form, such as a quick view control or a row in a editable grid, against which the current code is executed.

GridContext :- Grids present data in a tabular format. Grids can span the entire form or can be one of the items on a form; the latter are called subgrids.
The client API grid context object provides reference to a subgrid on a form against which the current code is executed.

Xrm :- The Xrm object is globally available to use in your code without to use the execution context in Client API. Below URL specifies the list of the namespaces and method which can be used with Xrm.


To work with above context we need to understand Web Resources first. Web resources in CRM are the virtual web files that stored in CRM database and used to implement web page functionalities in CRM. These files can be of HTML, JScript, Silverlight, or any other supported types.

CRM being a product, comes with an extensive set of features and functionalities. However, most of the times, you would have to extend these functionalities to meet your custom requirements. extending these functionalities generally happens in two ways.
  • Extending on client side :- Using web resources and form ways.
  • Extending on server side :- Plugins, Flows and Web Services.
Consider a scenario when you have a CRM form containing certain fields. CRM provides OOB features for basic validations such as mandatory fields, field length etc. However what if you would like to have a more complex validation such as validating if the entered phone number is in the correct format, or validating if the entered address actually exists, or if the entered SSN is correct or not?

CRM provides OOB UI customization such as creating sections and tabs on a form, rearranging columns etc. However, what if the client wants to build a custom page which shows all the information of the customer coming from their ERP system?

CRM comes with a standard ribbon bar, which contains all the buttons and options. What if you want to add a ribbon button of your own?

You can always call any external web services in your server side plugin code. However, what if you would like to call any external web services while you are still on client side?

For all above scenarios, we can use web resources. Below are the types of web resource you can create in D365.

  • Webpage(HTML) :- You may want to create any custom HTML page such as maps, custom application page, etc. and place it inside your CRM form.
  • Stylesheet(CSS) :- Any CSS files that will be used together with the HTML files.
  • Script(JScript) :- Any client side logic such as validations, manipulating column values, etc.
  • Data(XML) :- Can be used to store any static settings or configuration data.
  • Image(PNG, JPG, GIF, ICO, SVG) :- Any images used in the application on ribbon buttons, icons, forms etc.
  • Silverlight(XAP) :- Any custom Silverlight application embedded within CRM.
  • Stylesheet(XSL) :- Used to transform XML data.
  • String(RESX) :- Strings to create resource which can be used later on.

To work with web resource we need to understand Form Event Programming which is used to handle client side behaviors such as what happens when a user opens a form, changes some data, move through tabs etc.

To achieve such client-side interactions you will be writing JS code and adding it as a JScript web resource in CRM. However, the JS code which you will write has to use Dynamics CRM's Xrm.Page model and not the standard JavaScript DOM.

However Xrm.Page is depricated and now you can use FormContext of Dynamics CRM. Below are the list of supported events in form programming
  • OnLoad
  • OnSave
  • OnChange
  • TabStateChange
  • OnReadyStateComplete
  • PreSearch
  • Business Process Flow control events.
Now we will be implementing a JScript web resource for more clear picture about web resources. We have one Employee table and we have some requirement to be implemented as shown below:




So now we will be creating one JScript web resource.

Navigate to https://make.powerapps.com and select your environment. Select your solution.




Now click on New -> Other -> Web Resource. This will open a new window as shown below. Configure it as shown below:




Now click on Text Editor and paste below code in the text editor.




Save and Publish. Now click on Form and open Main Form.





Now select Events and click on Add library.




Search your web resource and click on Add. Once library is added you will be seeing On Save and On Load Event. expand On Load and click on Event Handler.
Configure it as shown below:




Now select Contact Type control and click on Events. Click on Event Handler and configure it as shown below:




Save and publish. Refresh the Employee Form.




As you can see, columns became mandatory now and phone and email are visible based on contact type.

Hope it helps...



















Comments

Popular posts from this blog

Read Only Sub Grid & Editable Sub grid in Dynamics 365

Understanding Sales Process in D365.

Understanding Business Unit in Dynamics 365.