Bhoopathi

"Be Somebody Nobody Thought You could Be"

Thursday, March 2

iframe onSave Events in MS CRm 2011

 //1.Save record embedded in iframe
//Account form with iframe, that displays contact form. Requirement is that when user press “save” button on account form, contact in iframe should be also saved.
var iframeXrmPage = Xrm.Page.getControl(“IFRAME_contact”).getObject().contentWindow.contentIFrame.Xrm.Page;
// 2. Suppose this is our custom entity named “new_mycustomentity” and we would like to show it in inside iframe within a custom aspx page.
//Users can create the new record from within the custom page.
//So we set the Iframe src to the url for creating new my custom entity record i.e.
//<iframe id=”IFRAME_NEWCUSTOMENTITY” frameborder=”0″ runat=”server”
//src = “http://localhost/Contoso/main.aspx?etn=new_mycustomentity&pagetype=entityrecord&#8221; & gt;
//However this is how it appears within in IFrame with no ribbons in it.
//So the next step would be to hide the blank space on the top as well as left navigation bar.Now we can use the following JavaScript that will hide the left navigation pane and blank ribbon.http://bingsoft.wordpress.com/2010/09/09/mscrm-2011-hide-areas-of-a-form/
function HideArea() {
// Hide the Ribbon Toolbar and move the form Content area to the top of the window
window.parent.document.getElementById(“crmTopBar”).style.display = “none”;
window.parent.document.getElementById(“crmContentPanel”).style.top = “0px”;
// Move Form Content area up to top of window, initial style.top is 135px
// set it to the height of the iframe
window.parent.document.getElementById(‘contentIFrame’).style.height = “400px”;
// Hide Left Hand Nav bar / pane
document.getElementById(“crmNavBar”).parentElement.style.display = “none”;
document.getElementById(“tdAreas”).parentElement.parentElement.parentElement.parentElement.colSpan = 2;
// Hide the Form Footer Bar
document.getElementById(“crmFormFooter”).parentElement.style.display = “none”;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// 3. How do I Validate with OnChange?
//Luckily this isn’t really that big of a deal. There are several options and its really whatever is best for you.
//Option 1: Blank the field.
//By far the easiest option is to just clear the field after prompting the user. The only downside is the user has to re-enter all of the data again.
function example(obj){
var rgCell = /[0-9]{3}-[0-9]{3}-[0-9]{4}/;
var sVal = obj.getEventSource().getValue();
if (rgCell.test(sVal)) {
// valid
} else {
// Invalid phone number
alert(‘Please enter a valid phone number (ex. 123-123-1234).’);
obj.getEventSource().setValue(”);
}
}
//Option 2: Utilize the OnSave
//There is a little more effort involved here, but a better experience for the user.
function example(obj){
var rgCell = /[0-9]{3}-[0-9]{3}-[0-9]{4}/;
var sVal = Xrm.Page.getAttribute(“mobilephone”).getValue(); //obj.getEventSource().getValue();
if (rgCell.test(sVal)) {
// valid
} else {
// Invalid phone number
alert(‘Please enter a valid phone number (ex. 123-123-1234).’);
Xrm.Page.getAttribute(“mobilephone”).controls.get(0).setFocus(); // only fires onSave
if (obj.getEventArgs() != null)
obj.getEventArgs().preventDefault();
}
}

Hide Insert Template Button in Email – IFrame Form

// Hide Insert Template Button in Email – IFrame Form
Some times we may get the requirement to hide the Insert Template button on the Inner Email iframe as shown velow. To hide this button call the below function in Form onLoad EventEmailTemp
function HideInsertTemplateButton()
{
var getInnerEmailFrame = window.top.document.getElementById(‘contentIFrame’);
var emailFrameDoc = getInnerEmailFrame.contentDocument ||      getInnerEmailFrame.contentWindow.document;
var el = emailFrameDoc.getElementById(‘idinserttemplate’);
if (el){
el.parentNode.removeChild(el);
}
}

Pass Custom Parameters to The HTML Web Resource through Iframe -XRM2011

Pass Custom Parameters to The HTML Web Resource through Iframe -XRM2011
Pass XRM Form Data to the web resource as Custom Parameters as follows:
function SendCustomUrlToIframe(ifarmeUrl, iFrame) {

 var customerId = Xrm.Page.data.entity.getId();

 if (customerId != null) 
{
  Xrm.Page.getControl(iFrame).setSrc(ifarmeUrl + "&id=" + customerId); 
}
}

Another Example:

The following sample shows you how to set the src property for the IFRAME and any parameters by using the onChange event of an option set field.
JScript
//Get the value of an option set attribute
var value = Xrm.Page.data.entity.attributes.get("new_pagechooser").getValuewTarget = "";
//Set the target based on the value of the option set
switch (value) {
    case 100000001:
        newTarget = "http://myServer/test/pageOne.aspx";
        break;
    default:
        newTarget = "http://myServer/test/pageTwo.aspx";
        break;
}
//Get the default URL for the IFRAME, which includes the 
// query string parameters
var IFrame = Xrm.Page.ui.controls.get("IFRAME_testar Url = IFrame.getSrc();
// Capture the parameters
var params = Url.substr(Url.indexOf("?"));
//Append the parameters to the new page URL
newTarget = newTarget + params;
// Use the setSrc method so that the IFRAME uses the
// new page with the existing parameters
IFrame.setSrc(newTarget);
Cheers,
Leave a comment

Accessing Xrm.Page through JavaScript MS CRM form in an IFrame

Accessing Xrm.Page or CrmForm through JavaScript from an html page showing CRM form in an IFrame and Saving data inside Iframe

Suppose we have an html page having an iframe that shows a MS CRM Record. If we want to access the attributes on the form or call the save function from our html page(Iframe), we can do it in the following manner.
If you try from CRM 4.0 then use the below code
var crmForm =document.getElementById(“myIframe”).contentWindow.document.getElementById(“contentIFrame”)
.document.frames[0].document.forms[‘crmForm’];
If you try from CRM 5.0 then use the below code
var XrmPage =document.getElementById(“myIframe”).contentWindow.document.getElementById(“contentIFrame”)
.document.frames[0].Xrm.Page;
and if you want to save the Data filled in Html Page(Iframe) follow the below procedure to save the data.
  • First, let’s assume you have an iFrame inside an entity.
    Secondly, we need to uncheck the “Restrict cross-frame scripting” check box on the iFrame properties.
    Lastly, it’s a good idea to make sure our iFrame is using HTTPS to prevent mixed mode security warnings.

    Talking Directly

    If you try to talk directly to the iFrame via something like:
    Xrm.Page.ui.controls.get(‘IFRAME_opp’).getObject().contentWindow.document
    Unless you are On-Premise, you may encounter the “Access is Denied” warning. This will show itself when dealing with URLs on a different domain (or subdomain). Cross “domain” scripting is a security feature within the browser. Now an easy fix is to disable the browser settings, but this isn’t a good solution.

    Messaging

    Instead of talking directly, we need send a messages to our iFrame. To send a message, we’d do something like:
    Xrm.Page.ui.controls.get(‘IFRAME_opp’).getObject().contentWindow.postMessage(‘test’, “*”)
    To receive the message on the iFrame, you need to listen for the message event. Let’s take a look at the JavaScript on our example iFrame page:
    function receiveMessage(e)
    {
    if (e.origin == ‘https://way.crm.dynamics.com&#8217;)
    {
    if (e.data == ‘test’)
    {
    document.getElementById(‘msgBox’).innerHTML = ‘testing…<br />’ + e.origin + ‘<br />’ + e.data;
    }
    if (e.data == ‘save’)
    {
    saveForm();
    }
    }
    }
    saveForm = function()
    {
    document.getElementById(‘msgBox’).innerHTML = ‘saving…’;
    }
    window.attachEvent(“onmessage”, receiveMessage);
    For ref plese use click this link http://msdn.microsoft.com/en-us/library/cc197015(VS.85).aspx
    Cheers,