internal static KeyValuePair <EntityRelationshipInfo, EntityRelationshipInfo> GetFromMetaData(string originLogicalName, ManyToManyRelationshipMetadata metaData)
        {
            EntityRelationshipInfo entityRelationship1 = new EntityRelationshipInfo
            {
                IntersectAttribute     = metaData.Entity1IntersectAttribute,
                LogicalName            = metaData.Entity1LogicalName,
                NavigationPropertyName = metaData.Entity1NavigationPropertyName,
            };

            EntityRelationshipInfo entityRelationship2 = new EntityRelationshipInfo
            {
                IntersectAttribute     = metaData.Entity2IntersectAttribute,
                LogicalName            = metaData.Entity2LogicalName,
                NavigationPropertyName = metaData.Entity2NavigationPropertyName,
            };

            if (metaData.Entity1LogicalName == originLogicalName)
            {
                return(new KeyValuePair <EntityRelationshipInfo, EntityRelationshipInfo>(entityRelationship1, entityRelationship2));
            }
            else
            {
                return(new KeyValuePair <EntityRelationshipInfo, EntityRelationshipInfo>(entityRelationship2, entityRelationship1));
            }
        }
Esempio n. 2
0
        public List <RelatedEntityType> ReadNNRelationship <RelatedEntityType>(string relationshipName, Entity currentEntity, Func <Entity, RelatedEntityType> crmEntityCreater, params string[] relatedColumns)
        {
            List <RelatedEntityType> relatedObjects = new List <RelatedEntityType>();

            ColumnSet columns = new ColumnSet(relatedColumns);

            RetrieveRelationshipRequest request = new RetrieveRelationshipRequest()
            {
                Name = relationshipName,
            };

            RetrieveRelationshipResponse response = (RetrieveRelationshipResponse)Connection.Service.Execute(request);

            ManyToManyRelationshipMetadata metaData = (ManyToManyRelationshipMetadata)response.Results["RelationshipMetadata"];

            KeyValuePair <EntityRelationshipInfo, EntityRelationshipInfo> relationshipInfo = EntityRelationshipInfo.GetFromMetaData(currentEntity.LogicalName, metaData);

            List <Guid> relatedIds = GetRelatedIds(currentEntity.Id, metaData.IntersectEntityName, relationshipInfo.Key.IntersectAttribute, relationshipInfo.Value.IntersectAttribute);

            foreach (Guid entityId in relatedIds)
            {
                Entity            relatedEntity = Connection.Service.Retrieve(relationshipInfo.Value.LogicalName, entityId, columns);
                RelatedEntityType relatedObject = crmEntityCreater(relatedEntity);
                relatedObjects.Add(relatedObject);
            }

            return(relatedObjects);
        }