Retrieve All Contacts(associated contacts) for All Accounts (N:N Relationship) - Console Application
-------------------------------------------------------------------------------------------------------------------------------------------------
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using System.Web;
namespace BH.Console.Utility
{
partial class Program
{
static OrganizationServiceProxy _crmProxy = null;
public static void Main(string[] args)
{
try
{
_crmProxy = CrmConnectionManager.GetInstance().GetProxy();
List<Contacts> associatedContacts = GetAllContacts(_crmProxy);
List<Accounts> acnts = GetAllAccounts(_crmProxy, associatedContacts);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
================================================================================
// ************** Get All Accounts with associated Contacts. ************************* ================================================================================
public static List<Accounts> GetAllAccounts(OrganizationServiceProxy _service, List<Contacts> associatedContacts )
{
List<Accounts> lstAcnts = new List<Accounts>();
QueryExpression query = new QueryExpression("new_account");
query.ColumnSet = new ColumnSet("new_accountid", "new_name");
FilterExpression filter = new FilterExpression(LogicalOperator.And);
filter.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));
query.Criteria.AddFilter(filter);
EntityCollection collAcnts = _service.RetrieveMultiple(query);
foreach (Entity acnt in collAcnts.Entities)
{
Account acnts= new Account();
acnts.AccountName = acnt.GetAttributeValue<string>("new_name");
List<Contacts> associatedContacts = associatedContacts.FindAll(g => g.acntId == acnt.Id);
acnts.associatedContacts= (string.Join(",", associatedContact.Select(g => Convert.ToString(g.contactName)).ToArray()));
lstAcnts.Add(acnts);
}
return lstAcnts;
}
================================================================================
// ****************** Get all associated Contacts********************************* ================================================================================
public static List<Contacts> GetAllContacts(OrganizationServiceProxy _service)
{
List<Contacts> recordContacts = new List<Contacts>();
QueryExpression query = new QueryExpression("new_conatct");
query.ColumnSet = new ColumnSet("new_conatctid", "new_name");
LinkEntity contactToAccount = new LinkEntity("new_contact", "new_account_new_conatct", "new_conatctid", "new_contactid", JoinOperator.Inner);
contactToAccount.EntityAlias = "ContactAccount"; contactToAccount .Columns.AddColumn("new_accountid");
query.LinkEntities.Add(contactToAccount);
EntityCollection collAcc = _service.RetrieveMultiple(query);
foreach (Entity e in collAcc.Entities)
{
Contact contacts = new Conatct();
contacts.conatctid = e.Id;
contacts.conatctName = e["new_name"].ToString();
contacts.accountId = (Guid)((AliasedValue)e["AccountContact.new_accountid"]).Value;
contacts.Add(contacts);
}
return recordContacts;
}
===============================================================================
// **************** Get Accounts with name "Ben" **************************** ===============================================================================
public static List<Accounts> GetAllAccounts(OrganizationServiceProxy _service)
{
List<Accounts> lstAcnts1 = new List<Accounts>();
QueryExpression query1 = new QueryExpression("new_account");
query1.ColumnSet = new ColumnSet("new_name");
query1.Criteria.AddCondition("new_name", ConditionOperator.Like, "%" + "Ben" + "%");
EntityCollection results = _service.RetrieveMultiple(query1);
foreach (Entity wp in results.Entities)
{
Accounts acnts1 = new Accounts();
acnts1.accountName = wp.GetAttributeValue<string>("new_name");
lstAcnts1.Add(acnts1);
}
return lstAcnts1;
}
}
public class Conatct
{
public Guid contactid{ get; set; }
public string contactName { get; set; }
public Guid accountId { get; set; }
}
public class Account
{
public Guid accountId { get; set; }
public string AccountName { get; set; }
public string AssociatedContacts { get; set; }
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using System.Web;
namespace BH.Console.Utility
{
partial class Program
{
static OrganizationServiceProxy _crmProxy = null;
public static void Main(string[] args)
{
try
{
_crmProxy = CrmConnectionManager.GetInstance().GetProxy();
List<Contacts> associatedContacts = GetAllContacts(_crmProxy);
List<Accounts> acnts = GetAllAccounts(_crmProxy, associatedContacts);
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
}
================================================================================
// ************** Get All Accounts with associated Contacts. ************************* ================================================================================
public static List<Accounts> GetAllAccounts(OrganizationServiceProxy _service, List<Contacts> associatedContacts )
{
List<Accounts> lstAcnts = new List<Accounts>();
QueryExpression query = new QueryExpression("new_account");
query.ColumnSet = new ColumnSet("new_accountid", "new_name");
FilterExpression filter = new FilterExpression(LogicalOperator.And);
filter.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));
query.Criteria.AddFilter(filter);
EntityCollection collAcnts = _service.RetrieveMultiple(query);
foreach (Entity acnt in collAcnts.Entities)
{
Account acnts= new Account();
acnts.AccountName = acnt.GetAttributeValue<string>("new_name");
List<Contacts> associatedContacts = associatedContacts.FindAll(g => g.acntId == acnt.Id);
acnts.associatedContacts= (string.Join(",", associatedContact.Select(g => Convert.ToString(g.contactName)).ToArray()));
lstAcnts.Add(acnts);
}
return lstAcnts;
}
================================================================================
// ****************** Get all associated Contacts********************************* ================================================================================
public static List<Contacts> GetAllContacts(OrganizationServiceProxy _service)
{
List<Contacts> recordContacts = new List<Contacts>();
QueryExpression query = new QueryExpression("new_conatct");
query.ColumnSet = new ColumnSet("new_conatctid", "new_name");
LinkEntity contactToAccount = new LinkEntity("new_contact", "new_account_new_conatct", "new_conatctid", "new_contactid", JoinOperator.Inner);
contactToAccount.EntityAlias = "ContactAccount"; contactToAccount .Columns.AddColumn("new_accountid");
query.LinkEntities.Add(contactToAccount);
EntityCollection collAcc = _service.RetrieveMultiple(query);
foreach (Entity e in collAcc.Entities)
{
Contact contacts = new Conatct();
contacts.conatctid = e.Id;
contacts.conatctName = e["new_name"].ToString();
contacts.accountId = (Guid)((AliasedValue)e["AccountContact.new_accountid"]).Value;
contacts.Add(contacts);
}
return recordContacts;
}
===============================================================================
// **************** Get Accounts with name "Ben" **************************** ===============================================================================
public static List<Accounts> GetAllAccounts(OrganizationServiceProxy _service)
{
List<Accounts> lstAcnts1 = new List<Accounts>();
QueryExpression query1 = new QueryExpression("new_account");
query1.ColumnSet = new ColumnSet("new_name");
query1.Criteria.AddCondition("new_name", ConditionOperator.Like, "%" + "Ben" + "%");
EntityCollection results = _service.RetrieveMultiple(query1);
foreach (Entity wp in results.Entities)
{
Accounts acnts1 = new Accounts();
acnts1.accountName = wp.GetAttributeValue<string>("new_name");
lstAcnts1.Add(acnts1);
}
return lstAcnts1;
}
}
public class Conatct
{
public Guid contactid{ get; set; }
public string contactName { get; set; }
public Guid accountId { get; set; }
}
public class Account
{
public Guid accountId { get; set; }
public string AccountName { get; set; }
public string AssociatedContacts { get; set; }
}
}