简体   繁体   中英

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?

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

I got the solution using Query:

        //Query for the GUID of the Account using Account Name
        QueryExpression query = new QueryExpression("account");
        string[] cols = { "accountid", "name" };
        query.Criteria = new FilterExpression();
        query.Criteria.AddCondition("name", ConditionOperator.Equal, "Account Name");
        query.ColumnSet = new ColumnSet(cols);
        var account = Service.RetrieveMultiple(query);
        //Casting the reference to GUID
        Guid accountId = (Guid)account[0].Attributes["accountid"];

This will return the Guid information using the account name.

Query Expression is one option. I find it to be the least efficient. Look for those methods that are more often used:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM