Thursday, December 22, 2011

Display Custom Advance Find view in Iframe in CRM 2011

Hi All, To bind FetchXml to Iframe Adi Katz,MVP has provided nice article to do in CRM4.0. Thanks Adi Katz for the great article. I just started converting it to work with CRM 2011. Here is the working copy of mine, to bind FetchXml to Iframe.


function loadIFrame() {

    window.fetchActivtities = new FetchViewer("IFRAME_TEST");
    fetchActivtities.FetchXml = getFetchXml();
    fetchActivtities.LayoutXml = getLayoutXml();
    fetchActivtities.Entity = "activitypointer";
    fetchActivtities.QueryId = "{00000000-0000-0000-00aa-000010001899}"; // view GUID
    fetchActivtities.RegisterOnTab(2); //IFRAME TAB INDEX


}



function getFetchXml() {

//  FetchXML Query
    return ' <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">' +
                '<entity name="activitypointer">' +
                 ' <attribute name="subject" />' +
                 ' <attribute name="scheduledstart" />' +
                 ' <attribute name="regardingobjectid" />' +
                 ' <attribute name="prioritycode" />' +
                 ' <attribute name="scheduledend" />' +
                 ' <attribute name="activitytypecode" />' +
                 ' <attribute name="instancetypecode" />' +
                 ' <order attribute="scheduledend" descending="false" />' +
                '<filter type="and">'+
                  '<condition attribute="regardingobjectid" operator="in">'+
                    '<value  uitype="account">{7CC58DF6-3114-E111-8E22-1CC1DEEAE7D7}</value>'+                  
                 ' </condition>'+
                '</filter>'+
                '  <attribute name="activityid" />' +
               ' </entity>' +
             ' </fetch>';

}

function getLayoutXml() {

// grid layout, you can get easily from Customization.xml file
    return '<grid name="resultset" object="4200" jump="subject" select="1" icon="1" preview="1">' +
               ' <row name="result" id="activityid" multiobjectidfield="activitytypecode">' +
               '   <cell name="instancetypecode" width="100" ishidden="1" />' +
               ' </row>' +
             ' </grid>';

}

function FetchViewer(iframeId) {
    var Instance = this;
    var vDynamicForm;
    var m_iframeTab;
    var m_iframeDoc;

    Instance.Entity = "";
    Instance.Iframe = null;
    Instance.FetchXml = "";
    Instance.QueryId = "";
    Instance.LayoutXml = "";

    Instance.RegisterOnTab = function (tabIndex) {
        Instance.Iframe = document.getElementById(iframeId);

        if (!Instance.Iframe)
            return alert("Iframe " + iframeId + " is undefined");

        m_iframeDoc = getIframeDocument();
        var loadingGifHTML = "<table height='100%' width='100%' style='cursor:wait'>";
        loadingGifHTML += "<tr>";
        loadingGifHTML += "<td valign='middle' align='center'>";
        loadingGifHTML += "<img alt='' src='/_imgs/AdvFind/progress.gif'/>";
        loadingGifHTML += "<div/><b>Loading View...</b>";
        loadingGifHTML += "</td></tr></table>";
        m_iframeDoc.body.innerHTML = loadingGifHTML;

        Instance.Refresh();

    }

    function RefreshOnReadyStateChange() {

        if (Instance.Iframe.readyState != 'complete')
            return;

        Instance.Refresh();
    }

    Instance.Refresh = function () {
   
        if (!Instance.Iframe)
            return alert("Iframe " + iframeId + " is undefined");

        m_iframeDoc = getIframeDocument();

        Instance.Iframe.detachEvent("onreadystatechange", RefreshOnReadyStateChange);

        var create = m_iframeDoc.createElement;
        var append1 = m_iframeDoc.appendChild;
        vDynamicForm = create("<FORM name='vDynamicForm' method='post'>");

        var append2 = vDynamicForm.appendChild;
        append2(create("<INPUT type='hidden' name='FetchXml'>"));
        append2(create("<INPUT type='hidden' name='LayoutXml'>"));
        append2(create("<INPUT type='hidden' name='EntityName'>"));
        append2(create("<INPUT type='hidden' name='DefaultAdvFindViewId'>"));
        append2(create("<INPUT type='hidden' name='ViewType'>"));
        append1(vDynamicForm);

        vDynamicForm.action = prependOrgName("/AdvancedFind/fetchData.aspx");
        vDynamicForm.FetchXml.value = Instance.FetchXml;
        vDynamicForm.LayoutXml.value = Instance.LayoutXml;
        vDynamicForm.EntityName.value = Instance.Entity;
        vDynamicForm.DefaultAdvFindViewId.value = Instance.QueryId;
        vDynamicForm.ViewType.value = 1039;
        vDynamicForm.submit();


        Instance.Iframe.attachEvent("onreadystatechange", OnViewReady);
    }

    function OnViewReady() {
        if (Instance.Iframe.readyState != 'complete') return;

        Instance.Iframe.style.border = 0;
        Instance.Iframe.detachEvent("onreadystatechange", OnViewReady);
        m_iframeDoc = getIframeDocument();
        m_iframeDoc.body.scroll = "no";
        m_iframeDoc.body.style.padding = "0px";
    }

    function getIframeDocument() {
        return Instance.Iframe.contentWindow.document;
    }

}


Note: This script is completely unsupported. it may break in the future with roll-up updates. 

37 comments:

  1. Hello,

    this is great post. It worked perfectly.

    The only issue I have is that ribbon does not change.

    I would like to get ribbon working and have charts available too.

    This seems to me is possible only in subgrid control.

    Do you have solution for us to share ?

    Thanks Jiri

    ReplyDelete
    Replies
    1. Hi, did you stumble uppon any solution yet? I have the same problem

      Delete
  2. Hi Jiri,

    Thanks for your response. Ribbon does not change using this approach. I did not find any solution regarding this issue. you can use subgrid to change ribbon.

    ReplyDelete
  3. Hello,

    With the above code is the on_click event got fired. I need to open the Contact Form etc when the row in the grid get clicked. Any help ? In my case when I click nothing happens.

    Furqan

    ReplyDelete
  4. Hi Ali,

    Here we are displaying adv find view in an Iframe. OOB Adv find view funtions will work. lookup fileds will open records on click.
    can you please provide me more information about your issue, I am not clear it. i can try to help you.

    ReplyDelete
  5. Hello Guru,

    The Code works perfectly. The only issue is I am facing is I am unable to open the Record when the user clicks on the Grid Row. Unable to open the entity record when the user clicks or double clicks on the record.

    regards
    Jayakumar Janakiraman

    ReplyDelete
  6. Hi Guru,
    Unable to open the entity record when the user clicks on the grid row.
    let me know if you have the solution for this.

    regards
    Jayakumar Janakiraman

    ReplyDelete
  7. Hi Jay,

    It seems grid does not have required fields in the layout to open the records. As mentioned in the above example instancetypecode(hidden field in the grid) & activityid(primary key) must be included in the fetchXML and also in the Grid Layout.

    Try adding these field. Hope it will resolves the issue. I would suggest get the fetchXML from Adv.find and modify accordingly.

    Thanks,
    Guru

    ReplyDelete
  8. thanks Guru this saved my day

    ReplyDelete
  9. Many users are reporting an XSS security error pop-up. It seems the only fix is to change a machine's local security settings (add CRM to trusted sites, set trusted sites to low security, etc). Is this normal?

    ReplyDelete
  10. I'm not sure the exact issue. But adding CRM site to Trusted sites resolves most of the issues.

    ReplyDelete
  11. How do we determine the GUID for the QueryID?

    ReplyDelete
    Replies
    1. Open any view in customization mode and then use ctr + N , it opens view in new window and you can get the ID of the view.
      other way is using Data base to find the view GUID.

      Delete
  12. Hi Guru Prasad - This is a great post, albeit too advanced for me at this time. I am new to Visual Studio and the BIDS environment and I have a question regarding fetch xml, ssrs, and multiple entities to create a custom chart in CRM. Basically I want to create a chart to track trends through entities. I'd like one chart to display 4 trend lines for number of leads,opportunities,orders and invoices created per month. Is this possible?

    ReplyDelete
    Replies
    1. I think you can use SSRS for your requirement.

      Delete
  13. If I'm going to print preview funcionality, I get in error page on the print preview page. Can you suggest me something to solve this problem?

    ReplyDelete
  14. Hi
    Thanks for the code
    I have 2 questions:
    1. Can I rename the column names of the grid by some attribute of "cell" node in LayoutXml?
    2. Does the aditional functionality shown on the second part of Adi's article - auto refreshing the grid after adding a record - fits CRM2011?
    here is a link to the second article:
    http://mscrm4ever.blogspot.co.il/2009/03/display-fetch-in-iframe-part-2.html
    Thanks in advance
    Tikva

    ReplyDelete
    Replies
    1. I think you can rename Column names of the grid using "alias" property. tried long back, did not remembered.

      Delete
  15. Hello, thanks by your post.
    I'm getting an error in the line create(""). The error is "Invalid calling object". It was working an other environment, but in my new environment with update rollup 12 I'm getting the error.

    Do you know which could be the reason for this?

    Thank you!

    ReplyDelete
    Replies
    1. Hi Serigo,

      This script is completely unsupported one. I think with recent rollup 12 it might break. I did not tried it. check the advance find layout xml, i think it might changed.

      Thanks,
      Guru

      Delete
  16. Hi Gure,

    I took this post and created my iFrame with advanced find view. Now we updated CRM with latest update roll-up 12. The code stopped working on chrome, is there any code changes that you have for the same to work on chrome also.

    ReplyDelete
    Replies
    1. Hi Rose,

      Sorry, I did not tested this functionality recently after roll-up 12 update.

      Thanks,
      Guru

      Delete
  17. This is stopped working after UR12 service update. Getting 404 error after the Post. Any idea?

    ReplyDelete
    Replies
    1. Sorry, I did not tested this functionality after roll-up 12 release. I tried this long back. this is completely unsupported script. i think its broken now. MS might changed the Advance find grid layout.

      Delete
  18. do we have updates on this? same issue when we installed RU 12.

    ReplyDelete
    Replies
    1. Hi Benjo,

      This functionality is completely unsupported and after RU 12,its stopped working. You can't display Adv. Find views in IFrame. I tried updating this solution after RU 12, I'm able to display only System/custom view's in an IFrame using this approach. Personal view's not supporting.

      Thanks,
      Guru

      Delete
  19. Hi Guru, having the same problem on RU 12. Hope you can give us insights on how to solve this issue soon. thanks in advance..

    ReplyDelete
  20. Replies
    1. As i mentioned earlier, i made some changes to the above code after UR 12. i was successful in displaying system and custom view's in Iframe. But i was not able to display Personal view's/Adv.find view's in Iframe.
      I thought there is no use of displaying system/custom view's in Iframe, we could use sub-grid in place.

      Let me know what was your exact requirement? Thanks!

      Delete
    2. Guru,

      can you share with me the code that you modified after ur 12. I would like to show a system/custom view in an iframe. No need for personal view.

      Delete
    3. Hi Ali,

      I have updated script available on this post http://guruprasadcrm.blogspot.in/2013/07/displaying-view-in-iframe-crm-2011.html

      Delete
  21. Hi Guru, I used this code to display records in an IFrame and the IFrame resides inside a HTML page. I have a requirement to export these records into an excel file like we do in an advanced find.

    Is it possible? Any approach?

    ReplyDelete
    Replies
    1. you can try creating excel from FetchXML retrieved records. you can use file steam object or similar ones to create the file.

      Delete
  22. Hi Guru,
    The code worked perfectly fine in ie8 with Windows xp...But it is throwing the following error in Windows 7-ie9.And the iframe is not loading.
    Can u suggest me the code changes regaring this..
    Microsoft Dynamics CRM Error Report Contents


    1.0

    Invalid calling object
    120
    /%7B635061225790003236%7D/WebResources/new_notesview
    /userdefined/edit.aspx?_gridType=10000&etc=10000&id=%7bA6E626BD-5FCC-E211-A808-000C29C0A300%7d&pagemode=iframe&preloadcache=1370526470848&rskey=24864390
    anonymous(){if(!Instance.Iframe){returnalert("Iframe"+iframeId+"isundefined");}m_iframeDoc=getIframeDocument();Instance.Iframe.detachEvent("onreadystatechange",RefreshOnReadyStateChange);varcreate=m_iframeDoc.createElement;varappend1=m_iframeDoc.appendChi

    anonymous(){if(!Instance.Iframe){returnalert("Iframe"+iframeId+"isundefined");}m_iframeDoc=getIframeDocument();Instance.Iframe.detachEvent("onreadystatechange",RefreshOnReadyStateChange);varcreate=m_iframeDoc.createElement;varappend1=m_iframeDoc.appendChild;vDynamicForm=create("");varappend2=vDynamicForm.appendChild;append2(create(""));append2(create(""));append2(create(""));append2(create(""));append2(create(""));append1(vDynamicForm);vDynamicForm.action="/"+ORG_UNIQUE_NAME+"/AdvancedFind/fetchData.aspx";vDynamicForm.FetchXml.value=Instance.FetchXml;vDynamicForm.LayoutXml.value=Instance.LayoutXml;vDynamicForm.EntityName.value=Instance.Entity;vDynamicForm.DefaultAdvFindViewId.value=Instance.QueryId;vDynamicForm.ViewType.value=1039;vDynamicForm.submit();Instance.Iframe.attachEvent("onreadystatechange",OnViewReady);}



    Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BRI/2)
    en-us
    en-in
    en-in
    1366x768
    Web
    2013-06-06T19:17:52


    1033
    1033
    1033
    1033
    {02ED47A7-4BB2-E211-BB98-000C29C0A300}
    {E7DE6DE9-EAB7-E211-A699-000C29C0A300}
    5.0.9690.3236


    ReplyDelete
  23. Hi Guru,
    The code worked perfectly fine in ie8 with Windows xp...But it is throwing the following error in Windows 7-ie9.And the iframe is not loading.
    Can u suggest me the code changes regaring this..
    Microsoft Dynamics CRM Error Report Contents


    1.0

    Invalid calling object
    120
    /%7B635061225790003236%7D/WebResources/new_notesview
    /userdefined/edit.aspx?_gridType=10000&etc=10000&id=%7bA6E626BD-5FCC-E211-A808-000C29C0A300%7d&pagemode=iframe&preloadcache=1370526470848&rskey=24864390
    anonymous(){if(!Instance.Iframe){returnalert("Iframe"+iframeId+"isundefined");}m_iframeDoc=getIframeDocument();Instance.Iframe.detachEvent("onreadystatechange",RefreshOnReadyStateChange);varcreate=m_iframeDoc.createElement;varappend1=m_iframeDoc.appendChi

    anonymous(){if(!Instance.Iframe){returnalert("Iframe"+iframeId+"isundefined");}m_iframeDoc=getIframeDocument();Instance.Iframe.detachEvent("onreadystatechange",RefreshOnReadyStateChange);varcreate=m_iframeDoc.createElement;varappend1=m_iframeDoc.appendChild;vDynamicForm=create("");varappend2=vDynamicForm.appendChild;append2(create(""));append2(create(""));append2(create(""));append2(create(""));append2(create(""));append1(vDynamicForm);vDynamicForm.action="/"+ORG_UNIQUE_NAME+"/AdvancedFind/fetchData.aspx";vDynamicForm.FetchXml.value=Instance.FetchXml;vDynamicForm.LayoutXml.value=Instance.LayoutXml;vDynamicForm.EntityName.value=Instance.Entity;vDynamicForm.DefaultAdvFindViewId.value=Instance.QueryId;vDynamicForm.ViewType.value=1039;vDynamicForm.submit();Instance.Iframe.attachEvent("onreadystatechange",OnViewReady);}



    Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; BRI/2)
    en-us
    en-in
    en-in
    1366x768
    Web
    2013-06-06T19:17:52


    1033
    1033
    1033
    1033
    {02ED47A7-4BB2-E211-BB98-000C29C0A300}
    {E7DE6DE9-EAB7-E211-A699-000C29C0A300}
    5.0.9690.3236


    ReplyDelete
    Replies
    1. Hi Gowthami,

      This script is completely unsupported.Looks like with the recent rollup updates, this script not working.I think this script will no longer work. I did not tried it in the recent times.

      Thanks,
      Guru

      Delete