private RetrieveAttributeResponse ExecuteInternal(RetrieveAttributeRequest request)
        {
            var response = new RetrieveAttributeResponse();

            var optionSet = new PicklistAttributeMetadata
            {
                OptionSet = new OptionSetMetadata()
            };

            response.Results["AttributeMetadata"] = optionSet;

            var enumExpression = CrmServiceUtility.GetEarlyBoundProxyAssembly().GetTypes().Where(t =>
                                                                                                 t.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0 &&
                                                                                                 t.GetCustomAttributes(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute), false).Length > 0 &&
                                                                                                 t.Name.Contains(request.LogicalName)).ToList();

            // Search By EntityLogicalName_LogicalName
            // Then By LogicName_EntityLogicalName
            // Then By LogicaName
            var enumType = enumExpression.FirstOrDefault(t => t.Name == request.EntityLogicalName + "_" + request.LogicalName) ??
                           enumExpression.FirstOrDefault(t => t.Name == request.LogicalName + "_" + request.EntityLogicalName) ??
                           enumExpression.FirstOrDefault(t => t.Name == request.LogicalName);

            AddEnumTypeValues(optionSet.OptionSet, enumType,
                              String.Format("Unable to find local optionset enum for entity: {0}, attribute: {1}", request.EntityLogicalName, request.LogicalName));

            return(response);
        }
コード例 #2
0
        private PicklistAttributeMetadata CreateOptionSetAttributeMetadata(RetrieveAttributeRequest request, Type propertyType)
        {
            if (propertyType == typeof(OptionSetValue))
            {
                var enumExpression =
                    CrmServiceUtility.GetEarlyBoundProxyAssembly().GetTypes().Where(
                        t =>
                        t.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0 &&
                        t.GetCustomAttributes(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute), false).Length > 0 &&
                        t.Name.Contains(request.LogicalName)).ToList();

                // Search By EntityLogicalName_LogicalName
                // Then By LogicName_EntityLogicalName
                // Then By LogicalName
                propertyType = enumExpression.FirstOrDefault(t => t.Name == request.EntityLogicalName + "_" + request.LogicalName) ??
                               enumExpression.FirstOrDefault(t => t.Name == request.LogicalName + "_" + request.EntityLogicalName) ??
                               enumExpression.FirstOrDefault(t => t.Name == request.LogicalName);
            }

            var optionSet = new PicklistAttributeMetadata
            {
                OptionSet = new OptionSetMetadata()
            };

            AddEnumTypeValues(optionSet.OptionSet,
                              propertyType,
                              $"Unable to find local OptionSet enum for entity: {request.EntityLogicalName}, attribute: {request.LogicalName}");

            return(optionSet);
        }
コード例 #3
0
        /// <summary>
        /// Fakes out calls to RetireveAttribute Requests, using enums to generate the OptionSetMetaData.  Userful for mocking out any calls to determine the text values of an optionset.
        /// </summary>
        /// <param name="defaultLangaugeCode">The default langauge code.  Defaults to reading DefaultLanguageCode from the config, or 1033 if not found</param>
        /// <returns></returns>
        public TDerived WithLocalOptionSetsRetrievedFromEnum(int?defaultLangaugeCode = null)
        {
            defaultLangaugeCode = defaultLangaugeCode ?? AppConfig.DefaultLanguageCode;
            ExecuteFuncs.Add((s, r) =>
            {
                var attRequest = r as RetrieveAttributeRequest;
                if (attRequest == null)
                {
                    return(s.Execute(r));
                }

                var response = new RetrieveAttributeResponse();

                var optionSet = new PicklistAttributeMetadata
                {
                    OptionSet = new OptionSetMetadata()
                };

                response.Results["AttributeMetadata"] = optionSet;

                var enumExpression = CrmServiceUtility.GetEarlyBoundProxyAssembly().GetTypes().Where(t =>
                                                                                                     (t.Name == attRequest.EntityLogicalName + "_" + attRequest.LogicalName ||
                                                                                                      t.Name == attRequest.LogicalName + "_" + attRequest.EntityLogicalName) &&
                                                                                                     t.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0 &&
                                                                                                     t.GetCustomAttributes(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute), false).Length > 0);

                // Return EntityLogicalName_Logical Name first
                var enumType = enumExpression.OrderBy(t => t.Name != attRequest.EntityLogicalName + "_" + attRequest.LogicalName).FirstOrDefault();

                if (enumType == null)
                {
                    throw new Exception($"Unable to find local optionset enum for entity: {attRequest.EntityLogicalName}, attribute: {attRequest.LogicalName}");
                }

                foreach (var value in Enum.GetValues(enumType))
                {
                    optionSet.OptionSet.Options.Add(
                        new OptionMetadata
                    {
                        Value = (int)value,
                        Label = new Label
                        {
                            UserLocalizedLabel = new LocalizedLabel(value.ToString(), defaultLangaugeCode.Value),
                        },
                    });
                }

                return(response);
            })
            ;
            return(This);
        }
コード例 #4
0
        private RetrieveOptionSetResponse ExecuteInternal(RetrieveOptionSetRequest request)
        {
            var response  = new RetrieveOptionSetResponse();
            var optionSet = new OptionSetMetadata(new OptionMetadataCollection());

            response.Results["OptionSetMetadata"] = optionSet;

            var types    = CrmServiceUtility.GetEarlyBoundProxyAssembly(Info.EarlyBoundEntityAssembly).GetTypes();
            var enumType = types.FirstOrDefault(t => IsEnumType(t, request.Name)) ?? types.FirstOrDefault(t => IsEnumType(t, request.Name + "Enum"));

            AddEnumTypeValues(optionSet, enumType, "Unable to find global optionset enum " + request.Name);

            return(response);
        }
コード例 #5
0
ファイル: TestBase.cs プロジェクト: ScottColson/XrmUnitTest
 /// <summary>
 /// Gets the organization service.
 /// </summary>
 /// <param name="organizationName">Name of the organization.</param>
 /// <param name="impersonationUserId">The impersonation user identifier.</param>
 /// <returns></returns>
 public static IClientSideOrganizationService GetOrganizationService(string organizationName  = null,
                                                                     Guid impersonationUserId = new Guid())
 {
     LoadUserUnitTestSettings();
     organizationName = organizationName ?? OrgName;
     if (UseLocalCrmDatabase)
     {
         return(GetLocalCrmDatabaseOrganizationService(organizationName, impersonationUserId));
     }
     else
     {
         var service = CrmServiceUtility.GetOrganizationService();
         if (service == null)
         {
             throw new Exception("Organization Service was Null!");
         }
         return(service);
     }
 }
コード例 #6
0
ファイル: TestBase.cs プロジェクト: JQ-IOWA/XrmUnitTest
        /// <summary>
        /// Gets the organization service.
        /// </summary>
        /// <param name="organizationName">Name of the organization.</param>
        /// <param name="impersonationUserId">The impersonation user identifier.</param>
        /// <param name="enableProxyTypes">if set to <c>true</c> [enable proxy types].</param>
        /// <returns></returns>
        public static IClientSideOrganizationService GetOrganizationService(string organizationName  = null,
                                                                            Guid impersonationUserId = new Guid(), bool enableProxyTypes = true)
        {
            LoadUserUnitTestSettings();
            organizationName = organizationName ?? OrgName;
            if (UseLocalCrmDatabase)
            {
                return(GetLocalCrmDatabaseOrganizationService(organizationName, impersonationUserId));
            }

            var info = GetCrmServiceEntity(organizationName, enableProxyTypes);

            if (AppConfig.UseDebugCredentialsForTesting)
            {
                // Only in Unit tests should this be allowed.
                info.UserName       = AppConfig.DebugUserAccountName;
                info.UserPassword   = AppConfig.DebugUserAccountPassword;
                info.UserDomainName = AppConfig.DebugUserAccountDomain;
            }
            return(CrmServiceUtility.GetOrganizationService(info));
        }
コード例 #7
0
 public static void InitializeTestSettings()
 {
     if (!TestSettings.AssumptionXmlPath.IsConfigured)
     {
         TestSettings.AssumptionXmlPath.Configure(new PatherFinderProjectOfType(typeof(MsTestProvider), "Assumptions\\Entity Xml"));
     }
     if (!TestSettings.UserTestConfigPath.IsConfigured)
     {
         TestSettings.UserTestConfigPath.Configure(new PatherFinderProjectOfType(typeof(MsTestProvider), "UnitTestSettings.user.config"));
     }
     if (!TestSettings.EntityBuilder.IsConfigured)
     {
         TestSettings.EntityBuilder.ConfigureDerivedAssembly <EntityBuilder <Entity> >();
     }
     if (!TestSettings.EarlyBound.IsConfigured)
     {
         TestSettings.EarlyBound.ConfigureDerivedAssembly <CrmContext>();
         CrmServiceUtility.GetEarlyBoundProxyAssembly(TestSettings.EarlyBound.Assembly);
     }
     if (!TestSettings.TestFrameworkProvider.IsConfigured)
     {
         TestSettings.TestFrameworkProvider.Configure(new MsTestProvider());
     }
 }
コード例 #8
0
        private RetrieveRelationshipResponse ExecuteInternal(RetrieveRelationshipRequest request)
        {
            if (string.IsNullOrWhiteSpace(request.Name))
            {
                throw new NotImplementedException("Unable to process a RetrieveRelationshipRequest without a Name");
            }

            PropertyInfo property = null;

            foreach (var type in CrmServiceUtility.GetEarlyBoundProxyAssembly(Info.EarlyBoundEntityAssembly).GetTypes())
            {
                property = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                           .FirstOrDefault(a =>
                {
                    if (a.GetCustomAttributes(typeof(AttributeLogicalNameAttribute), false).Length == 0)
                    {
                        return(false);
                    }

                    var att = a.GetCustomAttributes(typeof(RelationshipSchemaNameAttribute), false);
                    return(att.Length != 0 && ((RelationshipSchemaNameAttribute)att.FirstOrDefault())?.SchemaName == request.Name);
                });
                if (property != null)
                {
                    break;
                }
            }

            if (property == null)
            {
                throw new KeyNotFoundException($"Unable to find a relationship {request.Name}.");
            }

            RelationshipMetadataBase relationship;

            if (typeof(Entity).IsAssignableFrom(property.PropertyType))
            {
                var referencedType = EntityHelper.GetEntityLogicalName(property.PropertyType);
                relationship = new OneToManyRelationshipMetadata
                {
                    ReferencedEntity     = referencedType,
                    ReferencedAttribute  = EntityHelper.GetIdAttributeName(referencedType),
                    ReferencingEntity    = EntityHelper.GetEntityLogicalName(property.DeclaringType),
                    ReferencingAttribute = property.GetAttributeLogicalName()
                };
            }
            else
            {
                var att = property.GetAttributeLogicalName();
                relationship = new ManyToManyRelationshipMetadata
                {
                    IntersectEntityName = att.Substring(0, att.Length - "_association".Length)
                };
            }
            var response = new RetrieveRelationshipResponse
            {
                Results =
                {
                    ["RelationshipMetadata"] = relationship
                }
            };

            return(response);
        }
コード例 #9
0
        private RetrieveAttributeResponse ExecuteInternal(RetrieveAttributeRequest request)
        {
            var response   = new RetrieveAttributeResponse();
            var entityType =
                CrmServiceUtility.GetEarlyBoundProxyAssembly().GetTypes().FirstOrDefault(t =>
                                                                                         t.GetCustomAttribute <EntityLogicalNameAttribute>(true)?.LogicalName == request.EntityLogicalName);

            var propertyTypes = entityType?.GetProperties()
                                .Where(p =>
                                       p.GetCustomAttribute <AttributeLogicalNameAttribute>()?.LogicalName == request.LogicalName
                                       ).Select(p => p.PropertyType.IsGenericType
                    ? p.PropertyType.GenericTypeArguments.First()
                    : p.PropertyType).ToList();

            var propertyType = propertyTypes?.Count == 1
                ? propertyTypes[0]
                : propertyTypes?.FirstOrDefault(p => p != typeof(OptionSetValue) &&
                                                p != typeof(EntityReference));      // Handle OptionSets/EntityReferences that may have multiple properties

            if (propertyType == null)
            {
                throw new Exception($"Unable to find a property for Entity {request.EntityLogicalName} and property {request.LogicalName} in {CrmServiceUtility.GetEarlyBoundProxyAssembly().FullName}");
            }

            AttributeMetadata metadata;

            if (propertyType.IsEnum || propertyTypes.Any(p => p == typeof(OptionSetValue)))
            {
                metadata = CreateOptionSetAttributeMetadata(request, propertyType);
            }
            else if (propertyType == typeof(string))
            {
                metadata = new StringAttributeMetadata(request.LogicalName);
            }
            else if (propertyTypes.Any(p => p == typeof(EntityReference)))
            {
                metadata = new LookupAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
#if !XRM_2013
            else if (propertyType == typeof(Guid))
            {
                metadata = new UniqueIdentifierAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
#endif
            else if (propertyType == typeof(bool))
            {
                metadata = new BooleanAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
            else if (propertyType == typeof(Money))
            {
                metadata = new MoneyAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
            else if (propertyType == typeof(int))
            {
                metadata = new IntegerAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
            else if (propertyType == typeof(long))
            {
                metadata = new BigIntAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
            else if (propertyType == typeof(DateTime))
            {
                metadata = new DateTimeAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
            else if (propertyType == typeof(double))
            {
                metadata = new DoubleAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
            else if (propertyType == typeof(decimal))
            {
                metadata = new DecimalAttributeMetadata
                {
                    LogicalName = request.LogicalName
                };
            }
            else
            {
                throw new NotImplementedException($"Attribute Type of {propertyType.FullName} is not implemented.");
            }
            response.Results["AttributeMetadata"] = metadata;
            return(response);
        }