locked
Loop on all Accounts RRS feed

  • Question

  • I would like to write a loop using web service to go through all Accounts.

     

    CRMSDK.

    CrmService CrmService = GetCrmService("CRM"

    );

    CRMSDK.

    QueryByAttribute query = new CRMSDK.QueryByAttribute

    ();

    query.ColumnSet =

    new CRMSDK.AllColumns

    ();

    query.EntityName = CRMSDK.

    EntityName

    .account.ToString();

    query.Attributes =

    new string[] { "accountid"

    };

    query.Values =

    new string

    [] { Name };

    CRMSDK.

    BusinessEntityCollection

    AccountF = CrmService.RetrieveMultiple(query);

     

     

    Monday, November 14, 2011 1:23 PM

Answers

  • Logic will be same on CRM 4

     

    CRM 4 has different SDK, so we have to change bit

     

    use following

     

    ICrmService service = your crm 4 service;
    RetrieveMultipleRequest req = new RetrieveMultipleRequest();
    req.Query = query;
    req.ReturnDynamicEntities = true;
    RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);
    BusinessEntityCollection theAccounts = resp.BusinessEntityCollection;
    if (theAccounts.BusinessEntities.Count > 0) {
      DynamicEntity theAccount = (DynamicEntity) theAccounts.BusinessEntities[0];

    foreach(DynamicEntity theAccount = (DynamicEntity) theAccounts.BusinessEntities)

    {

    string s = theAccount.Properties["name"].ToString();

    string s1 =  (theAccount.Properties.Contains("accountnumber")) ? theAccount.Properties["accountnumber"].ToString() : "" ;

    }

     

    thanks


    Many Thanks -Bhautik Desai xRM Technologies
    • Marked as answer by Hussain Saffar Tuesday, November 15, 2011 8:23 PM
    Tuesday, November 15, 2011 9:43 AM

All replies

  •  Microsoft.Xrm.Sdk.IOrganizationService xrmService = crm.OrgService;
    
                EntityCollection allAccounts = xrmService.RetrieveMultiple(new QueryExpression("account") { ColumnSet = new ColumnSet(true) });
    
                foreach (Entity eachAccount in allAccounts.Entities)
                {
                    string sname = eachAccount["name"].ToString();
                    string strAccountNumber = (eachAccount.Attributes.Contains("accountnumber"))? eachAccount["accountnumber"].ToString(): string.Empty;
                }
    


    Many Thanks -Bhautik Desai xRM Technologies
    Monday, November 14, 2011 1:48 PM

  • Many Thanks -Bhautik Desai xRM Technologies
    Monday, November 14, 2011 1:53 PM
  • Does this work for CRM 4.0 as well???
    hussain
    Monday, November 14, 2011 7:19 PM
  • Logic will be same on CRM 4

     

    CRM 4 has different SDK, so we have to change bit

     

    use following

     

    ICrmService service = your crm 4 service;
    RetrieveMultipleRequest req = new RetrieveMultipleRequest();
    req.Query = query;
    req.ReturnDynamicEntities = true;
    RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);
    BusinessEntityCollection theAccounts = resp.BusinessEntityCollection;
    if (theAccounts.BusinessEntities.Count > 0) {
      DynamicEntity theAccount = (DynamicEntity) theAccounts.BusinessEntities[0];

    foreach(DynamicEntity theAccount = (DynamicEntity) theAccounts.BusinessEntities)

    {

    string s = theAccount.Properties["name"].ToString();

    string s1 =  (theAccount.Properties.Contains("accountnumber")) ? theAccount.Properties["accountnumber"].ToString() : "" ;

    }

     

    thanks


    Many Thanks -Bhautik Desai xRM Technologies
    • Marked as answer by Hussain Saffar Tuesday, November 15, 2011 8:23 PM
    Tuesday, November 15, 2011 9:43 AM