Link to home
Start Free TrialLog in
Avatar of MindenMan
MindenManFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Cannot print conntents of IFrame in CRM 4.0

Hi all;
I'm using MSCRM4.0 and I've created an Iframe that refers to another html page that then brings up a map of where our customer is - as per the following article:
http://blogs.msdn.com/crm/archive/2007/08/01/adding-live-maps-to-microsoft-crm-3-0.aspx

The iFrame and map works a treat and brings up more or less any address in the UK.  However, when I go to the "print" button on the account window and the preview is generated I just get an empty window along with all the other account info printed.  I've tried just adding a print button with script to the html page.  This works kinda, in that the map prints but I also get numerous script errors such as:
 
Line 38 Char 1 LOCID_UI_DIR undefined

Line 174 Char 1 Object expected

Line 115 1 _bPresenceEnabled is undefined

These errors appear to come from the following page:
sfa/accts/edit.aspx?id={GUID of account is here}

My current print "fix" is using a print.js file as follows:
if ( printIsNativeSupport() )
  window.print2 = window.print;
window.print = printFrame;

// main stuff
function printFrame(frame, onfinish) {
  if ( !frame ) frame = window;

  function execOnFinish() {
    switch ( typeof(onfinish) ) {
      case "string": execScript(onfinish); break;
      case "function": onfinish();
    }
    if ( focused && !focused.disabled ) focused.focus();
  }

  if ( frame.document.readyState !== "complete" &&
       !confirm("The document to print is not downloaded yet! Continue with printing?") )
  {
    execOnFinish();
    return;
  }

 if ( window.print2 ) { // IE5
    var focused = document.activeElement;
    frame.focus();
    if ( frame.print2 ) frame.print2();
    else frame.print();
    execOnFinish();
    return;
  }
 
  var eventScope = printGetEventScope(frame);
  var focused = document.activeElement;
 
  window.printHelper = function() {
    execScript("on error resume next: printWB.ExecWB 6, 1", "VBScript");
    printFireEvent(frame, eventScope, "onafterprint");
    printWB.outerHTML = "";
    execOnFinish();
    window.printHelper = null;
  }
 
  document.body.insertAdjacentHTML("beforeEnd",
    "<object id=\"printWB\" width=0 height=0 \
    classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\">");
 
  printFireEvent(frame, eventScope, "onbeforeprint");
  frame.focus();
  window.printHelper = printHelper;
  setTimeout("window.printHelper()", 0);
}


// helpers
function printIsNativeSupport() {
  var agent = window.navigator.userAgent;
  var i = agent.indexOf("MSIE ")+5;
  return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0;
}

function printFireEvent(frame, obj, name) {
  var handler = obj[name];
  switch ( typeof(handler) ) {
    case "string": frame.execScript(handler); break;
    case "function": handler();
  }
}

function printGetEventScope(frame) {
  var frameset = frame.document.all.tags("FRAMESET");
  if ( frameset.length ) return frameset[0];
  return frame.document.body;
}

And my HTML page is this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>

    <head>

    <title></title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <script type="text/jscript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1" mce_src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1"></script>
   
      <script type="jscript" src="print.js"></script>
<script type="text/jscript">
function window.onload() {
  idPrint.disabled = false;
}

var originalTitle;
function beforeprint() {
  idPrint.disabled = true;
  originalTitle = document.title;
  document.title = originalTitle + " - by Baseline Consultancy";
}

function afterprint() {
  document.title = originalTitle;
  idPrint.disabled = false;
}

</script>

<script type="text/jscript">

    var map = null;
    var Address = "";
    var latitude = null;
    var longitude = null;
    function GetMap()

    {
    // Try to get lat&long from the CRM

    latitude = parent.document.forms[0].all.address1_latitude.DataValue;

    longitude = parent.document.forms[0].all.address1_longitude.DataValue;
   
    //Get adress from CRM
    var addressline1 = parent.document.forms[0].all.address1_line1.DataValue;
    var city = parent.document.forms[0].all.address1_city.DataValue;
    var County = parent.document.forms[0].all.address1_stateorprovince.DataValue;
    var PostCode = parent.document.forms[0].all.address1_postalcode.DataValue;
    var Country = parent.document.forms[0].all.address1_country.DataValue;
   
if (Country == null)
    {
     Country = "United Kingdom"
     }

if (PostCode == null)
    {
     alert ("You must have a value for the Postcode in order to map the address, better still ensure you have the correct full address for a more accurate mapping!");
     }

      Address = addressline1+", "+city+", "+County+", "+PostCode+","+Country;

    map = new VEMap('myMap');

    map.LoadMap();
    if (latitude != null && longitude != null)

        {

        PlacePushPin(latitude,longitude);

        }

        else

        {

        map.Find(null,Address,null,null,0,10,true,true,true,true,FindCallBack);

        }

        }

        function FindCallBack(shapeLayer, results, positions, moreResults, e)

        {

        if(positions != null && positions.length > 0)

        {

        PlacePushPin(positions[0].LatLong.Latitude,positions[0].LatLong.Longitude);

        latitude = positions[0].LatLong.Latitude;

        longitude = positions[0].LatLong.Longitude;

        parent.document.forms[0].all.address1_latitude.DataValue = latitude;

        parent.document.forms[0].all.address1_longitude.DataValue = longitude;
       
      
      

        }

        }

        function PlacePushPin(lat, lon)

        {

        latlong = new VELatLong(lat,lon);

        customerPushPin = new VEShape(VEShapeType.Pushpin,latlong);

        map.AddShape(customerPushPin);

        map.SetCenterAndZoom(latlong,15);

        }

    </script>
     


    </head>

    <body onload="GetMap();">
<onbeforeprint="beforeprint()"onafterprint="afterprint()"  bgcolor="infobackground">
<p><input name="idPrint" type="button" value="Print this page" onclick="print()"/> </p>


    <div id='myMap' style="position:relative; width:600px; height:600px;"></div>

    </body>

    </html>

Any help would be greatly appreciated as this is driving me crazy!
Thanks
MM


ASKER CERTIFIED SOLUTION
Avatar of MindenMan
MindenMan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lucas
MindenMan, care to share how you solved it?  EE is such a pain, i just wasted my freebie on this post where you put "I've solved this myself."