Tuesday, March 6, 2012

How to pass current view id to Custom web page through ribbon button in CRM 2011

In my recent project I wanted to get all the records which are in current view and do some processing and display those records in a custom web page.

My Approach

1. Get the selected View id
2. pass it to the custom web page as query string through ribbon button.
function LoadRecords() {
var selectedViewId = document.getElementsByTagName("span");

var el = getElementsByAttribute("span", "currentview");
var viewId = el[0].id;

var UserID = GetCurrentUserID();
var sUrl = '/ISV/myCustomPage.aspx?crmuserid=' + UserID + '&viewid=' + viewId;
var sWin = window.open(sUrl, '', 'height=750 ,width=900, left=75, top=50 ,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,modal=yes');

function getElementsByAttribute(strTagName, strAttributeName) {
var arrElements = document.getElementsByTagName(strTagName);

var arrReturnElements = new Array();
var oCurrent;
var oAttribute;
for (var i = 0; i < arrElements.length; i++) {
oCurrent = arrElements[i];

oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
if (typeof oAttribute == "string" && oAttribute.length > 0) {
return arrReturnElements;

3. get the selected view from SavedQuery table
public SavedQuery GetSavedQueryById(Guid viewId)
SavedQuery savedQuery = null;
if (!viewId.Equals(Guid.Empty))
savedQuery = (SavedQuery)service.Retrieve(SavedQuery.EntityLogicalName, viewId, new ColumnSet(true));
return savedQuery;

4. extract the conditions for selected view from FetchXml column

private Dictionary<string, string> getCoditionListForSelectedView(string selectedView)
Dictionary<string, string; condList = new Dictionary<string, string>();
commonFacadecs = new CommonFacade();
string fetchXML = commonFacadecs.GetSavedQueryById(viewId).FetchXml.ToString();

string formattedXML = XElement.Parse(fetchXML).ToString();

XmlDocument xmlDoc = new XmlDocument();
XmlNodeList nodeList = xmlDoc.SelectNodes("/entity");
XmlNodeList nodeList2 = xmlDoc.GetElementsByTagName("condition");

for (int i = 0; i &lt; nodeList2.Count; i++)
XmlAttributeCollection xmlAttrc = nodeList2[i].Attributes;
string name = xmlAttrc[0].Value;
string oper = xmlAttrc[1].Value;
string Value = xmlAttrc[2].Value;
condList.Add(oper, Value);
return condList;
catch (Exception exception)

5.Retrieve records according to the filter condition


Above approach will give you the all the records which are coming from the selected view
then you can do whatever the processing and display it in custom web page.

No comments:

Post a Comment

MS CRM 2011 KB Article customization Issue.

Recently I have encountered some issue while customizing Kb Article Entity. I was doing following configuration in Article form. 1. Add Ba...