Accessing the Parent Form Xrm.Page in CRM 2011

For those of you trying to access a parent form’s Xrm.Page object in CRM 2011 you can use the following syntax.

From an IFrame HTML page

document.parentWindow.parent.Xrm.Page

From a new window/dialog

window.top.opener.parent.Xrm.Page

An example of a new window being used is when you are viewing the Account Form and then choose to create a new Contact from the left hand navigation pane and then within the Contact Form you need to access the Account Form (parent).

Potential CRM 4 syntax to be replaced is as follows

window.opener.parent.document.crmForm
window.opener.document.crmForm
window.opener.parent.crmForm

If you have downloaded the CRM 4 to CRM 2011 JavaScript Converter Tool before December 2010 then you can add the following code snippets to the TOP of the ConversionList.xml file straight after the <snippets> node, alternatively just download the tool again.


    <snippet>
      <crm4>window.opener.parent.document.|_|;</crm4>
      <crm5>window.top.opener.parent.|_|;</crm5>
    </snippet>
    <snippet>
      <crm4>window.opener.document.|_|;</crm4>
      <crm5>window.top.opener.parent.|_|;</crm5>
    </snippet>
    <snippet>
      <crm4>window.opener.parent.|_|;</crm4>
      <crm5>window.top.opener.parent.|_|;</crm5>
    </snippet>

Both Mitch Milam MVP and I searched high and low for this solution as the Xrm.Page object is not in the DOM at that same location as the CRM 4 crmForm object. Please let us know if you experience any problems with accessing parent forms and we will try and help you.

Also if you liked my CRM 4 to CRM 2011 JavaScript Converter Tool then you might like to take a look at Mitch Milam’s CRM Migration Assistant also for converting scripts.

From IE Developer Tools

Also, Paul Way has shown that you can access the Xrm.Page object from the Developer Tools console window using the following code.

frames[0].Xrm.Page

About these ads

About Rhett Clinton MVP
Dynamics CRM MVP

21 Responses to Accessing the Parent Form Xrm.Page in CRM 2011

  1. Pingback: Did you know, Dynamics CRM & xRM #8 « North 52

  2. Guru says:

    Its nice to the change in CRM 2011. I have similar requirement for setting contact address from its more addresses option. I need to set the one of the more address from more addresses to contact default address. In Addresses entity window.top.opener.parent does not work. Please let me know is this achievable this way or I have to make oData call to set the contact address.

  3. Demeilliez Frederic says:

    Hi,

    I’m having a problem using this in a form I open with the “Window.open(…)”-function. I’m getting an “Undefined” error… So when i’m trying to acces the Parent’s Entity Name, I’m getting the following error :

    Error – MEssage :
    There was an error with this field’s customized event.
    Field:ave_operationsofficerid
    Event:onchange
    Error:’window.top.opener.parent.Xrm.Page.data.entity’ is empty or not an object.

    My “JS”-Code :
    //if ((window.top != null) && (window.top.opener != null) && (window.top.opener.parent != null) && (window.top.opener.parent.Xrm != null) && (window.top.opener.parent.Xrm.Page != null) && (window.top.opener.parent.Xrm.Page.data != null) && (window.top.opener.parent.Xrm.Page.data.entity != null)) {
    if (window.top.opener.parent.Xrm.Page.data.entity.getEntityName() == “contact”) {
    ….
    }
    //}

    The above code works like a charm when opened in a “Form” opened by clicking on the “New” button of an entity in Contact. When I open the same “URL” or a random “URL” with “JavaScript”, using the “Window.open(…)”-function, it doesn’t work anymore. Is there something I need to do to make it work with the “Window.open(…)”. This is crucial when using a “Custom” webpage, where we need to refresh the parent or some fields of the parent.

    Kind Regards,
    Frederic

    • I think the problem is going to be with the initial objects of your code, somewhere in window.top.opener.parent. Your best bet is to use the IE Dev Tool and debug the javascript up to this line and then view each object to see what is available, e.g. window , window.top , window.parent , etc.. It may need to be window.opener or similar.

  4. Demeilliez Frederic says:

    Hi Rhett,

    Thx for the answer, I already tried to search it up with the “IE Dev Tool”. But I don’t immediatly find the right path to the “Parent”. Is there a way to search in the “IE Dev Tool” after “XRM”?

    It’s only when using the “Window.open(…)” that it doesn’t work. I use the exact same URL as used when hitting the “New”-button on a Form.

    Kind Regards,
    Frederic

  5. Demeilliez Frederic says:

    window.parent.opener.Xrm.

    Hi Rhett,

    While using the “IE Dev Tool” I found the correct path to the “XRM”-object. I’m sure you’re be more than be pleased if I tell you the correct path when using the “window.open(…)”-function. I use the following to retrieve the parents “Logical Entity Name” :
    alert(window.parent.opener.Xrm.Page.data.entity.getEntityName());

    I implement it as following :

    if ((window.top != null) && (window.top.opener != null) && (window.top.opener.parent != null) && (window.top.opener.parent.Xrm != null) && (window.top.opener.parent.Xrm.Page != null) && (window.top.opener.parent.Xrm.Page.data != null) && (window.top.opener.parent.Xrm.Page.data.entity != null)) {
    if (window.top.opener.parent.Xrm.Page.data.entity.getEntityName() == “contact”) {
    ….
    }
    }
    else if((window.parent != null) && (window.parent.opener != null) && (window.parent.opener.Xrm != null))
    {
    if (window.parent.opener.Xrm.Page.data.entity.getEntityName() == “contact”) {
    ….
    }
    }

    I hope that this will help you and others for futher development!

    Kind Regards,
    Frederic

    • Balhinio says:

      The window.top.opener.parent.Xrm.Page work well into the forms of an entitie, but when I try to use it in a CRM aspx page like (_grid\cmds\dlg_listphone.aspx) to get the parent attribute value, it doesnt work because the is no opener.

    • Terri says:

      This was helpful. Still don’t know how to get to the value of a field on the parent from the child. window.parent.opener.Xrm.Page.getAttribute(“fieldname”).getValue() does not work. Any suggestions are greatly appreciated.

  6. I posted a similar blog back in July dealing with the Xrm object: http://blog.customereffective.com/blog/2011/07/javascript-execution-context-xrm.html

    Instead of document.parentWindow.parent.Xrm.Page you can call window.parent.Xrm.Page, but it’s all the same in the end. Also, for anyone who uses IE Developer Tools: frames[0].Xrm.Page is the easiest way to go.

  7. In addition to Rhett’s snippets, here is one I’ve converted. I found this code really useful in v4.0 and have just upgrade the same customer to 2011 so needed to solve this issue.

    Code used to determine what relationship link was selected in the parent window. Code used in the onload event handler of the related entity. Useful when the same child entity is linked via separate relationships to the same parent entity.

    – v4 –
    var calling_window = window.opener.location.href;
    var sIndex = calling_window.search(‘tabSet=’);
    var sSubString = calling_window.substring(sIndex+7);
    if (sSubString == ‘new_contact_new_**********’)

    – 2011 –
    var calling_window = window.top.opener.location.href;
    var sIndex = calling_window.search(‘tabSet=’);
    var sSubString = calling_window.substring(sIndex+7);
    if (sSubString == ‘new_contact_new_**********’)

    Happy coding…
    Simon

  8. luka says:

    Hi,
    I am trying to change the value of a parent window field. I tried to access it via XRM.Page, but CRM doesn’t allow accessing the fields on parent form using this approach.
    Is the old “4.0” way using …crmForm… the only way to change the field in the parent form? It doesn’t look good to use old approach as there is no confirmation that they won’t take it out in the next version.

    I found on one blog, I qoute: ” i would suggest you acces sit by passing the attribute value into the parent form via Query String in extraqs paramter”, but I don’t know how this helps me update the parent form filed?

    Any ideas?
    Thank you in advance and kind regards,
    Luka

    • Hi Luka, to meet your scenario it seems you need to use window.top.opener.parent.Xrm.Page to access your parent window Xrm.Page object.

      I hope this helps,
      Thanks,
      Rhett

      • Sebastian Iturrioz says:

        Hi, I am trying to do exactly the same but the window.top.opener.parent.Xrm.Page doesn´t have the getAttribute method.
        I need that method to use the setValue!!

        Thanks,
        Sebastian.

      • Hi Sabastian,

        Where are you using this code as the namespacing is slighly different based on where you are trying to access the Xrm.Page object.

        You can use the IE Dev Toolbar to debug the script and navigate throught the DOM to find the exact location. The correct Xrm.Page object will have valid instantiated values in its child objects instead of null, like Xrm.Page.ui , etc..

        Hope this helps.

        Cheers,
        Rhett

      • Sebastian says:

        Hi Rhet, thanks for you reply!!
        Because I don´t have the getAttribute I have tried to use this two but it still not working.

        var control = window.parent.opener.Xrm.Page.data.entity.attributes.get(‘fieldSchemaName’);
        control._attribute.set_value(lookupValue);

        To see if those methods are the problems I have tried to set a lookup value with these two methods from the same entity and it is working, so seems to be that the problem is in the window.parent.opener…

        Thanks!
        Sebastian.

      • Have you tried the following, I’m not sure if you are in a dialog or an iframe.

        From an IFrame HTML page
        document.parentWindow.parent.Xrm.Page

        From a new window/dialog
        window.top.opener.parent.Xrm.Page

        Regards,
        Rhett

  9. Garcia says:

    Hello Nice post,

    I got a similar problem: I want to update fields in the Case Form when i click the case resolution button. When we do this the case resolution windows popup and we enter the name and the resolution description.

    How can i do that when i click the accept button all the data i entered be copied to a field in the case form.

    how can i achieve this.

  10. MVM says:

    Hi All,

    Please let me know how to capture date value in a parent form and display it on Child form. Both Parent and Child are Phone Call forms.
    I have used window.top.opener.document.getElementById(“fieldname”).value and window.top.opener.parent.Xrm.Page.getAttribute(“fieldname”) but nothing worked.

    Thanks
    MVM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: