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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Simple C# code to retrieve an Entity Record

(0) ShareShare
ReportReport
Posted on by

Hello,

I am looking for the simplest C# sample code to retrieve an entity record and value of some fields by passing the "ID" of the entity record 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mamatha Swamy Profile Picture
    5,426 on at
  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at

    Download crm sdk..it has sample code to retrieve, and for other operations.

    you can download sdk from

    www.microsoft.com/.../details.aspx

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hi Hasibur:

    As per your requirement, a simple sample code:

    private Entity RetrieveEntityById(IOrganizationService service, string strEntityLogicalName, Guid guidEntityId)

           {

               Entity RetrievedEntityById= service.Retrieve(strEntityLogicalname, guidEntityId, newColumnSet(true)); //it will retrieve the all attrributes

               return RetrievedEntityById;

           }

    How to call?

    Entity entity = RetrieveEntityById(service, "account", guidAccountId);

    how to get the attributes?

    String strAccountName = string.Empty;

    if(entity.Attributes.Contains("name"))

    {

        strAccountName = entity.Attributes["name"]

    }

    msdn.microsoft.com/.../gg328416.aspx

    Hope this helps.

    Thanks.

  • Verified answer
    Hasibur Rahman Profile Picture
    on at

    Aileen Gusni  - thanks for your reply. How would a go about if I want to retrieve them by a custom field (i.e. - ticketnumber (int))

    Thanks

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Hasibur,

    Just use this:

    if(entity.Attributes.Contains("new_ticketnumber"))

    {

       int intTickerNumber= Convert.ToInt32(entity.Attributes["new_ticketnumber"].ToString());

    }

  • Suggested answer
    Community Member Profile Picture
    on at

    Easy way,

    Install this Nuget package

    https://www.nuget.org/packages/CRMConnection/1.0.2

    Here is the sample to connect and use the service object into your own way.

    static void Main(string[] args)
            {
                Microsoft.Xrm.Sdk.IOrganizationService _service = null;
                Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy _serviceProxy;
                ServerConnection serverConnect = new ServerConnection();
    
               // Final parameter of this method is your CRM version. like crm4, crm5 etc. If you pass Blank value it will use crm default.
                ServerConnection.Configuration config = serverConnect.GetServerConfiguration("yourusername", "yourpassword","crm");
                _serviceProxy = ServerConnection.GetOrganizationProxy(config);
    
                // This statement is required to enable early-bound type support.
                _serviceProxy.EnableProxyTypes();
    
                _service = (Microsoft.Xrm.Sdk.IOrganizationService)_serviceProxy;
            }


  • Verified answer
    Hasibur Rahman Profile Picture
    on at

    Thanks  - the reply from Aileen Gusni solved the problem

  • Saddamk206 Profile Picture
    777 on at

    Retrieve a record of an entity:- 

     

            public Entity RetrieveEntityById(IOrganizationService service, string EntityLogicalname, Guid guidEntityId)
            {
                Entity entityRecord = service.Retrieve(EntityLogicalname, Entityguid, new ColumnSet(true));
                return entityRecord;
            }

     public void getEntityRecord()
    {
                 Entity entity = RetrieveEntityById(service, "account", guidAccountId); 
    if(entity.Attributes.Contains("name")) 
    {
      string  strAccountName = entity.Attributes["name"]  
    }

    }
  • Suggested answer
    Community Member Profile Picture
    on at
  • Community Member Profile Picture
    on at

    Can you help me with the code for My Active contacts under Contact entity, I want to retrieve all the records and also update them

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans