public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { var retrieveRequest = request as RetrieveRelationshipRequest; if (retrieveRequest == null) { throw new Exception("Only RetrieveRelationshipRequest can be processed!"); } var service = ctx.GetFakedOrganizationService(); var fakeRelationShip = ctx.GetRelationship(retrieveRequest.Name); if (fakeRelationShip == null) { throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", retrieveRequest.Name)); } var response = new RetrieveRelationshipResponse(); response.Results = new ParameterCollection(); response.Results.Add("RelationshipMetadata", GetRelationshipMetadata(fakeRelationShip)); response.ResponseName = "RetrieveRelationship"; return(response); }
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { var associateRequest = request as AssociateRequest; var service = ctx.GetFakedOrganizationService(); if (associateRequest == null) { throw new Exception("Only associate request can be processed!"); } var associateRelationship = associateRequest.Relationship; var relationShipName = associateRelationship.SchemaName; var fakeRelationShip = ctx.GetRelationship(relationShipName); if (fakeRelationShip == null) { throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipName)); } if (associateRequest.Target == null) { throw new Exception("Association without target is invalid!"); } foreach (var relatedEntityReference in associateRequest.RelatedEntities) { if (fakeRelationShip.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.ManyToMany) { var association = new Entity(fakeRelationShip.IntersectEntity) { Attributes = new AttributeCollection { { fakeRelationShip.Entity1Attribute, associateRequest.Target.Id }, { fakeRelationShip.Entity2Attribute, relatedEntityReference.Id } } }; service.Create(association); } else { //One to many //Get entity to update var entityToUpdate = new Entity(relatedEntityReference.LogicalName) { Id = relatedEntityReference.Id }; entityToUpdate[fakeRelationShip.Entity2Attribute] = associateRequest.Target; service.Update(entityToUpdate); } } return new AssociateResponse (); }
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { var associateRequest = request as AssociateRequest; var service = ctx.GetFakedOrganizationService(); if (associateRequest == null) { throw new Exception("Only associate request can be processed!"); } var associateRelationship = associateRequest.Relationship; var relationShipName = associateRelationship.SchemaName; var fakeRelationShip = ctx.GetRelationship(relationShipName); if (fakeRelationShip == null) { throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipName)); } if (associateRequest.Target == null) { throw new Exception("Association without target is invalid!"); } foreach (var relatedEntityReference in associateRequest.RelatedEntities) { if (fakeRelationShip.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.ManyToMany) { var association = new Entity(fakeRelationShip.IntersectEntity) { Attributes = new AttributeCollection { { fakeRelationShip.Entity1Attribute, associateRequest.Target.Id }, { fakeRelationShip.Entity2Attribute, relatedEntityReference.Id } } }; service.Create(association); } else { //One to many //Get entity to update var entityToUpdate = new Entity(relatedEntityReference.LogicalName) { Id = relatedEntityReference.Id }; entityToUpdate[fakeRelationShip.Entity2Attribute] = associateRequest.Target; service.Update(entityToUpdate); } } return(new AssociateResponse()); }
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { var disassociateRequest = request as DisassociateRequest; var service = ctx.GetFakedOrganizationService(); if (disassociateRequest == null) { throw new Exception("Only disassociate request can be processed!"); } var relationShipName = disassociateRequest.Relationship.SchemaName; var relationShip = ctx.GetRelationship(relationShipName); if (relationShip == null) { throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipName)); } if (disassociateRequest.Target == null) { throw new Exception("Disassociation without target is invalid!"); } foreach (var relatedEntity in disassociateRequest.RelatedEntities) { var isFrom1to2 = disassociateRequest.Target.LogicalName == relationShip.Entity1LogicalName || relatedEntity.LogicalName != relationShip.Entity1LogicalName || String.IsNullOrWhiteSpace(disassociateRequest.Target.LogicalName); var fromAttribute = isFrom1to2 ? relationShip.Entity1Attribute : relationShip.Entity2Attribute; var toAttribute = isFrom1to2 ? relationShip.Entity2Attribute : relationShip.Entity1Attribute; var query = new QueryExpression(relationShip.IntersectEntity) { ColumnSet = new ColumnSet(true), Criteria = new FilterExpression(LogicalOperator.And) }; query.Criteria.AddCondition(new ConditionExpression(fromAttribute, ConditionOperator.Equal, disassociateRequest.Target.Id)); query.Criteria.AddCondition(new ConditionExpression(toAttribute, ConditionOperator.Equal, relatedEntity.Id)); var results = service.RetrieveMultiple(query); if (results.Entities.Count == 1) { service.Delete(relationShip.IntersectEntity, results.Entities.First().Id); } } return(new DisassociateResponse()); }
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { var disassociateRequest = request as DisassociateRequest; var service = ctx.GetFakedOrganizationService(); if (disassociateRequest == null) { throw new Exception("Only disassociate request can be processed!"); } var relationShipName = disassociateRequest.Relationship.SchemaName; var relationShip = ctx.GetRelationship(relationShipName); if (relationShip == null) { throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipName)); } if (disassociateRequest.Target == null) { throw new Exception("Disassociation without target is invalid!"); } foreach (var relatedEntity in disassociateRequest.RelatedEntities) { var query = new QueryExpression(relationShip.IntersectEntity) { ColumnSet = new ColumnSet(true), Criteria = new FilterExpression(LogicalOperator.And) }; query.Criteria.AddCondition(new ConditionExpression(relationShip.Entity1Attribute, ConditionOperator.Equal, disassociateRequest.Target.Id)); query.Criteria.AddCondition(new ConditionExpression(relationShip.Entity2Attribute, ConditionOperator.Equal, relatedEntity.Id)); var results = service.RetrieveMultiple(query); if (results.Entities.Count == 1) { service.Delete(relationShip.IntersectEntity, results.Entities.First().Id); } } return new DisassociateResponse(); }
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { var associateRequest = request as AssociateRequest; var service = ctx.GetFakedOrganizationService(); if (associateRequest == null) { throw new Exception("Only associate request can be processed!"); } var relationShipName = associateRequest.Relationship.SchemaName; var relationShip = ctx.GetRelationship(relationShipName); if (relationShip == null) { throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipName)); } if (associateRequest.Target == null) { throw new Exception("Association without target is invalid!"); } foreach (var relatedEntity in associateRequest.RelatedEntities) { var association = new Entity(relationShip.IntersectEntity) { Attributes = new AttributeCollection { { relationShip.Entity1Attribute, associateRequest.Target.Id }, { relationShip.Entity2Attribute, relatedEntity.Id } } }; service.Create(association); } return new AssociateResponse (); }
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { var associateRequest = request as AssociateRequest; var service = ctx.GetFakedOrganizationService(); if (associateRequest == null) { throw new Exception("Only associate request can be processed!"); } var associateRelationship = associateRequest.Relationship; var relationShipName = associateRelationship.SchemaName; var fakeRelationShip = ctx.GetRelationship(relationShipName); if (fakeRelationShip == null) { throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipName)); } if (associateRequest.Target == null) { throw new Exception("Association without target is invalid!"); } foreach (var relatedEntityReference in associateRequest.RelatedEntities) { if (fakeRelationShip.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.ManyToMany) { var isFrom1to2 = associateRequest.Target.LogicalName == fakeRelationShip.Entity1LogicalName || relatedEntityReference.LogicalName != fakeRelationShip.Entity1LogicalName || String.IsNullOrWhiteSpace(associateRequest.Target.LogicalName); var fromAttribute = isFrom1to2 ? fakeRelationShip.Entity1Attribute : fakeRelationShip.Entity2Attribute; var toAttribute = isFrom1to2 ? fakeRelationShip.Entity2Attribute : fakeRelationShip.Entity1Attribute; var fromEntityName = isFrom1to2 ? fakeRelationShip.Entity1LogicalName : fakeRelationShip.Entity2LogicalName; var toEntityName = isFrom1to2 ? fakeRelationShip.Entity2LogicalName : fakeRelationShip.Entity1LogicalName; //Check records exist var targetExists = ctx.CreateQuery(fromEntityName) .Where(e => e.Id == associateRequest.Target.Id) .FirstOrDefault() != null; if (!targetExists) { throw new Exception(string.Format("{0} with Id {1} doesn't exist", fromEntityName, associateRequest.Target.Id.ToString())); } var relatedExists = ctx.CreateQuery(toEntityName) .Where(e => e.Id == relatedEntityReference.Id) .FirstOrDefault() != null; if (!relatedExists) { throw new Exception(string.Format("{0} with Id {1} doesn't exist", toEntityName, relatedEntityReference.Id.ToString())); } var association = new Entity(fakeRelationShip.IntersectEntity) { Attributes = new AttributeCollection { { fromAttribute, associateRequest.Target.Id }, { toAttribute, relatedEntityReference.Id } } }; service.Create(association); } else { //One to many //Get entity to update var entityToUpdate = new Entity(relatedEntityReference.LogicalName) { Id = relatedEntityReference.Id }; entityToUpdate[fakeRelationShip.Entity2Attribute] = associateRequest.Target; service.Update(entityToUpdate); } } return(new AssociateResponse()); }
public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext context) { var request = req as RetrieveRequest; if (request.Target == null) { throw new ArgumentNullException("Target", "RetrieveRequest without Target is invalid."); } var entityName = request.Target.LogicalName; var columnSet = request.ColumnSet; if (columnSet == null) { throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), "Required field 'ColumnSet' is missing"); } var id = context.GetRecordUniqueId(request.Target); //Entity logical name exists, so , check if the requested entity exists if (context.Data.ContainsKey(entityName) && context.Data[entityName] != null && context.Data[entityName].ContainsKey(id)) { //Return the subset of columns requested only var reflectedType = context.FindReflectedType(entityName); //Entity found => return only the subset of columns specified or all of them var resultEntity = context.Data[entityName][id].Clone(reflectedType, context); if (!columnSet.AllColumns) { resultEntity = resultEntity.ProjectAttributes(columnSet, context); } resultEntity.ApplyDateBehaviour(context); if (request.RelatedEntitiesQuery != null && request.RelatedEntitiesQuery.Count > 0) { foreach (var relatedEntitiesQuery in request.RelatedEntitiesQuery) { if (relatedEntitiesQuery.Value == null) { throw new ArgumentNullException("relateEntitiesQuery.Value", string.Format("RelatedEntitiesQuery for \"{0}\" does not contain a Query Expression.", relatedEntitiesQuery.Key.SchemaName)); } var fakeRelationship = context.GetRelationship(relatedEntitiesQuery.Key.SchemaName); if (fakeRelationship == null) { throw new Exception(string.Format("Relationship \"{0}\" does not exist in the metadata cache.", relatedEntitiesQuery.Key.SchemaName)); } var relatedEntitiesQueryValue = (QueryExpression)relatedEntitiesQuery.Value; QueryExpression retrieveRelatedEntitiesQuery = relatedEntitiesQueryValue.Clone(); if (fakeRelationship.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.OneToMany) { var isFrom1to2 = relatedEntitiesQueryValue.EntityName == fakeRelationship.Entity1LogicalName || request.Target.LogicalName != fakeRelationship.Entity1LogicalName || string.IsNullOrWhiteSpace(relatedEntitiesQueryValue.EntityName); if (isFrom1to2) { var fromAttribute = isFrom1to2 ? fakeRelationship.Entity1Attribute : fakeRelationship.Entity2Attribute; var toAttribute = isFrom1to2 ? fakeRelationship.Entity2Attribute : fakeRelationship.Entity1Attribute; var linkEntity = new LinkEntity { Columns = new ColumnSet(false), LinkFromAttributeName = fromAttribute, LinkFromEntityName = retrieveRelatedEntitiesQuery.EntityName, LinkToAttributeName = toAttribute, LinkToEntityName = resultEntity.LogicalName }; if (retrieveRelatedEntitiesQuery.Criteria == null) { retrieveRelatedEntitiesQuery.Criteria = new FilterExpression(); } retrieveRelatedEntitiesQuery.Criteria .AddFilter(LogicalOperator.And) .AddCondition(linkEntity.LinkFromAttributeName, ConditionOperator.Equal, resultEntity.Id); } else { var link = retrieveRelatedEntitiesQuery.AddLink(fakeRelationship.Entity1LogicalName, fakeRelationship.Entity2Attribute, fakeRelationship.Entity1Attribute); link.LinkCriteria.AddCondition(resultEntity.LogicalName + "id", ConditionOperator.Equal, resultEntity.Id); } } else { var isFrom1 = fakeRelationship.Entity1LogicalName == retrieveRelatedEntitiesQuery.EntityName; var linkAttributeName = isFrom1 ? fakeRelationship.Entity1Attribute : fakeRelationship.Entity2Attribute; var conditionAttributeName = isFrom1 ? fakeRelationship.Entity2Attribute : fakeRelationship.Entity1Attribute; var linkEntity = new LinkEntity { Columns = new ColumnSet(false), LinkFromAttributeName = linkAttributeName, LinkFromEntityName = retrieveRelatedEntitiesQuery.EntityName, LinkToAttributeName = linkAttributeName, LinkToEntityName = fakeRelationship.IntersectEntity, LinkCriteria = new FilterExpression { Conditions = { new ConditionExpression(conditionAttributeName, ConditionOperator.Equal, resultEntity.Id) } } }; retrieveRelatedEntitiesQuery.LinkEntities.Add(linkEntity); } var retrieveRelatedEntitiesRequest = new RetrieveMultipleRequest { Query = retrieveRelatedEntitiesQuery }; //use of an executor directly; if to use service.RetrieveMultiple then the result will be //limited to the number of records per page (somewhere in future release). //ALL RECORDS are needed here. var executor = new RetrieveMultipleRequestExecutor(); var retrieveRelatedEntitiesResponse = executor .Execute(retrieveRelatedEntitiesRequest, context) as RetrieveMultipleResponse; if (retrieveRelatedEntitiesResponse.EntityCollection.Entities.Count == 0) { continue; } resultEntity.RelatedEntities .Add(relatedEntitiesQuery.Key, retrieveRelatedEntitiesResponse.EntityCollection); } } return(new RetrieveResponse { Results = new ParameterCollection { { "Entity", resultEntity } } }); } else { // Entity not found in the context => FaultException throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), $"{entityName} With Id = {id:D} Does Not Exist"); } }
public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext context) { var request = req as RetrieveRequest; if (request.Target == null) { throw new ArgumentNullException("Target", "RetrieveRequest without Target is invalid."); } var service = context.GetOrganizationService(); var targetId = context.GetRecordUniqueId(request.Target); var resultEntity = service.Retrieve(request.Target.LogicalName, targetId, request.ColumnSet); resultEntity.ApplyDateBehaviour(context); if (request.RelatedEntitiesQuery != null && request.RelatedEntitiesQuery.Count > 0) { foreach (var relatedEntitiesQuery in request.RelatedEntitiesQuery) { if (relatedEntitiesQuery.Value == null) { throw new ArgumentNullException("relateEntitiesQuery.Value", string.Format("RelatedEntitiesQuery for \"{0}\" does not contain a Query Expression.", relatedEntitiesQuery.Key.SchemaName)); } var fakeRelationship = context.GetRelationship(relatedEntitiesQuery.Key.SchemaName); if (fakeRelationship == null) { throw new Exception(string.Format("Relationship \"{0}\" does not exist in the metadata cache.", relatedEntitiesQuery.Key.SchemaName)); } var relatedEntitiesQueryValue = (QueryExpression)relatedEntitiesQuery.Value; QueryExpression retrieveRelatedEntitiesQuery = relatedEntitiesQueryValue.Clone(); if (fakeRelationship.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.OneToMany) { var isFrom1to2 = relatedEntitiesQueryValue.EntityName == fakeRelationship.Entity1LogicalName || request.Target.LogicalName != fakeRelationship.Entity1LogicalName || string.IsNullOrWhiteSpace(relatedEntitiesQueryValue.EntityName); if (isFrom1to2) { var fromAttribute = isFrom1to2 ? fakeRelationship.Entity1Attribute : fakeRelationship.Entity2Attribute; var toAttribute = isFrom1to2 ? fakeRelationship.Entity2Attribute : fakeRelationship.Entity1Attribute; var linkEntity = new LinkEntity { Columns = new ColumnSet(false), LinkFromAttributeName = fromAttribute, LinkFromEntityName = retrieveRelatedEntitiesQuery.EntityName, LinkToAttributeName = toAttribute, LinkToEntityName = resultEntity.LogicalName }; if (retrieveRelatedEntitiesQuery.Criteria == null) { retrieveRelatedEntitiesQuery.Criteria = new FilterExpression(); } retrieveRelatedEntitiesQuery.Criteria .AddFilter(LogicalOperator.And) .AddCondition(linkEntity.LinkFromAttributeName, ConditionOperator.Equal, resultEntity.Id); } else { var link = retrieveRelatedEntitiesQuery.AddLink(fakeRelationship.Entity1LogicalName, fakeRelationship.Entity2Attribute, fakeRelationship.Entity1Attribute); link.LinkCriteria.AddCondition(resultEntity.LogicalName + "id", ConditionOperator.Equal, resultEntity.Id); } } else { var isFrom1 = fakeRelationship.Entity1LogicalName == retrieveRelatedEntitiesQuery.EntityName; var linkAttributeName = isFrom1 ? fakeRelationship.Entity1Attribute : fakeRelationship.Entity2Attribute; var conditionAttributeName = isFrom1 ? fakeRelationship.Entity2Attribute : fakeRelationship.Entity1Attribute; var linkEntity = new LinkEntity { Columns = new ColumnSet(false), LinkFromAttributeName = linkAttributeName, LinkFromEntityName = retrieveRelatedEntitiesQuery.EntityName, LinkToAttributeName = linkAttributeName, LinkToEntityName = fakeRelationship.IntersectEntity, LinkCriteria = new FilterExpression { Conditions = { new ConditionExpression(conditionAttributeName, ConditionOperator.Equal, resultEntity.Id) } } }; retrieveRelatedEntitiesQuery.LinkEntities.Add(linkEntity); } var retrieveRelatedEntitiesRequest = new RetrieveMultipleRequest { Query = retrieveRelatedEntitiesQuery }; //use of an executor directly; if to use service.RetrieveMultiple then the result will be //limited to the number of records per page (somewhere in future release). //ALL RECORDS are needed here. var executor = new RetrieveMultipleRequestExecutor(); var retrieveRelatedEntitiesResponse = executor .Execute(retrieveRelatedEntitiesRequest, context) as RetrieveMultipleResponse; if (retrieveRelatedEntitiesResponse.EntityCollection.Entities.Count == 0) { continue; } resultEntity.RelatedEntities .Add(relatedEntitiesQuery.Key, retrieveRelatedEntitiesResponse.EntityCollection); } } return(new RetrieveResponse { Results = new ParameterCollection { { "Entity", resultEntity } } }); }