How to get Activities of each Account in Dynamics CRM 2011?

Viewed 2452

I am using this C# SDK to get data from Dynamics CRM 2011: https://msdn.microsoft.com/en-us/library/gg695803(v=crm.5).aspx

I need to read all the Accounts from it and to read the Activities associated with it (actually, only the last Closed and Open Activity).

To get the accounts I am using the following code:

var accounts = xrm.AccountSet
                .Select(acc => new
                {
                    name = acc.Name,
                    guid = acc.AccountId,
                    parent = acc.ParentAccountId,
                    number = acc.AccountNumber,
                    website = acc.WebSiteURL,
                });

This way has been suggested in this question: Retrieve list of all accounts in CRM through C#?

The problem is, I can't find a way to get the Activities. Is there a field associated with Activities? All I can find is a separate Activity Entity with it's own fields in the Dynamics CRM 2011 Solution.

Also I am new to C#.

EDIT: Thanks to Jordi, I now seem to be able to get a specific type of Activity using this:

public class AccountTask
        {
            public string account;
            public string task;
        }

var accountsAndTasks = (from t in xrm.CreateQuery<Task>() join a in xrm.CreateQuery<Account>() on t.RegardingObjectId.Id equals a.AccountId select new AccountTask {

               account = a.Name,
               task = t.OwnerId.Name.ToString()
}).ToList();

Now, is there a way to get all Activity types at once for each Account?

And, is it possible to only get the latest Open and Closed Activity for each Account?

2 Answers
Related