If you read my previous post on interacting with parent controls from an IFRAME, you will understand a few of the challenges I faced in trying to pass information from an IFRAME control on a MS CRM form back to fields on that form.
In summary, as you are working with a browser application you need to have the correct conditions in which to pass information from what is one web site to another. To recap:
- You need to switch off cross-site security for the IFRAME in the parent form.
- You need to place your web pages in an IIS virtual directory underneath the MS CRM web root.
The second point is fine, but I recently ran in to an issue with a development server. I was carrying out my small piece of development work remotely, which is a common model we use. I was dealing with the server by VPN and remote access. This is obviously not the normal mode of work, which would be for users to access the server over the network.
Using the server by remote desktop meant a URL of localhost for me. This was fine, but when testing we found that we were getting a JScript "Access Denied" error on the line of code that was trying to set the value of one of the fields on the parent form. This was traced to a difference between the IFRAME URL and that in CRM. When the server domain is detected as being different by Internet Explorer, the protection against cross domain scripting is triggered and the JScript blocked from setting a value.
The Microsoft Dynamics CRM 4.0 SDK has a section on "Creating a Dynamic IFRAME" the idea being that you can change the source of an IFRAME at the behest of your own code. This may be to set a particular parameter at runtime that is not normally available through the IFRAME properties, or for another business process related need.
I wanted to set the source of the IFRAME to look at localhost when I was using the server from my remote development perspective, but to allow testing users to use the form page when they were accessing the server properly over their network. To begin with I considered using the global variable in CRM SERVER_URL but this returns the proper server name of the server, irrespective of whether you are coming from on the server or from another machine. Instead I used the hostname property of the Location object, which itself is part of the window object. This can be used in the form OnLoad event to check how the request has come through and sort the IFRAME appropriately thus:
if (location.hostname=='localhost')
{
crmForm.all.IFRAME_control.src = 'http://localhost/subdir/child_page.aspx';
}
That way the default is set to be "correct" for the common usage of the form, and adjusted for the peculiarities of development.
My next CRM 4.0 post will be on my voyage with the Lookup Field.