Search code examples
c#asp.netdynamics-crm

Retrieve/fetch records from DYNAMICS 365 crm using C# code


  • Problem 1:

    I am new to MICROSOFT DYNAMICS CRM 365. I have few tables with my CRM like(Account,Customer).I want to fetch all data from table account.

Below is my sample code for connection:(not sure this is correct or not but getting output message that i am connected to CRM)

public void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)  
    {  
        try  
        {  
            ClientCredentials credentials = new ClientCredentials();  
            credentials.UserName.UserName = UserName;  
            credentials.UserName.Password = Password;  
            Uri serviceUri = new Uri(SoapOrgServiceUri);  
            OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);  
            proxy.EnableProxyTypes();  
            _services = (IOrganizationService)proxy;  
            Response.Write("Connected to CRM \n");  
}  

I need all data to be retrieved on button click event .
Output should be: result of "select * from ABC";

  • Problem 2:

if possible please suggest how to fetch records using given column name.
Output should be: result of "select * from ABC where ColumnName="test";


Solution

  • Fetching all entities list into List

        var allEntities = **GetEntities(_service);**
        foreach (var Entity in allEntities)
        {
            ddlEntityName.Items.Add(Entity.LogicalName);
        }
    

    //Function with single parameter (oganizationservice as a parameter)

    //This will fetch Table/Entity Name Like ACCOUNT,CONTACT from Microsoft Dynamics CRM

    public Microsoft.Xrm.Sdk.Metadata.EntityMetadata[] GetEntities(IOrganizationService organizationService)
        {
            Dictionary<string, string> attributesData = new Dictionary<string, string>();
            RetrieveAllEntitiesRequest metaDataRequest = new RetrieveAllEntitiesRequest();
            RetrieveAllEntitiesResponse metaDataResponse = new RetrieveAllEntitiesResponse();
            metaDataRequest.EntityFilters = EntityFilters.Entity;
    
            XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas();
            myReaderQuotas.MaxNameTableCharCount = 2147483647;
    
            // Execute the request.
    
            metaDataResponse = (RetrieveAllEntitiesResponse)organizationService.Execute(metaDataRequest);
    
            var entities = metaDataResponse.EntityMetadata;
    
            return entities.OrderBy(x => x.LogicalName).ToArray();//to arrange in ascending order
        }