public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            var retrieveOptionSetRequest = (RetrieveOptionSetRequest)request;

            if (retrieveOptionSetRequest.MetadataId != Guid.Empty) //ToDo: Implement retrieving option sets by Id
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.ObjectDoesNotExist, $"Could not find optionset with optionset id: {retrieveOptionSetRequest.MetadataId}");
            }

            var name = retrieveOptionSetRequest.Name;

            if (string.IsNullOrEmpty(name))
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "Name is required when optionSet id is not specified");
            }

            if (!ctx.OptionSetValuesMetadata.ContainsKey(name))
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.ObjectDoesNotExist, string.Format("An OptionSetMetadata with the name {0} does not exist.", name));
            }

            var optionSetMetadata = ctx.OptionSetValuesMetadata[name];

            var response = new RetrieveOptionSetResponse()
            {
                Results = new ParameterCollection
                {
                    { "OptionSetMetadata", optionSetMetadata }
                }
            };

            return(response);
        }
        /// <summary>
        /// Extension method to join the attributes of entity e and otherEntity
        /// </summary>
        /// <param name="e"></param>
        /// <param name="otherEntity"></param>
        /// <param name="attributes"></param>
        /// <returns></returns>
        public static Entity JoinAttributes(this Entity e, Entity otherEntity, ColumnSet columnSet, string alias, XrmFakedContext context)
        {
            if (otherEntity == null)
            {
                return(e);                     //Left Join where otherEntity was not matched
            }
            otherEntity = otherEntity.Clone(); //To avoid joining entities from/to the same entities, which would cause collection modified exceptions

            if (columnSet.AllColumns)
            {
                foreach (var attKey in otherEntity.Attributes.Keys)
                {
                    e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, otherEntity[attKey]);
                }

                foreach (var attKey in otherEntity.FormattedValues.Keys)
                {
                    e.FormattedValues[alias + "." + attKey] = otherEntity.FormattedValues[attKey];
                }
            }
            else
            {
                //Return selected list of attributes
                foreach (var attKey in columnSet.Columns)
                {
                    if (!context.AttributeExistsInMetadata(otherEntity.LogicalName, attKey))
                    {
                        FakeOrganizationServiceFault.Throw(ErrorCodes.QueryBuilderNoAttribute, string.Format("The attribute {0} does not exist on this entity.", attKey));
                    }

                    if (otherEntity.Attributes.ContainsKey(attKey))
                    {
                        e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, otherEntity[attKey]);
                    }
                    else
                    {
                        e[alias + "." + attKey] = new AliasedValue(otherEntity.LogicalName, attKey, null);
                    }

                    if (otherEntity.FormattedValues.ContainsKey(attKey))
                    {
                        e.FormattedValues[alias + "." + attKey] = otherEntity.FormattedValues[attKey];
                    }
                }
            }
            return(e);
        }
Exemple #3
0
        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            var req = (AddMembersTeamRequest)request;

            if (req.MemberIds == null)
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "MemberIds parameter is required");
            }

            if (req.TeamId == Guid.Empty)
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "TeamId parameter is required");
            }

            var service = ctx.GetOrganizationService();

            // Find the list
            var team = ctx.CreateQuery("team").FirstOrDefault(e => e.Id == req.TeamId);

            if (team == null)
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.ObjectDoesNotExist, string.Format("Team with Id {0} wasn't found", req.TeamId.ToString()));
            }

            //ToDo:	FakeOrganizationServiceFault.Throw(ErrorCodes.CannotAddMembersToDefaultTeam, "Can't add members to the default business unit team.");

            foreach (var memberId in req.MemberIds)
            {
                var user = ctx.CreateQuery("systemuser").FirstOrDefault(e => e.Id == memberId);
                if (user == null)
                {
                    FakeOrganizationServiceFault.Throw(ErrorCodes.ObjectDoesNotExist, string.Format("SystemUser with Id {0} wasn't found", memberId.ToString()));
                }

                // Create teammembership
                var teammembership = new Entity("teammembership");
                teammembership["teamid"]       = team.Id;
                teammembership["systemuserid"] = memberId;
                service.Create(teammembership);
            }

            return(new AddMembersTeamResponse());
        }
        public static Entity ProjectAttributes(this Entity e, QueryExpression qe, XrmFakedContext context)
        {
            if (qe.ColumnSet == null || qe.ColumnSet.AllColumns)
            {
                return(RemoveNullAttributes(e)); //return all the original attributes
            }
            else
            {
                //Return selected list of attributes in a projected entity
                Entity projected = null;

                //However, if we are using proxy types, we must create a instance of the appropiate class
                if (context.ProxyTypesAssembly != null)
                {
                    var subClassType = context.FindReflectedType(e.LogicalName);
                    if (subClassType != null)
                    {
                        var instance = Activator.CreateInstance(subClassType);
                        projected    = (Entity)instance;
                        projected.Id = e.Id;
                    }
                    else
                    {
                        projected = new Entity(e.LogicalName)
                        {
                            Id = e.Id
                        }
                    };                                                       //fallback to generic type if type not found
                }
                else
                {
                    projected = new Entity(e.LogicalName)
                    {
                        Id = e.Id
                    }
                };


                foreach (var attKey in qe.ColumnSet.Columns)
                {
                    //Check if attribute really exists in metadata
                    if (!context.AttributeExistsInMetadata(e.LogicalName, attKey))
                    {
                        FakeOrganizationServiceFault.Throw(ErrorCodes.QueryBuilderNoAttribute, string.Format("The attribute {0} does not exist on this entity.", attKey));
                    }

                    if (e.Attributes.ContainsKey(attKey) && e.Attributes[attKey] != null)
                    {
                        projected[attKey] = CloneAttribute(e[attKey], context);

                        string formattedValue = "";

                        if (e.FormattedValues.TryGetValue(attKey, out formattedValue))
                        {
                            projected.FormattedValues[attKey] = formattedValue;
                        }
                    }
                }


                //Plus attributes from joins
                foreach (var le in qe.LinkEntities)
                {
                    ProjectAttributes(RemoveNullAttributes(e), projected, le, context);
                }
                return(RemoveNullAttributes(projected));
            }
        }
        public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx)
        {
            var req = (AddListMembersListRequest)request;

            if (req.MemberIds == null)
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "Required field 'MemberIds' is missing");
            }

            if (req.ListId == Guid.Empty)
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "Expected non-empty Guid.");
            }

            var service = ctx.GetOrganizationService();

            //Find the list
            var list = ctx.CreateQuery("list")
                       .Where(e => e.Id == req.ListId)
                       .FirstOrDefault();

            if (list == null)
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("List with Id {0} wasn't found", req.ListId.ToString()));
            }

            //Find the member
            if (!list.Attributes.ContainsKey("createdfromcode"))
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a CreatedFromCode attribute defined and it has to be an option set value.", req.ListId.ToString()));
            }

            if (list["createdfromcode"] != null && !(list["createdfromcode"] is OptionSetValue))
            {
                FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a CreatedFromCode attribute defined and it has to be an option set value.", req.ListId.ToString()));
            }

            var    createdFromCodeValue = (list["createdfromcode"] as OptionSetValue).Value;
            string memberEntityName     = "";

            switch (createdFromCodeValue)
            {
            case (int)ListCreatedFromCode.Account:
                memberEntityName = "account";
                break;

            case (int)ListCreatedFromCode.Contact:
                memberEntityName = "contact";
                break;

            case (int)ListCreatedFromCode.Lead:
                memberEntityName = "lead";
                break;

            default:
                FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a supported CreatedFromCode value (Account, Contact or Lead).", req.ListId.ToString()));
                break;
            }

            foreach (var memberId in req.MemberIds)
            {
                var member = ctx.CreateQuery(memberEntityName)
                             .Where(e => e.Id == memberId)
                             .FirstOrDefault();

                if (member == null)
                {
                    FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("Member of type {0} with Id {1} wasn't found", memberEntityName, memberId.ToString()));
                }

                //create member list
                var listmember = new Entity("listmember");
                listmember["listid"]   = new EntityReference("list", req.ListId);
                listmember["entityid"] = new EntityReference(memberEntityName, memberId);

                service.Create(listmember);
            }

            return(new AddListMembersListResponse());
        }