Friday, June 25, 2010

Advanced find view in an IFRAME in CRM from in MS CRM 4.0

// Fetch xml for the data we want to show in a CRM grid

fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"+
"<entity name='opportunity'>"+
"<attribute name='name'/>"+
"<attribute name='createdon'/>"+
"<attribute name='ownerid'/>"+
"<attribute name='new_finalstage'/>"+
"<attribute name='opportunityid'/>"+
"<order attribute='name' descending='false'/>"+
"<filter type='and'><condition attribute='statecode' operator='eq' value='0'/></filter>"+
"</entity></fetch>";

/* Layout of the adv find Grid
In object - is the objecttypecode of the result entity
jump - is the name of the primary column in the result view */

var layoutXml = ""+
"<grid name='resultset' object='3' jump='name' select='1' icon='1' preview='1'>"+
"<row name='result' id='opportunity'>"+
"<cell name='name' width='230' />"+
"<cell name='createdon' width='80' />"+
"<cell name='ownerid' width='120' />"+
"<cell name='new_finalstage' width='160' />"+
"</row>"+
"</grid>";

/* the main method to retrieve and bind the data to the CRM Grid according to the fetch XML and Layout XML
IFRAME_TEST- an IFRAME to show the advance find view
opportunity – is the main entity name
fetchXml – the fetchXml query we want to retrieve from CRM
layoutXml – the layout to show the retrieved data
name – sorting of records in CRM advanced find grid
false – sort order Descending
{350D0AA8-3EF5-496B-A714-15B2DE896D20} – advance find view id for the same entity (opportunity) */

EmbedAdvancedFindView("IFRAME_TEST", "opportunity", fetchXml, layoutXml, "name", "false", "{350D0AA8-3EF5-496B-A714-15B2DE896D20}");

// to remove the toolbar items from the advance find grid
var iFrame1 = null;
iFrame1 = crmForm.all.IFRAME_TEST;
var iDoc1 = iFrame1.contentWindow.document;
iFrame1.attachEvent( "onreadystatechange" , Ready1);
RemoveWhiteSpace(iFrame1);


function Ready1(){
try{
var iFrame = crmForm.all.IFRAME_TEST;
var iDoc1 = iFrame.contentWindow.document;
if(iDoc1.getElementById("gridMenuBar") != null)
iDoc1.getElementById("gridMenuBar").style.display = "none";
}catch (e)
{}
}


function EmbedAdvancedFindView(iFrameId, entityName, fetchXml, layoutXml, sortCol, sortDescend, defaultAdvFindViewId) {
try {

var iFrame = document.getElementById(iFrameId);
var httpObj = new ActiveXObject("Msxml2.XMLHTTP");
var url = "/" + ORG_UNIQUE_NAME + "/advancedFind/fetchData.aspx";

var params = "FetchXML=" + fetchXml
+ "&LayoutXML=" + layoutXml
+ "&EntityName=" + entityName
+ "&DefaultAdvFindViewId=" + defaultAdvFindViewId
+ "&ViewType=1039" // According to Michael Hohne over at Stunnware
+ "&SortCol=" + sortCol
+ "&SortDescend=" + sortDescend;

httpObj.Open("POST", url, true);

httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpObj.setRequestHeader("Content-length", params.length);

httpObj.onreadystatechange = function() {
if (httpObj.readyState == 4 && httpObj.status == 200) {
var doc = iFrame.contentWindow.document;
iFrame.src = null;
doc.open();
doc.write(httpObj.responseText);
doc.close();

doc.body.style.padding = "0px";
doc.body.style.backgroundColor = "#eaf3ff";
}
}
httpObj.send(params);
}
catch (e) {
}
}

// to expand the adv find grid to full width
function RemoveWhiteSpace(iframe)
{
var IFRAME_test = iframe;
IFRAME_test.onreadystatechange = function ()
{
if( IFRAME_test.readyState != 'complete' )
return;
IFRAME_test.style.border = "0px";
var IframeWindo = IFRAME_test.contentWindow;
if(IframeWindo.document.body.childNodes[0]!=null){
if(IframeWindo.document.body.childNodes[0].rows!=null){
IframeWindo.document.body.scroll = "no";
IframeWindo.document.body.childNodes[0].rows[0].cells[0].style.padding = "0px";
}
}
}
}

7 comments:

Christian said...

It looks very interesting :-)
However I keep getting the error that the record was not found.
Might it be that I need to change the defaultAdvFindViewId?

Chandan said...

Yes correct, please change the GUID to the advance find view id of the same entity. Configure every component as its written in my blog, It should work. If doesn't work, please let me know the error message.
Thank you.

sander said...

Thanks a million for the remove toolbar items code :)

didi said...

Thanks for your post.
But when i make entry through advanced view. To display current record in view. i need to click on refresh button. I need to have it as autorefreshment.

Could you please tell me how it can be done?

Priya said...

Thanks. Can we remove the space above the grid (the menubar is hidden though), but there is space in that area.

Adventurer's Scribblings said...

Chandan,

Hai I am Jayakumar Janakiraman. You post was very helpful for showing the Grid in an Customized ASP form in ISV folder.

Please do keep up your great work.

Jayakumar.Janakiraman@gmail.com

Anonymous said...

Thank you So much,,,it worked for me perfectly.