Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Copy an Entity without knowing the fields in C#

Posted on by 445

Hello Experts,

is there a way to copy an Entity in C# without knowing the fields?

I will make a Workflow Step where i can copy or clone every Entity in CRM.

Kind Regards,

Léon

*This post is locked for comments

  • Priyesh Profile Picture
    Priyesh 7,392 User Group Leader on at
    RE: Copy an Entity without knowing the fields in C#

    Hi All,

    I wanted to use the EntityExtensions' Clone method in order to just clone an existing record using plugin.

    Now, I'm using CoreAssemblies 8.2.0.2 and I know that Microsoft.Xrm.Client needs to be separately added. And these two assemblies together are not getting along well.

    Any idea of any other replacements of the Microsoft.Xrm.Client so that I can use EntityExtensions' Clone methods instead of cloning the records manually by looping through each field?

    Kindly advise.

    Thanks.

  • BlackBeard Profile Picture
    BlackBeard 445 on at
    RE: Copy an Entity without knowing the fields in C#

    Hello Andrew,

    thank you :) im so blind...

    Now it works fine.

    Thank you all for your help!

    Kind Regards,

    Léon

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Copy an Entity without knowing the fields in C#

    Hello,

    1. When you create record using service.Create method id of record is not set automatically but it is returned as a result of call. Replace

    service.Create(CloneSeminar);

    with line

    CloneSeminar.Id = service.Create(CloneSeminar);

    2. Looks like you're working with collection in your loop but your code modifies parent record instead of child:

                           CloneSeminar.Attributes.Remove("mrd_seminarinhaltid");

                           CloneSeminar.Attributes.Remove("mrd_ak_seminar");

                           CloneSeminar.Attributes["mrd_ak_seminar"] = CloneSeminar.Id;

    Should it be CloneSeminarinhalt instead?

    3. I assume that mrd_ak_seminar is lookup field and you populate it with Guid value. During the creation it will throw an error so you should replace line

    CloneSeminarinhalt.Attributes["mrd_ak_seminar"] = CloneSeminar.Id;

    with line

    CloneSeminarinhalt.Attributes["mrd_ak_seminar"] = CloneSeminar.ToEntityReference();

  • Suggested answer
    Preeti Sharma Profile Picture
    Preeti Sharma 2,678 on at
    RE: Copy an Entity without knowing the fields in C#

    Hi,

    You can retrieve entity metadata using retrieveentityrequest and use this metadata to create entity

    Following links may be helpful to you:

    msdn.microsoft.com/.../microsoft.xrm.sdk.messages.retrieveentityrequest.aspx

    msdn.microsoft.com/.../gg509071.aspx

    Hope this helps:)

  • BlackBeard Profile Picture
    BlackBeard 445 on at
    RE: Copy an Entity without knowing the fields in C#

    Hello,

    so now i can copy entitys but that leads to antoher problem.

    If i copy my Entity i want to copy the related Entitys to.

    But i can't assigne the related Entity to my "Master" Entity.

    The code cant find the ID from my "Master" Entity

    This is my code:

    //Record clone
                    Entity CloneSeminar = Seminar;
                    CloneSeminar.Id = Guid.Empty;
                    CloneSeminar.Attributes.Remove("mrd_seminarid");
                    CloneSeminar.Attributes.Remove("mrd_ak_seminarbeginn");
                    CloneSeminar.Attributes.Remove("mrd_ak_seminarende");
                    CloneSeminar.Attributes.Remove("mrd_ak_seminarnummer");
                    service.Create(CloneSeminar);
    
                    
    
                    tracingService.Trace("Seminar wurde erstellt");
    
                    CloneSeminar.ToEntityReference();
                    Clone.Set(executionContext, CloneSeminar);
    
    
    
    
                    // Retrive related Entities
                    QueryExpression query = new QueryExpression("mrd_seminarinhalt");
                    query.Criteria.AddCondition("mrd_ak_seminarid", ConditionOperator.Equal, rref.Id);
                                    
                    // Query
                    EntityCollection retrieveedxpan = service.RetrieveMultiple(query);
                                                    
                    if (retrieveedxpan.Entities.Count == 0)
                    {
                        // Datensätze wurden nicht gefunden
                        tracingService.Trace("Es wurden keine Seminarinhalte gefunden!");
                    }
                    else
                    {
    
                        foreach (var entity in retrieveedxpan.Entities)
                        {
    
                            entity.Attributes.Remove("mrd_seminarinhaltid");
                            
                            //CloneSeminarinhalt ermitteln
                            Entity CloneSeminarinhalt = entity;
                            CloneSeminarinhalt.Id = Guid.Empty;
                            CloneSeminar.Attributes.Remove("mrd_seminarinhaltid");
                            CloneSeminar.Attributes.Remove("mrd_ak_seminar");
                            CloneSeminar.Attributes["mrd_ak_seminar"] = CloneSeminar.Id;
                            //service.Create(CloneSeminarinhalt);
    
                            
    
                            tracingService.Trace(Convert.ToString(CloneSeminar.Attributes["mrd_seminarid"]));
                            tracingService.Trace(Convert.ToString(CloneSeminarinhalt.Id));
                            
                           
                            
                            tracingService.Trace("Seminarinhalt wurde erstellt und zugewießen");
                                                                         
                        }
                        
                                           
                    }
                }


  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Copy an Entity without knowing the fields in C#

    Hi Leon,

    Maybe this will help you out.

    community.dynamics.com/.../cloning-a-record-in-dynamics-crm

    It has a step by step procedure on creating a clone of a record.

    Hope this helps.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Copy an Entity without knowing the fields in C#

    I'm really sorry but you will have to write own extension replacing this "Clone" method with your own code.

    If you want your code work to work you will have to follow this.

  • BlackBeard Profile Picture
    BlackBeard 445 on at
    RE: Copy an Entity without knowing the fields in C#

    But i work with this .dll :D

    This is my code and the "Clone" extentsion only exist in Microsoft.Xrm.Client.

                    // Seminar ermitteln
                    Entity Seminar = service.Retrieve(rref.LogicalName, rref.Id, new ColumnSet(true));
                    EntityReference Id = Seminar.ToEntityReference();
    
                    //Record klonen
                    Entity newSeminar = EntityExtensions.Clone(Seminar, false);
                    Seminar.Id = Guid.Empty;
                    //service.Create(newRecord);
    
                    tracingService.Trace(Convert.ToString(newSeminar.LogicalName));
    
    
                    QueryExpression query = new QueryExpression("mrd_seminarinhalt");
                    query.Criteria.AddCondition("mrd_ak_seminarid", ConditionOperator.Equal, Id);
    
    
                    // Query ausfürhen
                    EntityCollection retrieveedxpan = service.RetrieveMultiple(query);
                    
                    if (retrieveedxpan.Entities.Count == 0)
                    {
                        // Datensätze wurden nicht gefunden
                        tracingService.Trace("Es wurden keine Seminarinhalte gefunden!");
                    }
                    else
                    {
                        
                        foreach (var entity in retrieveedxpan.Entities)
                        {
                            //ChildRecord ermitteln
                            Entity ChildSeminar = new Entity("mrd_seminarinhalt");
    
                            //ChildRecord klone
                            var newChildSeminar = EntityExtensions.Clone(ChildSeminar, false);
                            newChildSeminar.Id = Guid.Empty;
    
                            tracingService.Trace(Convert.ToString(newChildSeminar.LogicalName));
    
                            //service.Create(newChildRecord);
                                                   
                        }
                        
                                           
                    }


  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Copy an Entity without knowing the fields in C#

    Leon,

    Remove reference on Microsoft.Xrm.Client. This assembly is not available in the sandbox.

  • BlackBeard Profile Picture
    BlackBeard 445 on at
    RE: Copy an Entity without knowing the fields in C#

    Hello Karsten,

    my code is know ready, but if i try to execute them in my CRM than i got this error and i dont know how to fix it..

    <Message>Unexpected exception from plug-in (Execute): WorkflowActivityLibrary5.SeminareKopieren: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Xrm.Client, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.</Message>

    Kind Regards,

    Léon

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans