Home > MS CRM 4.0 > Display Fetch in IFRAME

Display Fetch in IFRAME


The blog entry from Adi,
Check original post here
 
 
My first post about displaying fetch inside an IFRAME has two disadvantages. The first is that the context of the parent window does not follow to the new entity window when you click on add new record (grid) button. This is might be annoying if you’re counting on the default mapping between the two entities. The second problem is that the grid does not refresh after you add (new button) or remove (delete button) a record. This means you are not able to see the changes unless you press on the grid refresh button your self.

I made piece with the fact that the parent context did not follow through to the child window since I was using advanced find. But the fact that the gird does not refresh automatically came as a surprise since I remember this feature working “AS IS” in v3.0. I guess ms did a few changes to the auto function which searched and refreshed the grid. Thanks to Dave Berry who commented / Posted about this and brought it to my attention.

In order to overcome this issue I added two new properties to the FetchViewer class.

The first parameter is called WithParentContext. This property allows you to decide whether the parent window object id is passed to the child window. I made it a public (as opposed to internal) choice since I figured you can display any type of query inside the FetchViewer including a query that does not reflect a direct parent child relationship.

The second parameter is the EntityCode (ObjectTypeCode) of the entity being fetched. If you want the grid to refresh automatically you must supply this value. This is important since the value is eventually passed to MS original auto function which expects this value.

The rest of the fetch viewer class was not changed and you can read all about it here.

  1. function OnCrmPageLoad()  
  2. {  
  3.     window.fetchContacts = new FetchViewer("IFRAME_test");  
  4.     fetchContacts.WithParentContext = true;  
  5.     fetchContacts.EntityCode = 2;  
  6.     fetchContacts.FetchXml  = getFetchXml();  
  7.     fetchContacts.LayoutXml = getLayoutXml();  
  8.     fetchContacts.Entity    = "contact";  
  9.     fetchContacts.QueryId   = "{00000000-0000-0000-00AA-000000666400}";  
  10.     fetchContacts.RegisterOnTab(0); //IFRAME ON THE FIRST TAB  
  11. }  
  12.   
  13. function getFetchXml()  
  14. {  
  15.     return ‘<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"><entity name="contact"><attribute name="fullname"/><attribute name="telephone1"/><attribute name="contactid"/><order attribute="fullname" descending="false"/><filter type="and"><condition attribute="parentcustomerid" operator="eq" uitype="account" value="’ + crmForm.ObjectId + ‘"/></filter></entity></fetch>’;  
  16. }  
  17.   
  18. function getLayoutXml()  
  19. {  
  20.     return ‘<grid name="resultset" object="2" jump="lastname" select="1" icon="1" preview="1"><row name="result" id="contactid"><cell name="fullname" width="300" /><cell name="telephone1" width="125" /></row></grid>’;  
  21. }  
  22.   
  23. function FetchViewer( iframeId )  
  24. {  
  25.     var Instance = this;  
  26.     var vDynamicForm;  
  27.     var m_iframeTab;  
  28.     var m_iframeDoc;  
  29.     var m_iframeShowModalDialogFunc = null;  
  30.     var m_windowAutoFunc = null;  
  31.    
  32.     Instance.WithParentContext = false;  
  33.     Instance.EntityCode = 0;  
  34.     Instance.Entity    = "";  
  35.     Instance.Iframe    = null;  
  36.     Instance.FetchXml  = "";  
  37.     Instance.QueryId   = "";  
  38.     Instance.LayoutXml = "";  
  39.   
  40.     Instance.RegisterOnTab  = function( tabIndex )  
  41.     {   
  42.         Instance.Iframe = document.getElementById( iframeId );    
  43.   
  44.         if( !Instance.Iframe )  
  45.         {  
  46.             return alert( "Iframe " + iframeId + " is undefined" );  
  47.         }  
  48.     
  49.         m_iframeDoc = getIframeDocument();  
  50.         var loadingGifHTML  = "<table height=’100%’ width=’100%’ style=’cursor:wait’>";  
  51.         loadingGifHTML += "<tr>";  
  52.         loadingGifHTML += "<td valign=’middle’ align=’center’>";  
  53.         loadingGifHTML += "<img alt=” src=’/_imgs/AdvFind/progress.gif’/>";  
  54.         loadingGifHTML += "<div/><b>Loading View…</b>";  
  55.         loadingGifHTML += "</td></tr></table>";  
  56.         m_iframeDoc.body.innerHTML = loadingGifHTML;  
  57.   
  58.         if( parseInt( "0" + tabIndex ) == 0 )   
  59.         {  
  60.             Instance.Refresh();  
  61.         }  
  62.         else   
  63.         {  
  64.             Instance.Iframe.attachEvent( "onreadystatechange" , RefreshOnReadyStateChange );   
  65.         }  
  66.     }  
  67.   
  68.     function RefreshOnReadyStateChange()  
  69.     {  
  70.         if( Instance.Iframe.readyState != ‘complete’ )  
  71.         {  
  72.             return;  
  73.         }  
  74.     
  75.         Instance.Refresh();  
  76.     }  
  77.   
  78.     Instance.Refresh = function()  
  79.     {  
  80.         if( !Instance.Iframe )  
  81.         {  
  82.             return alert( "Iframe " + iframeId + " is undefined" );  
  83.         }  
  84.    
  85.         m_iframeDoc = getIframeDocument();    
  86.   
  87.         Instance.Iframe.detachEvent( "onreadystatechange" , RefreshOnReadyStateChange );  
  88.   
  89.         var create  = m_iframeDoc.createElement;  
  90.         var append1 = m_iframeDoc.appendChild;   
  91.         vDynamicForm = create("<FORM name=’vDynamicForm’ method=’post’>");  
  92.   
  93.         var append2 = vDynamicForm.appendChild;  
  94.         append2(create("<INPUT type=’hidden’ name=’FetchXml’>"));  
  95.         append2(create("<INPUT type=’hidden’ name=’LayoutXml’>"));  
  96.         append2(create("<INPUT type=’hidden’ name=’EntityName’>"));  
  97.         append2(create("<INPUT type=’hidden’ name=’DefaultAdvFindViewId’>"));  
  98.         append2(create("<INPUT type=’hidden’ name=’ViewType’>"));  
  99.         append1( vDynamicForm );  
  100.   
  101.         vDynamicForm.action = "/" + ORG_UNIQUE_NAME + "/AdvancedFind/fetchData.aspx";  
  102.         vDynamicForm.FetchXml.value   = Instance.FetchXml;  
  103.         vDynamicForm.LayoutXml.value  = Instance.LayoutXml;  
  104.         vDynamicForm.EntityName.value = Instance.Entity;  
  105.         vDynamicForm.DefaultAdvFindViewId.value = Instance.QueryId;  
  106.         vDynamicForm.ViewType.value = 1039;  
  107.         vDynamicForm.submit();  
  108.   
  109.         Instance.Iframe.attachEvent( "onreadystatechange" , OnViewReady );  
  110.     }  
  111.   
  112.     function OnViewReady()  
  113.     {  
  114.         if( Instance.Iframe.readyState != ‘complete’ )   
  115.         {  
  116.             return;  
  117.         }  
  118.     
  119.         Instance.Iframe.style.border = 0;  
  120.         Instance.Iframe.detachEvent( "onreadystatechange" , OnViewReady );  
  121.      
  122.         if (Instance.WithParentContext == true)  
  123.         {  
  124.             getIframeWindow().open = OnWindowOpen;  
  125.         }  
  126.     
  127.         if (m_iframeShowModalDialogFunc == null)  
  128.         {  
  129.             m_iframeShowModalDialogFunc = getIframeWindow().showModalDialog;  
  130.             getIframeWindow().showModalDialog = OnIframeShowModalDialog;  
  131.         }  
  132.     
  133.         if (Instance.EntityCode > 0)  
  134.         {   
  135.             if (m_windowAutoFunc == null)  
  136.             {  
  137.                 m_windowAutoFunc = window.auto;  
  138.                 window.auto = OnWindowAuto;  
  139.             }  
  140.         }  
  141.     
  142.         m_iframeDoc = getIframeDocument();  
  143.         m_iframeDoc.body.scroll = "no";  
  144.         m_iframeDoc.body.style.padding = "0px";     
  145.     }  
  146.   
  147.     function OnWindowOpen(url, name, features)   
  148.     {  
  149.         //new window  
  150.         if (url.indexOf(‘?’) == -1)  
  151.         {  
  152.             if (url.indexOf(‘userdefined’) == -1 )   
  153.             {  
  154.                 url = url + "?_CreateFromType=" + crmForm.ObjectTypeCode + "&_CreateFromId=" + crmForm.ObjectId;  
  155.             }  
  156.             else   
  157.             {  
  158.                 url = url + "?_CreateFromType=" + crmForm.ObjectTypeCode + "&_CreateFromId=" + crmForm.ObjectId + "&etc=" + Instance.EntityCode + "#";  
  159.             }  
  160.       }   
  161.      
  162.         return window.open(url, name, features);  
  163.     }  
  164.    
  165.     function OnIframeShowModalDialog(sUrl, vArguments, sFeatures)  
  166.     {  
  167.         m_iframeShowModalDialogFunc(sUrl, vArguments, sFeatures);  
  168.         Instance.Refresh();  
  169.     }  
  170.    
  171.     function OnWindowAuto(otc)  
  172.     {  
  173.         if ( otc == Instance.EntityCode )  
  174.         {  
  175.             getIframeDocument().all.crmGrid.Refresh();  
  176.         }  
  177.   
  178.         m_windowAutoFunc(otc);  
  179.     }  
  180.   
  181.     function getIframeDocument()  
  182.     {  
  183.         return getIframeWindow().document;  
  184.     }  
  185.    
  186.     function getIframeWindow()  
  187.     {  
  188.         return Instance.Iframe.contentWindow;  
  189.     }  
  190. }  
  191.   
  192. OnCrmPageLoad();  
Advertisements
Categories: MS CRM 4.0
  1. No comments yet.
  1. No trackbacks yet.

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: