How to access controls from a console

2020-06-22 08:43发布

问题:

I tried to execute the following command from the console.

var subject = Xrm.Page.ui.controls.get("subject");

That's the exact syntax I'm using in the web resource that I'm plugging in to CRM. However, I only got an error message saying that "unable to get property 'controls' of undefined or null reference".

I do understand the message. What I want to know is two-fold.

  1. What syntax will work from the console (F12) to refer to the stuff on the screen?
  2. Why doesn't it work the way I did? Where doesn ui come from?

I've checked that I can refer to both Xrm and Crm.Page but apparently ui is null (it's listed when I print out the contents of Page but sett to null).

回答1:

In addition to what @Daryl said, I can add that I use different syntax. For some reason, I don't get his to work either. Might have to do with different browser version or something. Instead try to execute this, if you still can't get it to work (although I must admit that his is shorter = better).

Xrm.Page.getAttribute("lastname").getValue();

The lastname parts is tested a minute ago on creation of an instance of entity Contact. I just put in a breakpoint inside a script that is executed onchange and while broken-pointed, I entered the command above to the console.

If neither approach works for you, you've got some weird problem with your CRM or browser.



回答2:

I know this is a kinda old thread, but if you still getting that 'object doesn't support property..' error when executing the command from console, IE F12; try calling it from the frame i.e

frames[0].Xrm.Page.getAttribute("controlId").getValue();

In CRM 2013 it is a little different

frames[1].Xrm.Page


回答3:

It's kind of tough to detect the frames across different browsers, so this little javascript can help you out:

for(var i=0;i<5;i++) //loop through 0 to 4
    if(frames[i].Xrm.Page.ui != undefined) //check if undefined    
    {
        Xrm = frames[i].Xrm;  //assign Xrm
        console.info("~: Xrm updated with frame " + i + " :~"); //show info
        break; //breakout the loop
    }

What it does ?

What it's basically doing is to loop through 0-5 to find frame where Xrm.Page.ui is not undefined, once it gets it it assigns it to the Xrm and breaks the loop.

How to use ?

To use it just copy/paste and run in the browser console once per session then after you can run/test all your Xrm codes form the browser console.



回答4:

This works for me Xrm.Page.getControl("controlId"). It's just a shortcut for what you have already though...cant-disable-set-to-read-only-protect-gray-out-etc-a-field



回答5:

A reason some people need this information is to access their own code. If you need to access your own methods from the console, in 2011, any global methods (or namespaces) in your javascript were also in forms[0]. Obviously, this is a bad idea, just from a naming standpoint. In forms v6+ any global objects or functions are in an object called customScriptsFrame inside frames[0] (or presumably whichever frame the Xrm is found).

frames[0].customScriptsFrame.myFunctionName();