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)

Copy an Entity without knowing the fields in C#

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Karsten Wirl Profile Picture
    4,477 on at

    Hello Léon.

    Maybe this can help you:

    debajmecrm.com/.../clone-record-dynamics-crm-clone-method

    Kind regards,

    Karsten

  • BlackBeard Profile Picture
    445 on at

    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

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Leon,

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

  • BlackBeard Profile Picture
    445 on at

    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);
                                                   
                        }
                        
                                           
                    }


  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

  • BlackBeard Profile Picture
    445 on at

    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
    Preeti Sharma Profile Picture
    2,678 on at

    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:)

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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();

  • BlackBeard Profile Picture
    445 on at

    Hello Andrew,

    thank you :) im so blind...

    Now it works fine.

    Thank you all for your help!

    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

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