Hosk CRM Dev Tip – Get EntityReference from Entity

 This is probably a tip a lot of people have been using forever but for some reason I only found this functionality a couple of weeks ago.

A lot of times in the code you need to create an EntityReference to pass to another method, use in a query expression.
I often use to use this code
EntityReference oldEntRef = new EntityReference (“account” , entity.Id);
 

this worked fine but it was a bit of extra typing and if I was using code which wasn’t early bound then I would have to look up the logicalname of the entity, which can be tricky for custom entities.

but then I found this easy method.

 
EntityReference entRef = entity.ToEntityReference();
Magic

7 thoughts on “Hosk CRM Dev Tip – Get EntityReference from Entity

  1. martin October 1, 2014 / 4:16 pm

    what’s the trick to getting it to include entref.Name?

    Like

  2. yarsh March 26, 2015 / 10:01 pm

    Upgrading CRM from 4.0, and this just solved an issue I spent 2 days trying to figure out. Kudos.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.