web
You’re offline. This is a read only version of the page.
close
Skip to main content
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)
  • Community Member Profile Picture
    on at
    RE: Simple C# code to retrieve an Entity Record

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

  • Community Member Profile Picture
    on at
    RE: Simple C# code to retrieve an Entity Record

    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

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Simple C# code to retrieve an Entity Record

    aspsolutionkirit.blogspot.in/.../update-contact-entity-from-website-to.html

  • Saddamk206 Profile Picture
    777 on at
    RE: Simple C# code to retrieve an Entity Record

    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"]  
    }

    }
  • Verified answer
    Hasibur Rahman Profile Picture
    on at
    RE: Simple C# code to retrieve an Entity Record

    Thanks  - the reply from Aileen Gusni solved the problem

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Simple C# code to retrieve an Entity Record

    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
    Aileen Gusni Profile Picture
    44,524 on at
    RE: Simple C# code to retrieve an Entity Record

    Hasibur,

    Just use this:

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

    {

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

    }

  • Verified answer
    Hasibur Rahman Profile Picture
    on at
    RE: Simple C# code to retrieve an Entity Record

    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
    RE: Simple C# code to retrieve an Entity Record

    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.

  • Suggested answer
    Mahadeo Matre Profile Picture
    17,021 on at
    RE: Simple C# code to retrieve an Entity Record

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

    you can download sdk from

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

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans