web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieving Guid of a lookup field

(0) ShareShare
ReportReport
Posted on by 60

I am looking for a c# code to get the GUID of a lookup field.  This should be a generic code to accommodate any entity. 

Following is the scenario.

Need to create a lead  from a external web form. In web form  Nationality is provided as Text.   This text needs to be translated to   GUID  to add lookup field in the lead form.  

What I need a function which  receive parameter Lookup table, lookupGUIDcolumnName, and LookupTextColumnName.

eg: Lookup table = crm_nationality

     lookupGUIDcolumnName= crm_nationalityId

     LookupTextColumnName = crm_nationalityText

Any help is greatly appreciated. 

                   

*This post is locked for comments

I have the same question (0)
  • Verified answer
    David Jennaway Profile Picture
    14,065 on at

    You can do this using QueryByAttribute to get the GUID of a record based on matching on a field value. It would look something like this:

    You'll then need to check how many records are returned - ideally it would be one, but you should handle the scenarios of 0 records (no match), or multiple records (duplicates). If duplicates are possible, you can use the Orders property to specify the order

    QueryByAttribute qa = new QueryByAttribute(LookupTable);
    qa.ColumnSet = new ColumnSet(LookupTextColumnName, LookupGUIDcolumnName);
    //  Attribute to query.
    qa.Attributes.AddRange(LookupTextColumnName);
    //  Value of queried attribute to return.
    qa.Values.AddRange(LookupTextValue);
    EntityCollection ec = _service.RetrieveMultiple(qa);


  • jma728 Profile Picture
    60 on at

    Hi David,

    The code you provided is worked well for me.  Thanks a lot.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans