Hi all,
One of the missing things in MDS is the cross-model entity copy
here is a API usage example for copying an entity from a model /version to another (or even copy an entity into a new model with a new entity name).
you can download the complete source code of my codeplex tool including this sample on : http://mdsmanager.codeplex.com
note: if source entity contains domain based attributes, and if the target model does not contain the domain based attribute parent entity, the attribute type will automatically converted to “Freeform” instead of “Domain based attribute”.
private void CopyEntity(Identifier newModelId, Identifier versionId, Entity EntityToCopy) { OperationResult or = new OperationResult(); try { Cursor.Current = Cursors.WaitCursor; Common.MDS_WS mds = new MDS_WS(); //create a new guid for the new entity (target entity must NOT have the same ID than the source entity) Guid newEntityId = Guid.NewGuid(); //create a new metadata instance Metadata md = new Metadata(); md.Entities = new Collection(); //add a new entity in this new metadata, with values and fields from source entity md.Entities.Add(new Entity() { Identifier = new ModelContextIdentifier() { ModelId = newModelId, Name = !string.IsNullOrEmpty(txtNewEntityName.Text) ? txtNewEntityName.Text : EntityToCopy.Identifier.Name, Id = newEntityId }, AuditInfo = EntityToCopy.AuditInfo, ExtensionData = EntityToCopy.ExtensionData, ExplicitHierarchies = EntityToCopy.ExplicitHierarchies, IsBase = EntityToCopy.IsBase, IsFlat = EntityToCopy.IsFlat, IsSystem = EntityToCopy.IsSystem }); //publish this new entity using (ServiceClient c = MDS_WSConnect.CreateMdsProxy()) { Metadata retMd = c.MetadataCreate(new International(), md, true, out or); //get source entity attributes md = mds.GetMetaData(new International(), ref or, (Identifier)cbModel.SelectedItem, ((CustomVersion)cbVersion.SelectedItem).Identifier, EntityToCopy.Identifier, MDS_WS.MDAction.AttributesDetails); //create a new metadata instance for attributes to copy Metadata newMD = new Metadata(); newMD.Attributes = new Collection(); //for each source entity attribute, create a new attribute containing values and fields from this source entity foreach (MetadataAttribute oldMA in md.Attributes) { MetadataAttribute ma = new MetadataAttribute() { AttributeType = oldMA.AttributeType, Identifier = new MemberTypeContextIdentifier() { //entityId is coming from the target entity EntityId = retMd.Entities.First().Identifier, //Id is coming from the target entity Id = retMd.Entities.First().Identifier.Id, //internal Id is coming from the target entity InternalId = retMd.Entities.First().Identifier.InternalId, ModelId = newModelId, MemberType = oldMA.Identifier.MemberType, Name = oldMA.Identifier.Name }, DataType = oldMA.DataType, DataTypeInformation = oldMA.DataTypeInformation, AuditInfo = oldMA.AuditInfo, ChangeTrackingGroup = oldMA.ChangeTrackingGroup, DisplayWidth = oldMA.DisplayWidth, ExtensionData = oldMA.ExtensionData, InputMaskId = oldMA.InputMaskId, IsCode = oldMA.IsCode, IsName = oldMA.IsName, IsReadOnly = oldMA.IsReadOnly, IsSystem = oldMA.IsSystem, Permission = oldMA.Permission, SortOrder = oldMA.SortOrder }; //add all destination attributes to the metadata newMD.Attributes.Add(ma); } //create the attributes metadata c.MetadataCreate(new International(), newMD, true, out or); } //if operation is done and no error, displays message if (or != null && or.Errors.Count == 0) MessageBox.Show("DONE!"); } catch (Exception ex) { //to displays errors if any StringBuilder sb = new StringBuilder(); if (or != null && or.Errors.Count > 0) { foreach (Error err in or.Errors) { sb.AppendFormat("{0}" + Environment.NewLine, err.Code + " - " + err.Description); } } if (sb.Length > 0) { throw new Exception(sb.ToString()); } else { throw ex; } } finally { Cursor.Current = Cursors.Default; } }