Sunday 21 September 2014

Passing Xrm Object from parent page(CRM) to child


Some times we need open a new window from CRM. This new window can be a window a that you open with a simple simple window.open() code. Or it can be a web resource that you want to open dynamically. Most of the time you need the Xrm object on that child page. I've written two small javascript function to pass Xrm object between parent and master page.

function GetXrmElement() {

                //to check if its an iframe
                var IframeCheck = isIframe();
                if (!IframeCheck) {
                    //support for CRM 4.0
                    if (window.opener != null && window.opener.Xrm != null) {
                        Xrm = window.opener.Xrm;
                    }
                        //CRM 2011 and upwords
                    else if (window.top != null && window.top.opener != null
                        && window.top.opener.parent != null && window.top.opener.parent.Xrm != null) {
                        Xrm = window.top.opener.parent.Xrm
                    }
                    else if (this.parent != null && this.parent.Xrm != null)
                        Xrm = this.parent.Xrm;

                    else if (window.top != null) {
                        Xrm = window.top.Xrm;
                    }


                    else {
                        Xrm = null;
                    }
                }

                else {
                    if (this.parent != null && this.parent.Xrm != null)
                        Xrm = this.parent.Xrm;
                    else if (document.parentWindow != null && document.parentWindow.parent != null && document.parentWindow.parent.Xrm != null)
                        Xrm = document.parentWindow.parent.Xrm;
                    else if (window.top != null)
                        Xrm = window.top.Xrm;
                    else
                        Xrm = null;
                }

                if (Xrm == null)
                    alert("Cannot retrieve Xrm object");
            }

No comments: