Beispiel #1
0
        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            var req = request as InitializeFromRequest;

            if (req == null)
            {
                throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), "Cannot execute InitializeFromRequest without the request");
            }

            //TODO: Implement logic to filter mapping attributes based on the req.TargetFieldType
            if (req.TargetFieldType != TargetFieldType.All)
            {
                throw PullRequestException.PartiallyNotImplementedOrganizationRequest(req.GetType(), "logic for filtering attributes based on TargetFieldType other than All is missing");
            }

            var service          = ctx.GetOrganizationService();
            var fetchXml         = string.Format(FetchMappingsByEntity, req.EntityMoniker.LogicalName, req.TargetEntityName);
            var mapping          = service.RetrieveMultiple(new FetchExpression(fetchXml));
            var sourceAttributes = mapping.Entities.Select(a => a.GetAttributeValue <AliasedValue>("attributemap.sourceattributename").Value.ToString()).ToArray();
            var columnSet        = sourceAttributes.Length == 0 ? new ColumnSet(true) : new ColumnSet(sourceAttributes);
            var source           = service.Retrieve(req.EntityMoniker.LogicalName, req.EntityMoniker.Id, columnSet);

            var entity = new Entity
            {
                LogicalName = req.TargetEntityName,
                Id          = Guid.NewGuid()
            };

            if (mapping.Entities.Count > 0)
            {
                foreach (var attr in source.Attributes)
                {
                    var mappingEntity = mapping.Entities.FirstOrDefault(e => e.GetAttributeValue <AliasedValue>("attributemap.sourceattributename").Value.ToString() == attr.Key);
                    if (mappingEntity == null)
                    {
                        continue;
                    }
                    var targetAttribute = mappingEntity.GetAttributeValue <AliasedValue>("attributemap.targetattributename").Value.ToString();
                    entity[targetAttribute] = attr.Value;
                }
            }

            var response = new InitializeFromResponse
            {
                Results =
                {
                    ["Entity"] = entity
                }
            };

            return(response);
        }
        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            var req = request as InitializeFromRequest;

            if (req == null)
            {
                throw new FaultException <OrganizationServiceFault>(new OrganizationServiceFault(), "Cannot execute InitializeFromRequest without the request");
            }

            //TODO: Implement logic to filter mapping attributes based on the req.TargetFieldType
            if (req.TargetFieldType != TargetFieldType.All)
            {
                throw PullRequestException.PartiallyNotImplementedOrganizationRequest(req.GetType(), "logic for filtering attributes based on TargetFieldType other than All is missing");
            }

            var service          = ctx.GetOrganizationService();
            var fetchXml         = string.Format(FetchMappingsByEntity, req.EntityMoniker.LogicalName, req.TargetEntityName);
            var mapping          = service.RetrieveMultiple(new FetchExpression(fetchXml));
            var sourceAttributes = mapping.Entities.Select(a => a.GetAttributeValue <AliasedValue>("attributemap.sourceattributename").Value.ToString()).ToArray();
            var columnSet        = sourceAttributes.Length == 0 ? new ColumnSet(true) : new ColumnSet(sourceAttributes);
            var source           = service.Retrieve(req.EntityMoniker.LogicalName, req.EntityMoniker.Id, columnSet);

            // If we are using proxy types, and the appropriate proxy type is found in
            // the assembly create an instance of the appropiate class
            // Othersise return a simple Entity
            Entity entity = new Entity
            {
                LogicalName = req.TargetEntityName,
                Id          = Guid.Empty
            };

            if (ctx.ProxyTypesAssembly != null)
            {
                var subClassType = ctx.FindReflectedType(req.TargetEntityName);
                if (subClassType != null)
                {
                    var instance = Activator.CreateInstance(subClassType);
                    entity = (Entity)instance;
                }
            }

            if (mapping.Entities.Count > 0)
            {
                foreach (var attr in source.Attributes)
                {
                    var mappingEntity = mapping.Entities.FirstOrDefault(e => e.GetAttributeValue <AliasedValue>("attributemap.sourceattributename").Value.ToString() == attr.Key);
                    if (mappingEntity == null)
                    {
                        continue;
                    }
                    var targetAttribute = mappingEntity.GetAttributeValue <AliasedValue>("attributemap.targetattributename").Value.ToString();
                    entity[targetAttribute] = attr.Value;

                    var isEntityReference = string.Equals(attr.Key, source.LogicalName + "id", StringComparison.CurrentCultureIgnoreCase);
                    if (isEntityReference)
                    {
                        entity[targetAttribute] = new EntityReference(source.LogicalName, (Guid)attr.Value);
                    }
                    else
                    {
                        entity[targetAttribute] = attr.Value;
                    }
                }
            }

            var response = new InitializeFromResponse
            {
                Results =
                {
                    ["Entity"] = entity
                }
            };

            return(response);
        }