Search code examples
c#dynamics-crmmicrosoft-dynamicsdynamics-365dynamics-crm-365

How to retrieve a Entity without Guid in Dynamics 365 SDK?


Well, I have this command: IOrganizationService.Retrieve(String entityName, Guid ID, ColumnSet columnSet) reference: IOrganizationService

I tried the SDK Code Samples but the retrieve information is made using the Guid when this create a record, like here: Guid _accountId = orgService.Create(account);

I want to get this Guid using the name of the Account(Entity), how can I do that?


Solution

  • You have to use RetrieveMultiple method & QueryExpression to achieve it. For example, the below MSDN code example is self explained to get the contact(s) with lastname = “Brown”

    //  Query using ConditionExpression and FilterExpression
    ConditionExpression condition1 = new ConditionExpression();
    condition1.AttributeName = "lastname";
    condition1.Operator = ConditionOperator.Equal;
    condition1.Values.Add("Brown");            
    
    FilterExpression filter1 = new FilterExpression();
    filter1.Conditions.Add(condition1);
    
    QueryExpression query = new QueryExpression("contact");
    query.ColumnSet.AddColumns("firstname", "lastname");
    query.Criteria.AddFilter(filter1);
    
    EntityCollection result1 = _serviceProxy.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["firstname"] + " " + a.Attributes["lastname"]);
    }
    

    Query expressions are used in methods that retrieve more than one record, such as the IOrganizationService.RetrieveMultiple method, in messages that perform an operation on a result set specified by a query expression, such as BulkDeleteRequest and when the ID for a specific record is not known.

    Reference