user2573690
user2573690

Reputation: 6013

Retrieve list of all accounts in CRM through C#?

I'm trying to retrieve all Account records from CRM 2011 so that I can cycle through them using a ForEach loop and populate a drop down. I was reading this post (Retrieving list of Entities) and am able to retrieve all accounts which meet a certain condition, but how can I retrieve all? That is every single Account record, no matter of the condition?

This is the code I was working with but I don't know which method to use after context.AccountSet. to get all accounts.

var context = new XrmServiceContext();
var parentAccount = context.AccountSet.All(snippet => snippet.ParentAccountId == "Account1");

Using context.AccountSet.All I can get all records which meet the condition, but I don't really need the condition...

Thanks for any help!

Upvotes: 3

Views: 7011

Answers (4)

Karan Chaparwal
Karan Chaparwal

Reputation: 11

Firstly you should have a connection to the CRM .Either by CRMConnection Class or by latest CRM 2016 connection method. link below. https://msdn.microsoft.com/en-in/library/jj602970.aspx

After establishing connection. You can use QueryExpression Class to query the entity and Store the entire data in a ColumnSet which is a set of columns as name suggests. This code store account names and id in crm to individual list. as simple as that kindly note the columnset takes a string parameter so the string array of columnname can be passed to it. Thanks and let me know if you have a doubt.

 QueryExpression query = new QueryExpression("account");
        query.ColumnSet.AddColumns("name","accountid");
    //  query.Criteria.AddFilter(filter1);

        EntityCollection result1 = service.RetrieveMultiple(query);
        Console.WriteLine(); Console.WriteLine("Query using Query Expression with ConditionExpression and FilterExpression");
        Console.WriteLine("---------------------------------------");

        foreach (var a in result1.Entities)
        {
            //Console.WriteLine("Name: " + a.Attributes["name"]);
            ListAccountName.Add(a.Attributes["name"].ToString());
            ListAccountId.Add(a.Attributes["accountid"].ToString());           
        }           

Upvotes: 1

Bvrce
Bvrce

Reputation: 2170

Why not just retrieve what is pertinent to the drop down?

There are many attributes that Account has that will just bloat the query.

/* If you only want name */
var accounts = context.AccountSet.Select(acc => acc.Name);
/* If you want more attributes */
var accounts = context.AccountSet
    .Select(acc => new
        {
            name = acc.Name,
            guid = acc.AccountId,
            parent = acc.ParentAccountId,
            number = acc.AccountNumber
        });
/* No need to call .ToList() on accounts, just iterate through the IQuerable */
foreach (var account in accounts)
{
    // Add account to drop down
}

Upvotes: 3

Guido Preite
Guido Preite

Reputation: 15138

AccountSet already contains all the records, this is the reason why if you do a .ToList() you get a List of Account, because you convert the AccontSet collection to a List.

Upvotes: 3

Mauro De Biasio
Mauro De Biasio

Reputation: 1146

try this:

var parentAccount =  (from c in context.CreateQuery<Account>()
                     select c);

If it's not returning the right type instead of var use

IEnumerable<Account>

Also you may need to include

using System.Linq;

Upvotes: 1

Related Questions