Tuesday, December 21, 2010

Use Custom View in the in form MS CRM 4.0

There is some times a requirement like people want to use custom views in MS CRM form instead of the available views also with some filtering criterias.
This is how it will work.

The followings are the things need to be taken care
1. collect the entity details
2. create an IFRAME
3. make sure the columns you want to put are present in CRM and searchable(imp)
4. this is just a sample, change it as per your requirements
5. this is just showing the record which you have selected, by taking the guid

so check the code,against most of the lines we have comments which will make it easy

//the main function which will do the majic

function EmbedAdvancedFindView (iFrameId, entityName, fetchXml, layoutXml, sortCol, sortDescend, defaultAdvFindViewId, entityTypeId) {
// Initialize our important variables
var httpObj = new ActiveXObject("Msxml2.XMLHTTP");
var url = SERVER_URL + "/AdvancedFind/fetchData.aspx";
var iFrame = document.getElementById(iFrameId);
var win = iFrame.contentWindow;
var doc = iFrame.contentWindow.document;

// Provide a global function within the parent scope to avoid XSS limitations
// in updating the iFrame with the results from our HTTP request
PushResponseContents = function (iFrame, httpObj, entityTypeId) {
var win = iFrame.contentWindow;
var doc = iFrame.contentWindow.document;
var m_iFrameShowModalDialogFunc = null;
var m_windowAutoFunc = null;

// Write the contents of the response to the Iframe
doc.open();
doc.write(httpObj.responseText);
doc.close();

// Set some style elements of the Advanced Find window
// to mesh cleanly with the parent record's form
doc.body.style.padding = "0px";
doc.body.scroll="no";

// Should we overwrite the functionality of the "New" button?
if ((typeof(entityTypeId) != "undefined") && (entityTypeId != null)) {
var buttonId = "_MBopenObj" + entityTypeId;
var newButton = doc.getElementById(buttonId);

eval("newButton.action = 'locAddRelatedToNonForm(" + entityTypeId + ", " + crmForm.ObjectTypeCode + ", \"" + crmForm.ObjectId + "\",\"\");'");
}

// Swap the showModalDialog function of the iFrame
if (m_iFrameShowModalDialogFunc == null) {
m_iFrameShowModalDialogFunc = win.showModalDialog;
win.showModalDialog = OnIframeShowModalDialog;
}

if (m_windowAutoFunc == null) {
m_windowAutoFunc = win.auto;
win.auto = OnWindowAuto;
}

// Configure the automatic refresh functionality for dialogs
function OnIframeShowModalDialog(sUrl, vArguments, sFeatures) {
m_iFrameShowModalDialogFunc(sUrl, vArguments, sFeatures);
doc.all.crmGrid.Refresh();
}

function OnWindowAuto(otc) {
doc.all.crmGrid.Refresh();

m_windowAutoFunc(otc);
}
}

// Without a null src, switching tabs in the form reloads the src
iFrame.src = null;

// Preload the iFrame with some HTML that presents a Loading image
var loadingHtml = ""
+ "<table height='100%' width='100%' style='cursor:wait'>"
+ " <tr>"
+ " <td valign='middle' align='center'>"
+ " <img alt='' src='/_imgs/AdvFind/progress.gif' />"
+ " <div /><i>Loading View...</i>"
+ " </td>"
+ " </tr>"
+ "</table>";

doc.open();
doc.write(loadingHtml);
doc.close();

// Compile the FetchXML, LayoutXML, sortCol, sortDescend, defaultAdvFindViewId, and viewId into
// a list of params to be submitted to the Advanced Find form
var params = "FetchXML=" + fetchXml
+ "&LayoutXML=" + layoutXml
+ "&EntityName=" + entityName
+ "&DefaultAdvFindViewId=" + defaultAdvFindViewId
+ "&ViewType=1039" // According to Michael Hohne over at Stunnware, this is static
+ "&SortCol=" + sortCol
+ "&SortDescend=" + sortDescend;

// Establish an async connection to the Advanced Find form
httpObj.open("POST", url, true);

// Send the proper header information along with the request
httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpObj.setRequestHeader("Content-length", params.length);

// Function to write the contents of the http response into the iFrame
httpObj.onreadystatechange = function () {
if (httpObj.readyState == 4 && httpObj.status == 200) {
parent.PushResponseContents(iFrame, httpObj, entityTypeId);
}
}
//alert(params);
// Set it, and forget it! and enjoy :)
httpObj.send(params);
}

///*********************************************
//Create the objects

//get the current record's GUID
var IFRAME_Name = "IFRAME_Accounts";
var entityName = "new_residencestatus";

var currentGUID = crmForm.ObjectId.toString();


// Embed an AF window
fetchXml = ""
+ "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"
+ " <entity name='new_residencestatus'>" //the enityname for whoch u want to create the view
+ " <attribute name='new_residencestatusid'/>" //columns u want to fetch
+ " <attribute name='new_name'/>" //same as above
+ " <order attribute='new_name' descending='false'/>" //for which column u want to order
+ " <filter type='and'>"
+ " <condition attribute='new_residencestatusid' operator='eq' value='" //pass your conditional value
+ currentGUID
+"'/>"
+ " </filter>"
+ " </entity>"
+ "</fetch>";

layoutXml = ""
+ "<grid name='resultset'>"
+ " <row name='result' id='new_residencestatusid'>" //be careful for this is , it should be same as the primarykey
+ " <cell name='new_name' width='150' />" //columns you want to show and fetched in the column list
+ " </row>"
+ "</grid>";

var sortColumnName = "new_name";
var sortDescend = "false";
var associatedViewGUID = "{93A408E0-A6E5-4149-9510-4EF333C0FC43}";
var objectTypeCode = crmForm.ObjectTypeCode.toString();

//call the master and majic method :)

EmbedAdvancedFindView(IFRAME_Name, entityName, fetchXml, layoutXml, sortColumnName, sortDescend, associatedViewGUID, objectTypeCode);

//hide the unnecessary menu bar from the grid

function hideViewMenu(){
var formview = document.all.IFRAME_Accounts;
if( formview.readyState != 'complete' )
return;

formview.contentWindow.document.getElementById("gridMenuBar").style.display = "none";
formview.contentWindow.document.getElementById("mnuBar1").parentNode.style.display = "none";
}
//hide the menubar from the view
var Form = crmForm.all.IFRAME_Accounts;
Form.attachEvent("onreadystatechange",hideViewMenu);
Sudhanshu

1 comment:

  1. will this work in CRM 2013 ? Because Advanced find ribbon is displaying on CRM Main entity form ribbon where we have placed iframe ? Any way to hide that ?

    ReplyDelete