public static List <Dynamics365Relationship> GetRelationships(Dynamics365Entity entity, Dynamics365Connection connection) { ConnectionCache cache = new ConnectionCache(connection); string cacheKey = string.Format("GetRelationships:{0}", entity.LogicalName); List <Dynamics365Relationship> relationships = (List <Dynamics365Relationship>)cache[cacheKey]; if (relationships == null) { relationships = new List <Dynamics365Relationship>(); RetrieveEntityRequest request = new RetrieveEntityRequest() { LogicalName = entity.LogicalName, EntityFilters = EntityFilters.Relationships, RetrieveAsIfPublished = false }; using (OrganizationServiceProxy proxy = connection.OrganizationServiceProxy) { RetrieveEntityResponse response = (RetrieveEntityResponse)proxy.Execute(request); foreach (ManyToManyRelationshipMetadata metadata in response.EntityMetadata.ManyToManyRelationships) { Dynamics365Relationship relationship = new Dynamics365Relationship() { SchemaName = metadata.SchemaName, EntityLogicalName = metadata.Entity1LogicalName == entity.LogicalName ? metadata.Entity1LogicalName : metadata.Entity2LogicalName, RelatedEntityLogicalName = metadata.Entity1LogicalName == entity.LogicalName ? metadata.Entity2LogicalName : metadata.Entity1LogicalName }; relationships.Add(relationship); } foreach (OneToManyRelationshipMetadata metadata in response.EntityMetadata.ManyToOneRelationships) { Dynamics365Relationship relationship = new Dynamics365Relationship() { SchemaName = metadata.SchemaName, EntityLogicalName = metadata.ReferencedEntity == entity.LogicalName ? metadata.ReferencedEntity : metadata.ReferencingEntity, RelatedEntityLogicalName = metadata.ReferencedEntity == entity.LogicalName ? metadata.ReferencingEntity : metadata.ReferencedEntity }; relationships.Add(relationship); } foreach (OneToManyRelationshipMetadata metadata in response.EntityMetadata.OneToManyRelationships) { Dynamics365Relationship relationship = new Dynamics365Relationship() { SchemaName = metadata.SchemaName, EntityLogicalName = metadata.ReferencedEntity == entity.LogicalName ? metadata.ReferencedEntity : metadata.ReferencingEntity, RelatedEntityLogicalName = metadata.ReferencedEntity == entity.LogicalName ? metadata.ReferencingEntity : metadata.ReferencedEntity }; relationships.Add(relationship); } } cache[cacheKey] = relationships; } return(relationships); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (value != default(object)) { Dynamics365Relationship relationship = (Dynamics365Relationship)value; return(relationship.SchemaName); } return(null); }
/// <summary> /// Gets all relationships for the specified entity. /// </summary> /// <returns>The relationships.</returns> public List <Dynamics365Relationship> GetRelationships() { List <Dynamics365Relationship> relationships = new List <Dynamics365Relationship>(); try { relationships.AddRange(Dynamics365Relationship.GetRelationships(Entity, Connection).Where(r => r.RelatedEntityLogicalName == AssociatedEntity.LogicalName).ToList()); } catch { } return(relationships); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (!string.IsNullOrEmpty((string)value)) { string relationshipSchemaName = (string)value; Dynamics365RelationshipOperation operation = (Dynamics365RelationshipOperation)context.Instance; List <Dynamics365Relationship> relationships = Dynamics365Relationship.GetRelationships(operation.Entity, operation.Connection); Dynamics365Relationship relationship = Dynamics365Relationship.GetRelationships(operation.Entity, operation.Connection).FirstOrDefault(r => r.SchemaName == relationshipSchemaName); return(relationship); } return(null); }