{knowhow} Clone an entity record programmatically in Microsoft Dynamics CRM using Clone method

Recently in my project the customer came up with a requirement where they needed to clone a record programmatically. They wanted a common API which can be used to clone records of any entities.

Normally the requirement would be like there would be a button on the entity record form called ‘Copy Record’ or ‘Clone Record’ and then once you click that, a new form would open up with the data copied from the parent record. I have explained the same stuff in the blog post – http://debajmecrm.com/2015/01/27/how-to-using-xrm-utility-openentityform-to-clone-all-fields-of-one-record-to-another-in-dynamics-crm/.

However this time, the requirement was a bit different. This time the client needed an API which can clone a single entity or a collection of entities. The function would accept either an Entity as parameter or EntityCollection as parameter.

I was thinking if Dynamics CRM provides something like MemberwiseClone method of C#. Could not find initially anything like that initially and decided to write something of my own. But before that, I decided to go back to the CRM programmer’s bible – CRM SDK. And guess what, CRM indeed has a clone method which does exactly the same stuff I was looking.

Below is the screenshot of the method definition from the SDK.


As you can see, the method is present in the Microsoft.Xrm.Client.dll.

With the help of this, you could do something like the below to clone a record.


var customer = proxy.Retrieve("account", Guid.Parse("2767215B-C2D2-E111-B664-005056AB021D"), "Columns which you want to copy");
var newCustomer = EntityExtensions.Clone(customer, true); // You can also write customer.Clone(true);
newCustomer.Id = Guid.Empty;

A small note here. After you clone from the parent record and create a new customer, please remember to remove the primary key fields. Otherwise you would get an error like – Cannot insert duplicate key.

And voila! It worked.

A little bit of more searching and I came across this wonderful blog post. http://inogic.com/blog/2014/08/clone-records-in-dynamics-crm/. A greet post indeed like all other posts from Inogic. Wish I would have searched that before. Smile. It contains multiple ways to clone the record in Dynamics CRM.


P.S – The stuff I have mentioned here is only available in CRM on-premise. The Inogic blog gives you a very informative pointer to achieve this across all environments.


Hope this helps!


5 thoughts on “{knowhow} Clone an entity record programmatically in Microsoft Dynamics CRM using Clone method”

    1. Hi Kamran,
      I believe what you mean by secondaries is related entities. The boolean parameter in the Clone Method is actually an indicator as to whether you want to clone the related entities as well.
      However the parent record from which you are cloning, should also have the related records fetched within itself.
      For e.g – If you retrieve the account entity record and the associated contacts also and then try to clone this record by passing true in the clone method, it should clone the related records as well.
      I haven’t tried that though. Please check and let me know!

  1. Very Nice blog ,
    but Microsoft.Xrm.Client does not exist any more at CRM 2016.
    Do You know what is its replaceable?

    1. Hi,

      Thanks for reading my blog.

      You are right. Microsoft has made a decision to move everything to Microsoft.XrmTooling. However you can still use the 2015 SDK dll for 2016 projects.
      Also you can look forward to nuget packages and downloads in visual studio. it is still available there.


Leave a Reply