Tag Archives: console

Handy little methods for Xrm.Page calls in browser for Dynamics CRM

Based on the last post about retrieving Microsoft Dynamics CRM record based information from the console ‘Dynamics CRM – Access GUID from the browser console‘, I thought I write up some other useful bits that can be used to get information in a pinch.

Most of these have be run from the viewport where the record in question is open, unless otherwise stated.

Get an entity’s origin

frames[1].Xrm.Page.data.entity.getEntityName();

This is particularly handy when you want to know the logical name to work with programmatically. All this without having to open up the solution or form and plow through a few forms and several clicks.

Get logical name for controls in the controls collection

frames[1].Xrm.Page.getControl(0).getName();

The controls collection is a child of the ui object, which itself is a child of the Xrm.Page object. [See the visual layout on MSDN]. The index allows retrieval of control items as they occur on the form.

Get logical name for control based on display name

frames[1].Xrm.Page.getControl(0).setFocus();

Now there are no direct method calls, that I found or aware of, that could get you the logical name for a field based on its display name. Which is a big shame because that would be a very useful one.

Now this might be somewhat tedious based on what wants to be achieved, and most definitely based on the number of fields on the form, as the user will have to randomly call the setFocus() method on a guess of where the control might be in the index based on position i.e. if the field is placed in the middle of the list of fields, chances are the index will be roughly half of the length of the count of fields.

But once the focus reveals the field that the user is interested in, the previous method getName() can be used to retrieve the logical name using that same index.

Conclusion

These calls are part of the Xrm.Page model which can be referred to on the MSDN site. However, they can be utilized for quick little views at relevant information via the browser’s console.

JavaScript Consoles

The one bane of JavaScript development is that it’s not as easy to run  code as instantly as a compiled language such as C# or Java. Even when using a fully featured IDE, the results are limited to attaching it to a browser port via HTML to see the results. There are some options however. Some require internet access, others once set up, such as Node.js, allow you to execute the code with nothing more than the CLI.

Browser Console

The ever present and common tool that most web development enthusiasts would be accustomed to is the built in console that most modern browsers have. This is usually a part of a comprehensive set of developer tools that most browsers come bundled with. It’s a simple as opening up the console and typing in JavaScript commands. Each browser has it’s own implementation but the base level items should be quite similar to someone familiar with one.

Firefox browser console

Firefox browser console running JavaScript

CodePen

CodePen has been a stomping ground for front end wizardry or as a platform for experimenting and seeking feedback. So with this new JS console, not only can HTML/CSS/JavaScript and various extensions thereof be shared, but also the code output. Read the official update on Codepen.io blog.

Codepen

Codepen.io

JSbin

Another similar code sharing site is JSbin, in fact the JavaScript console  for Codepen is an almost complete port of the one used in JSbin. JavaScript entered in the JavaScript section can be seen either in the output section by virtue of it’s effects or the results directly seen in the console by logging items to it. Find out more about JSbin here.

JSbin

JSbin

Node.js

Enter server side JavaScript. Apart from that Node.js opens up JavaScript on the development machine itself, allowing access to native file system, create utility scripts etcetera. One of the cool thing is the ability to run a Node.js command prompt which allows the running of snippets of code typed directly into the prompt or running script files.

Node.js

Node.js

Brackets

Now this is a interesting concept from Adobe. It’s a lightweight code editor with JSLint baked in and one very invaluable feature is the ability to attach to a running browser process and see updates made to either the HTML/CSS/JavaScript in the browser’s viewport instantly. At the moment the only supported browser is chrome and is expected to expand to others.

Brackets.io text editor

Brackets text editor

 

Dynamics CRM – Access GUID from the browser console

Now this blog hasn’t quite morphed from its primordial form and I have yet to declare its mission statement.

In a nutshell it is to log and explore all aspects of my  journey through learning JavaScript – its many utilities and facets. One such area is its use in Microsoft Dynamics CRM.

So without discussing further details of background of JavaScript use in CRM, I’d like to rehash a quick and easy way to retrieve a form’s GUID (Globally Unique Identifier) through the browser console. Just to point out that this tip comes from other sources [1] [2] but I noticed it wasn’t quite working.

The posts suggests entering the following into the browser console:

frames[0].Xrm.Page.data.entity.getId();

However doing so in Firefox throws an error saying that data node is null.

XrmPagedata

The issue is that the first index of frames i.e. frames[0] is actually referring to the list of records from the current view and not the actual record instance itself.

Typing in frames[0] in the console illustrates this:

XrmPagedataframes

This returns _root branch before the page itself. Clicking on this should display the list of records. The implementation seems to have undergone changes since CRM 2013.

In order to access the record directly and therefore its GUID the correct frame index is 1:

XrmPagedataframes1

Clicking on the link returned will open the record in a new page. So with this index change the call mentioned in the original blog(s) will return the record’s GUID.

XrmPagedataentitygetId

Note: I haven’t tried it on IE or Chrome as IE keeps crashing constantly when the developer tools is opened. and I just haven’t bothered with Chrome.