Charteris Community Server

Welcome to the Charteris plc Community
Welcome to Charteris Community Server Sign in | Join | Help
in Search

Alistair Laing's Blog

Interacting with parent controls in Microsoft Dynamics CRM 4.0.

If you do a search in Google on the title above, you'll find plenty of code samples of the JavaScript to get and set the values of controls on CRM 4.0 forms and even how to do it from an IFRAME in a page. As you may know if you have any experience of extending MS CRM 4.0, the IFRAME is a big part of offering extra functionality to users.

Of course, as you may know if you have a bit of a history in Web UI design, there are a few hurdles to be able to be allowed to run JavaScript if your IFRAME contains a page from a site that is different to the HTML page hosting the IFRAME. This so-called cross site scripting was a big problem in earlier browsers and security blocks added in to stop this being hijacked for nefarious purposes. To deal with this the form designer in CRM 4.0 has the option to switch this security on or off. Unfortunately this has its limitations and I found that the option did not have any effect on the script I was trying to run from the page in the IFRAME. In the end I had to create a virtual directory in IIS in the same website as CRM. The other part I had to watch for is that I used the URL from the root of the site, not the redirected path that the CRM application uses.

Once over that hurdle, I had a bit of a wrestle with JavaScript and dragging it out of my brain at the same time! To explain what I was trying to do, I had two controls on the parent form to populate. No problem, I had the names of the attributes and CRM 4.0 has a nice way of naming fields and controls from the attribute names. Unfortunately I got an access denied error thrown by the script engine in the browser. By a process of trial and error I established that one control worked and the other did not. Not because of value types or anything, it was because one was a lookup and the other was a straight text field. Lookups need a different approach to accessing them, as they use an array as part of the matching to list setup.

Having established this, I dived in to the code in the Microsoft Dynamics CRM 4.0 SDK which is something like thus :

//Create an array to set as the DataValue for the lookup control.
var lookupData = new Array();
//Create an Object add to the array.
   var lookupItem= new Object();
//Set the id, typename, and name properties to the object.
   lookupItem.id = '{1AAC1363-01A1-DB11-8432-0003FF9CE217}';
   lookupItem.typename = 'account';
   lookupItem.name = 'A Bike Store';
// Add the object to the array.
   lookupData[0] = lookupItem;
// Set the value of the lookup field to the value of the array.
   crmForm.all.parentaccountid.DataValue = lookupData;

Fine, but what guid id is that and how was I going to find it out. Further searching established that it is the guid of the entity that is listed by the lookup. Finding this out was a bit of a hunt again. Firstly I found the name of the entity being looked up by checking the properties of the lookup on the parent form. This pointed me to the source entity. Then I navigated through settings to call up the entity. Doing this lined up a URL with the GUID on it - perfect. The typename was also shown on one of these property pages. I filled them all in, and it didn't work. I did some cursing of Javascript and started hunting again.

I finally ended up wandering the directories of the SDK again. If you browse down the folders client\howto\usinglookups there are two script files readvaluelookupcontrol.js and setvaluelookupcontrol.js. I opened up the latter and it has the following code:

// Create a lookupItem to store the values that you want to set to a target lookup control.
var lookupItem = new Array();

// Specify the values on the signature of LookupControlItem. These values are the GUID of pricelevel, the type code of pricelevel, and the name of the lookup value.
lookupItem[0] = new LookupControlItem ("{F31BB38A-0EC0-403F-99A6-3AF469D7D76E"}, 1022, "Retail");

// Set the form control value to the lookupItem that you just created.
crmForm.all.pricelevelid.DataValue = lookupItem ;

Ok fine, but the realisation dawned slowly that the second line of code was calling a function I hadn't seen so far. I was correct, after doing some more Google work to establish this. Further hunting revealed that this is a function used by the CRM application itself. If you go hunting in Inetpub, you will find the file that defines the function. Take a look at the file Lookup.js which you will find under the web root in _static\_controls\lookup . In there is the definition of LookupControlItem thus:

function LookupControlItem(sId, iType, sName, sOnclick, sDisplayClass, sData, sTypeName, iCategory, sAmbiguousRecordsXml)
{
this.id = sId;
this.type = iType;
this.name = sName;
this.onclick = sOnclick;
this.displayClass = sDisplayClass;
this.data = sData;
this.typename = sTypeName;
this.category = iCategory;
this.ambiguousRecordsXml = sAmbiguousRecordsXml;
}

Include the code by reference or placement, and call it in the style above and it will work.

Its up to you whether you wrap your parent call in a test (helpful for form load timing issues) but I found this worked in the end.

So the SDK is ok, but needs a bit of background knowledge to work with. Another really helpful file that comes with the SDK is a pdf of the Microsoft Dynamics CRM 4.0 UI Style Guide. Essential to match your UI to the rest of the CRM look and feel to make your IFRAME disappear - if you can work CSS and Style of course!

Comments

 

alistairl said:

Going through my own walkthrough testing of the above I discovered a real problemw with this approach. The guid required actually relates to the individual record, not the object, so back to the drawing board.

Doing more searching helped me to find an alternative approach based around triggering the autoresolve functionality in CRM 4.0. I will post this proper approach soon.

September 22, 2008 8:07 PM
 

Alistair Laing's Blog said:

If you read my previous post on interacting with parent controls from an IFRAME ,  you will understand

October 2, 2008 3:26 PM
 

Alistair Laing's Blog said:

If you have used or seen a demonstration of MS CRM 4.0, you will have seen the helpful lookup field

October 2, 2008 4:32 PM
 

Bappa said:

How will I get the button id of

Product Category > Product category mappings > New Product Category button .... I need to make visible false

October 19, 2008 4:30 PM
 

Chuck Condron said:

I am trying to do, what i believe is the opposite.  In our CRM form, one of the fields is Webcam URL, which is an NvarChar text field.

like this:

Webcam URL: http://10.24.4.50:8088

I would like to take the contents of that webcam url field and use that as the Iframe URL

so if I created a tab called Webcam View and on that tab was an Iframe, the Iframe would display the webpage located at http://10.24.4.50:8088 (or whatever that form's webcam URL field contained.

Is this possible?

would you be able to email me at chuckcondron@clearchannel.com?

thank you so much!

February 28, 2009 1:16 AM
 

alistairl said:

Hi Chuck,

If I understand what you want to do correctly, the way to do this is documented in the CRM 4.0 SDK.

Take a look at msdn.microsoft.com/.../cc150846.aspx which shows how to change the src attribute of the IFRAME. Typically this would go in the OnLoad of the form so it is setup for the user to view.

This is also known as "Creating a Dynamic IFrame" and you will see a number of examples related to this on the web.

Cheers

Alistair.

March 3, 2009 12:05 PM
 

Chuck Condron said:

yes I actually did it with form scripting and it was very easy, just one line:

crmForm.all.IFRAME_Webcam.src = crmForm.all.new_webcam.DataValue;

where new_webwebacm is my custom entity.  I thought it was going to be a lot harder..

March 3, 2009 3:08 PM
 

Tahir said:

Can you tell me how to Add Lookup to a normal aspx page that resides in the ISV folder?

August 2, 2009 1:53 PM
 

Rich Chapler said:

Excellent write-up... used it find the accountid for a Case OnLoad default field setting.  Thank you.

February 26, 2010 2:36 PM
 

Christopher Berry said:

I have a bunch of custom aspx pages in iFrames.  The interaction between CRM and these iFrames was inhibiting me from scripting lookup controls.  Very excellent find.  I really appreciate it!

April 7, 2010 2:44 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

About alistairl

After starting my career as an accountant, I veered off and commenced a career in software development. Over the years I have concentrated on Microsoft technologies, and participated in the associated certification programme, gaining MCSD, MCDBA and MCPS in relevant subjects. I have been part of and led teams in some of the major Internet developments using Microsoft Technologies in Scotland, and now combine development and business analysis work for Charteris. I have broad experience but recent focus around SharePoint and Dynamics CRM 4.0 integration.
Powered by Community Server (Commercial Edition), by Telligent Systems