2
votes

I've done a lot of hunting and can't find a single example of how to retrieve the Connection information between an Account and Contact in Dynamics CRM 2011. Can someone point me in the right direction?

FYI, this is my usual method of retrieving data (it doesn't cover this issue and nothing I have tried has even come close to working)

        var context = new XrmServiceContext(crmService);
        var accounts = context.AccountSet.Where(a => a.Name.StartsWith("A"));

        Console.WriteLine("Accounts beginning with the letter A");

        foreach (Account account in accounts)
        {
            Console.WriteLine("{0} ({1})", account.Id, account.Name);
        }

Thanks in advanced.

2
Are you getting any errors? How is not working?glosrob
@glosrob I'm not sure you've understood the question. I'm looking to retrieve the connection information between Contacts and Accounts. The code was only given as an example to show my coding style and the fact that I am using Early Bound Entities.Chris Felstead
Indeed I misunderstood :) Glad you have sorted. I will update my answer to show how it can be done with early bound entities; The MSDN example is overly complex in my opinion.glosrob

2 Answers

5
votes

Edit: Updating answer to match requirements.

Details of connections are stored in the Connection entity set.

var context = new XrmServiceContext(crmService);
var accounts = context.AccountSet.Where(a => a.Name.StartsWith("A"));

Console.WriteLine("Accounts beginning with the letter A");

foreach (Account account in accounts)
{
    Console.WriteLine("{0} ({1})", account.Id, account.Name);
    var accToConConnections = 
    context.ConnectionSet.Where(con => con.Record1Id.Id.Equals(account.Id) &&
                                       con.Record2ObjectTypeCode.Value.Equals((int)Contact.EntityTypeCode));

   //do something with the connections if you want!
}
2
votes

Answered my own question. There is an example buried in MSDN that Google had ommitted from it's search results. MSDN example