Treeview Display – Data From CRM

Here in this article I have posted the code snippet for displaying the QueueView(of CRM) in an Asp.Net Web Page.

Step 1 :

Fetching the data from the CRM

const String fetchXml = @"<fetch mapping=""logical"" count=""50"" version=""1.0"">
<entity name=""queue""><attribute name=""name"" /><attribute name=""primaryuserid"" />
<attribute name=""queueid"" /><attribute name=""queuetypecode"" />
<filter type=""or""><condition attribute=""primaryuserid"" operator=""eq-userid"" />
<condition attribute=""queuetypecode"" operator=""eq"" value=""1"" /></filter>
<link-entity name=""queueitem"" from=""queueid"" to=""queueid"">
<link-entity name=""incident"" from=""incidentid"" to=""objectid"">
<attribute name=""incidentid"" /><attribute name=""subjectid"" />
<attribute name=""title"" />
<link-entity name=""subject"" from=""subjectid"" to=""subjectid"">
<attribute name=""subjectid"" />
<attribute name=""title"" />
</link-entity></link-entity></link-entity></entity></fetch>";
var fetchResponse = string.Empty;
var crmService = new CrmService();
try
{
crmService = new CrmService();
crmService.Url = "http://<servername>/mscrmservices/2007/crmservice.asmx";
crmService.Credentials = CredentialCache.DefaultNetworkCredentials;
var token = new CrmAuthenticationToken {AuthenticationType = 0, OrganizationName = "<organizationname>"};
crmService.CrmAuthenticationTokenValue = token;
crmService.UnsafeAuthenticatedConnectionSharing = true;
 

Step 2 :

QueueView as in CRM with “My Work” and Public “Queue”

#region QueueView
var request = new WhoAmIRequest();
var user = (WhoAmIResponse) crmService.Execute(request);
var condition = new ConditionExpression {AttributeName = "queuetypecode", Operator = ConditionOperator.In, Values = new string[] {"2", "3"}};
/// Set the condition for the retrieval
var conditionUser = new ConditionExpression {AttributeName = "primaryuserid", Operator = ConditionOperator.In, Values = new string[] {user.UserId.ToString()}};
// Set the condition for the retrieval
// Create the FilterExpression.
var filter = new FilterExpression {FilterOperator = LogicalOperator.And};
// Set the properties of the filter.
filter.Conditions.Add(conditionUser);
filter.Conditions.Add(condition);

var queryQueue = new QueryExpression{ EntityName = EntityName.queue.ToString(), ColumnSet = new AllColumns(), Criteria = filter};
BusinessEntityCollection queueFilter = crmService.RetrieveMultiple(queryQueue);
// Define your Dataset and DataTable
var dataSetQueue = new DataSet();
var userQueues = new DataTable();
//// Set Column headers for the DataTable
userQueues.Columns.Add("QueueName");
userQueues.Columns.Add("QueueID");
////Iterate the BusinessEntityColleciton
foreach (BusinessEntity queues in queueFilter.BusinessEntities)
 {var ic = (queue) queues;
DataRow dr = userQueues.NewRow();
dr["QueueName"] = ic.name;
 dr["QueueID"] = "{" + ic.queueid.Value + "}";
userQueues.Rows.Add(dr);                 }
dataSetQueue.Tables.Add(userQueues);
#endregion

#region FetchXml
fetchResponse = crmService.Fetch(fetchXml);
var doc = new XmlDocument();
 doc.LoadXml(fetchResponse);
 var docReader = new XmlNodeReader(doc);
//read into dataset
var dataSet = new DataSet();
dataSet.ReadXml(docReader);

var dt = dataSet.Tables[1];
DataTable uniqueQueues = dt.DefaultView.ToTable(true, "queueid", "name");
uniqueQueues.Columns.Add("QueueCaseCount");
var uniqueQueuesSubjects = dt.DefaultView.ToTable(true, "queueid", "subjectid.subjectid");
uniqueQueuesSubjects.Columns.Add("CaseCount");
uniqueQueuesSubjects.Columns.Add("Subject Title");

foreach (DataRow dr in uniqueQueues.Rows)
{
int cc = 0;
for (var j = 0; j < dt.Rows.Count; j++)
{
if (dr[0].ToString() == dt.Rows[j].ItemArray[2].ToString())
{ cc++;}}

dr[2] = cc;}
foreach (DataRow dr in uniqueQueuesSubjects.Rows)
{
var cc = 0;
for (var j = 0; j < dt.Rows.Count; j++)
 {
if (dr[0].ToString() == dt.Rows[j].ItemArray[2].ToString() && dr[1].ToString() == dt.Rows[j].ItemArray[5].ToString())
{
 cc++;
dr[3] = dt.Rows[j].ItemArray[6]; //subject Title }}

dr[2] = cc;}
var assignedCnt = 0;
var wipCnt = 0;
for (var i = 0; i < uniqueQueues.Rows.Count; i++)
 {
if (uniqueQueues.Rows[i].ItemArray[1].ToString().Contains("private queue"))
{
uniqueQueues.Rows[i][1] = "Assigned";
assignedCnt++;
}

if (uniqueQueues.Rows[i].ItemArray[1].ToString().Contains("WIP Bin queue"))
 {
uniqueQueues.Rows[i][1] = "In Progress";
wipCnt++; }}

if (assignedCnt == 0)
 {uniqueQueues.Rows.Add(userQueues.Rows[0].ItemArray[1], "Assigned", "0"); }

if (wipCnt == 0) {
uniqueQueues.Rows.Add(userQueues.Rows[1].ItemArray[1], "In Progress", "0"); }

var asg = 0;
var prg = 0;

for (var i = 0; i < uniqueQueuesSubjects.Rows.Count; i++)
 {
if (userQueues.Rows[0].ItemArray[1].ToString().ToUpper() == uniqueQueuesSubjects.Rows[i].ItemArray[0].ToString())
{ asg++;}
if (userQueues.Rows[1].ItemArray[1].ToString().ToUpper() ==  uniqueQueuesSubjects.Rows[i].ItemArray[0].ToString())
{ prg++;}}
if (asg == 0){
uniqueQueuesSubjects.Rows.Add(userQueues.Rows[0].ItemArray[1], string.Empty, 0, "No Subjects");}
if (prg == 0)
{uniqueQueuesSubjects.Rows.Add(userQueues.Rows[1].ItemArray[1], string.Empty, 0, "No Subjects");}
uniqueQueuesSubjects = uniqueQueuesSubjects.DefaultView.ToTable(true, "queueid", "subjectid.subjectid", "CaseCount", "Subject Title");
var myWorkQueue = new DataTable("myWorkQueue");
myWorkQueue.Columns.Add("queueid");
myWorkQueue.Columns.Add("name");
myWorkQueue.Columns.Add("QueueCaseCount");
var myWorkSubject = new DataTable("myWorkSubject");
myWorkSubject.Columns.Add("queueid");
myWorkSubject.Columns.Add("subjectid.subjectid");
myWorkSubject.Columns.Add("CaseCount");
myWorkSubject.Columns.Add("Subject Title");
for (var i = 0; i < uniqueQueues.Rows.Count; i++)
{if (uniqueQueues.Rows[i].ItemArray[1].ToString() == "Assigned")
{myWorkQueue.Rows.Add(uniqueQueues.Rows[i].ItemArray[0], uniqueQueues.Rows[i].ItemArray[1],uniqueQueues.Rows[i].ItemArray[2]);
uniqueQueues.Rows.RemoveAt(i);}
if (uniqueQueues.Rows[i].ItemArray[1].ToString() == "In Progress")
{ myWorkQueue.Rows.Add(uniqueQueues.Rows[i].ItemArray[0], uniqueQueues.Rows[i].ItemArray[1], uniqueQueues.Rows[i].ItemArray[2]);
uniqueQueues.Rows.RemoveAt(i); }}
for (int i = 0; i < uniqueQueuesSubjects.Rows.Count; i++)  {
if (uniqueQueuesSubjects.Rows[i].ItemArray[0].ToString() == myWorkQueue.Rows[0].ItemArray[0].ToString() || uniqueQueuesSubjects.Rows[i].ItemArray[0].ToString() == myWorkQueue.Rows[1].ItemArray[0].ToString())
{myWorkSubject.Rows.Add(uniqueQueuesSubjects.Rows[i].ItemArray[0], uniqueQueuesSubjects.Rows[i].ItemArray[1], uniqueQueuesSubjects.Rows[i].ItemArray[2], uniqueQueuesSubjects.Rows[i].ItemArray[3]);
uniqueQueuesSubjects.Rows.RemoveAt(i);
i--;}}
myWorkQueue.DefaultView.Sort = "QueueCaseCount DESC";
DataTable dvmyWorkQueue = myWorkQueue.DefaultView.ToTable();
myWorkSubject.DefaultView.Sort = "CaseCount DESC";
DataTable dvmyWorkSubject = myWorkSubject.DefaultView.ToTable();
uniqueQueues.DefaultView.Sort = "QueueCaseCount DESC";
DataTable dvuniqueQueues = uniqueQueues.DefaultView.ToTable();
uniqueQueuesSubjects.DefaultView.Sort = "CaseCount DESC";
DataTable dvuniqueQueuesSubjects = uniqueQueuesSubjects.DefaultView.ToTable();

#region Treeview For My Work

var dsMyWork = new DataSet();
dsMyWork.Tables.Add(dvmyWorkQueue);
dsMyWork.Tables[0].TableName = "myWorkQueue";
dsMyWork.Tables.Add(dvmyWorkSubject);
dsMyWork.Tables[1].TableName = "myWorkSubject";

dsMyWork.Relations.Add("Children", dvmyWorkQueue.Columns[0], dvmyWorkSubject.Columns[0]);
//define parent child relation in dataset

if (dsMyWork.Tables[0].Rows.Count > 0)
{trvMyWork.Nodes.Clear();
var myWorkNode = new TreeNode("My Work") {ImageUrl = @"Images\QueueuImage.png"};

trvMyWork.Nodes.Add(myWorkNode);
foreach (DataRow masterRow in dsMyWork.Tables[0].Rows)
{ var masterNode = new TreeNode((String) masterRow[1], Convert.ToString(masterRow[0])){Text = masterRow[1].ToString() + "(" + masterRow[2].ToString() + ")", ImageUrl = @"Images\MyWorkQueue.png"};
myWorkNode.ChildNodes.Add(masterNode);
foreach (var childRow in masterRow.GetChildRows("Children")){
var childNode = new TreeNode((String) childRow[3], Convert.ToString(childRow[3]));
if (childRow[3].ToString() == "0"){
childNode.Text = "No Records in Queue";}
else{ childNode.Text = string.Format("{0} ({1})", childRow[3].ToString(), childRow[2].ToString());
childNode.ImageUrl = @"Images\Subject.png";}
masterNode.ChildNodes.Add(childNode);}  }
trvMyWork.ExpandAll();}
#endregion

#region Treeview For Queues
var ds = new DataSet();
ds.Tables.Add(dvuniqueQueues);
ds.Tables[0].TableName = "dvuniqueQueues";
ds.Tables.Add(dvuniqueQueuesSubjects);
ds.Tables[1].TableName = "dvuniqueQueuesSubjects";
ds.Relations.Add("Children", dvuniqueQueues.Columns[0], dvuniqueQueuesSubjects.Columns[0]);
//define parent child relation in dataset
if (ds.Tables[0].Rows.Count > 0) {
trv.Nodes.Clear();
foreach (DataRow masterRow in ds.Tables[0].Rows) {
var masterNode = new TreeNode((String) masterRow[1], Convert.ToString(masterRow[0])){
Text = masterRow[1].ToString() + "(" + masterRow[2] + ")",ImageUrl = @"Images\QueueuImage.png"};
trv.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children")) {
var childNode = new TreeNode((String) childRow[3], Convert.ToString(childRow[3]));
if (childRow[3].ToString() == "0"){
childNode.Text = @"No Records in queue"; }
else  {
childNode.Text = string.Format("{0} ({1})", childRow[3].ToString(), childRow[2].ToString());
childNode.ImageUrl = @"Images\Subject.png"; }
masterNode.ChildNodes.Add(childNode); } }
trv.ExpandAll(); }
#endregion
}
catch (SoapException ex)
 {
var errMSg = ex.Detail.InnerXml;
}
#endregion

This will help you create the queueview similar to the one in CRM and this can be used as custom pages in CRM.
Thanks 🙂