Search code examples
c#dynamics-crmdynamics-crm-2015

how to create Account in dynamics crm using asp.net?


public List<AccountEntityModels> RetriveRecord()
{           
  using (OrganizationService service = new OrganizationService("MyConnectionString"))               
  {               
    QueryExpression query = new QueryExpression
    {                    
      EntityName = "account",
      ColumnSet = new ColumnSet("name")
    };
    List<AccountEntityModels> info = new List<AccountEntityModels>();
    EntityCollection accountRecord = service.RetrieveMultiple(query);

    if (accountRecord != null && accountRecord.Entities.Count > 0)                    
    {                   
      AccountEntityModels accountModel;

      for (int i = 0; i < accountRecord.Entities.Count; i++)
      {                  
        accountModel = new AccountEntityModels();
        if (accountRecord[i].Contains("name") && accountRecord[i]["name"] != null)
          accountModel.AccountName = accountRecord[i]["name"].ToString();

        info.Add(accountModel);
      }
    }
    return info;
  }
}

Solution

  • The above code is for retrieving all the CRM accounts, storing in generic List collection and returning it.

    If you want to create an account, use this code.

    Entity account = new Entity("account");
    account["name"] = "test account";
    Guid _accountId = _service.Create(account);
    

    Read more for samples explained clearly, also about _service initialization.