最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access controls from a console - Stack Overflow

programmeradmin6浏览0评论

I tried to execute the following mand 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 e 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).

I tried to execute the following mand 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 e 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).

Share Improve this question edited Jan 8, 2013 at 12:20 asked Jan 5, 2013 at 17:26 user1675891user1675891 5
  • Is your mand code wrapped inside a JS function? Because otherwise Xrm.Page.ui will always be interpreted as null or not an object, as it is in your case. – amartine Commented Jan 6, 2013 at 14:19
  • Can you please explain why that is the case? Xrm is a variable accessible from the console - why does wrapping the call in a function make a difference? Thanks! – Ryan Commented Nov 12, 2014 at 6:45
  • 4 @Ryan I believe he's mistaken or that he misunderstood the question. There's a whole bunch Xrm and Xrm.Page all over the DOM. But the one that's targeting what one sees is accessed by adding frames[0] in front of it (when trying from the console, not from a web resource). Annoyance of the stronger degree... – Konrad Viltersten Commented Nov 13, 2014 at 12:51
  • That is extremely useful! Thanks a lot. I don't understand why the frames[0] is required or why it helps though. – Ryan Commented Nov 14, 2014 at 4:44
  • I'm not entirely certain but it might have to do with the fact that the application is run in one of these frames, although one doesn't see it. I guess you might try to register two different organizations, access them both in the same browser, same window or maybe different browsers or something and check if they might get frames[0], frames[1] etc. When CRM talks to it's own controls, it knows which instance it is and it can't access another organization. But when console window makes the call, the reference bees ambiguous. Maybe. Go and check - get back to us and tell. :) – Konrad Viltersten Commented Nov 14, 2014 at 15:45
Add a ment  | 

5 Answers 5

Reset to default 13

I know this is a kinda old thread, but if you still getting that 'object doesn't support property..' error when executing the mand 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

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.

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

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 mand above to the console.

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

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();

发布评论

评论列表(0)

  1. 暂无评论