private List<ObjectProvider> GetObjectProviders()
        {
            if (this.InstallAdapter.MetadataTimestamp != this.InstallAdapter.RetrieveMetadataTimestamp())
            {
                List<ObjectProvider> MtoMAvailableProviders = new List<ObjectProvider>();

                this.availableProviders = new List<ObjectProvider>();
                RetrieveAllEntitiesRequest retrieveAll = new RetrieveAllEntitiesRequest();
                RetrieveAllEntitiesResponse response = new RetrieveAllEntitiesResponse();
                RetrieveEntityRequest entityRequest = new RetrieveEntityRequest();

                this.PublishPreConfigurationMessage(string.Format(CultureInfo.CurrentCulture, Resources.StartRetrievingEntityMetadata));
                response = this.InstallAdapter.OrganizationService.Execute(retrieveAll) as RetrieveAllEntitiesResponse;
                foreach (EntityMetadata crmMetadata in response.EntityMetadata)
                {
                    entityRequest.MetadataId = crmMetadata.MetadataId.Value;
                    entityRequest.EntityFilters = EntityFilters.Relationships;
                    RetrieveEntityResponse entityResponse = this.InstallAdapter.OrganizationService.Execute(entityRequest) as RetrieveEntityResponse;
                    if (entityResponse.EntityMetadata.DisplayName.LocalizedLabels.Count > 0 && !entityResponse.EntityMetadata.LogicalName.StartsWith("dynamics_deleted", StringComparison.OrdinalIgnoreCase) && entityResponse.EntityMetadata.IsCustomizable.Value)
                    {
                        this.availableProviders.Add(new DynamicObjectProvider() { Adapter = this.InstallAdapter, Id = crmMetadata.MetadataId.Value, DisplayName = entityResponse.EntityMetadata.DisplayName.LocalizedLabels[0].Label, Name = entityResponse.EntityMetadata.LogicalName });
                    }

                    List<ManyToManyRelationshipMetadata> entityMToMRelationships = (from meta in entityResponse.EntityMetadata.ManyToManyRelationships
                                                                                         where (meta.IsCustomRelationship.Value == true)
                                                                                         || (meta.SecurityTypes == SecurityTypes.ParentChild && meta.IsCustomRelationship.Value == false)
                                                                                         select meta).ToList();
                    if (entityMToMRelationships.Count > 0)
                    {
                        foreach (ManyToManyRelationshipMetadata relation in entityMToMRelationships)
                        {
                            if (!MtoMAvailableProviders.Any(f => f.DisplayName == relation.SchemaName))
                            {
                                MtoMAvailableProviders.Add(new DynamicObjectProvider() { Adapter = this.InstallAdapter, Id = relation.MetadataId.Value, DisplayName = relation.SchemaName, Name = relation.SchemaName + ",Relationship" });
                            }
                        }
                    }
                }

                foreach (ObjectProvider relationObjectProvider in MtoMAvailableProviders)
                {
                    this.availableProviders.Add(relationObjectProvider);
                }

                this.PublishPostConfigurationMessage(string.Format(CultureInfo.CurrentCulture, Resources.FinishedRetreivingEntityMetadata));
                this.InstallAdapter.MetadataTimestamp = this.InstallAdapter.RetrieveMetadataTimestamp();
                this.PublishPostConfigurationMessage(string.Format(CultureInfo.CurrentCulture, Resources.MetadataTimeStamp, this.InstallAdapter.MetadataTimestamp));
            }

            return this.availableProviders;
        }
        private static void GeneratePicklistObjectDefinitionEntries(ObjectDefinition objDef, RetrieveAllEntitiesResponse response)
        {
            var orderedEntityMetadata = ((EntityMetadata[])response.Results.First().Value).Where(x => x.DisplayName.LocalizedLabels.Count > 0).OrderBy(x => x.DisplayName.LocalizedLabels[0].Label);

            // For each entity, add a Type
            foreach (var entityMetadata in orderedEntityMetadata)
            {
                var picklistAttributesMetaData = entityMetadata.Attributes.Where(x => x.AttributeType == AttributeTypeCode.Picklist && x.IsCustomizable.Value == true).OrderBy(x => x.LogicalName);
                var picklistAttributes = picklistAttributesMetaData.Where(x => ((PicklistAttributeMetadata)x).OptionSet.IsGlobal.Value == false);
                var entityDisplayName = entityMetadata.DisplayName.LocalizedLabels[0].Label;

                // All picklists for an entity are here.
                if (picklistAttributes.Count() > 0)
                {
                    FieldDefinition entityDef = new FieldDefinition() { Name = entityDisplayName.ToString().Replace(" ", string.Empty), DisplayName = entityDisplayName, TypeName = entityMetadata.LogicalName, IsRequired = false };
                    objDef.Types.First().Children.Add(entityDef);
                    ComplexType complexType = new ComplexType() { ClrType = typeof(System.Collections.Generic.Dictionary<string, object>), Name = entityMetadata.LogicalName };
                    objDef.Types.Add(complexType);
                    foreach (var picklist in picklistAttributes)
                    {
                        string displayName = picklist.DisplayName.LocalizedLabels[0].Label;
                        ComplexType fieldType = CRM2011AdapterUtilities.ComplexTypeConvert(picklist.AttributeType.Value, objDef);

                        if (complexType != null)
                        {
                            FieldDefinition tempDef = new FieldDefinition() { Name = picklist.LogicalName, DisplayName = displayName, TypeDefinition = fieldType, TypeName = fieldType.Name };
                            complexType.Fields.Add(tempDef);
                        }
                    }
                }
            }

            var optionSetValueType = new CollectionType() { ClrType = typeof(string[]), Name = "OptionSetValue" };
            optionSetValueType.Item = new FieldDefinition() { Name = "Item", TypeName = "System.String", DisplayName = "Options", IsRequired = false };

            objDef.Types.Add(optionSetValueType);
        }