private void QueryQuickSearchEntities()
        {
            OrganizationServiceProxy.RegisterExecuteMessageResponseType("ExecuteFetch", typeof(ExecuteFetchResponse));

            // Get the entities defined in the correct order
            string fetchxml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='multientitysearchentities'>
                                    <attribute name='entityname' />
                                    <order attribute='entityorder' descending='false' />
                                  </entity>
                                </fetch>";

            // We have to use the deprecated ExecuteFetchRequest because you can't access the multientitysearchentities through RetrieveMultiple
            ExecuteFetchRequest request = new ExecuteFetchRequest();

            request.FetchXml = fetchxml;
            ExecuteFetchResponse entityList    = (ExecuteFetchResponse)OrganizationServiceProxy.Execute(request);
            jQueryObject         entityListDOM = jQuery.FromHtml(entityList.FetchXmlResult);


            _entityTypeNames = new List <string>();
            jQueryObject results = entityListDOM.First().Find("result");

            results.Each(delegate(int index, Element element)
            {
                string entityName = XmlHelper.SelectSingleNodeValue((XmlNode)(object)element, "entityname");
                _entityTypeNames.Add(entityName);
            });

            MetadataQueryBuilder builder = new MetadataQueryBuilder();

            builder.AddEntities(_entityTypeNames, new List <string>("ObjectTypeCode", "DisplayCollectionName"));
            builder.SetLanguage((int)Script.Literal("USER_LANGUAGE_CODE"));

            RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)OrganizationServiceProxy.Execute(builder.Request);

            _entityMetadata = new Dictionary <string, EntityMetadata>();
            foreach (EntityMetadata entity in response.EntityMetadata)
            {
                _entityMetadata[entity.LogicalName] = entity;
            }
        }
        public void QueryMetadata()
        {
            // Load the display Names
            MetadataQueryBuilder builder    = new MetadataQueryBuilder();
            List <string>        entities   = new List <string>();
            List <string>        attributes = new List <string>();

            foreach (string entityLogicalName in EntityLookup.Keys)
            {
                entities.Add(entityLogicalName);
                EntityQuery entity = EntityLookup[entityLogicalName];
                foreach (string attributeLogicalName in entity.Attributes.Keys)
                {
                    AttributeQuery attribute = entity.Attributes[attributeLogicalName];
                    string         fieldName = attribute.LogicalName;
                    int            pos       = fieldName.IndexOf('.');
                    if (entity.AliasName != null && pos > -1)
                    {
                        fieldName = fieldName.Substr(pos);
                    }
                    attributes.Add(fieldName);
                }
            }
            builder.AddEntities(entities, new List <string>("Attributes", "DisplayName", "DisplayCollectionName", "PrimaryImageAttribute"));
            builder.AddAttributes(attributes, new List <string>("DisplayName", "AttributeType", "IsPrimaryName"));
            builder.SetLanguage((int)Script.Literal("USER_LANGUAGE_CODE"));

            RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)OrganizationServiceProxy.Execute(builder.Request);

            // Update the display names
            // TODO: Add the lookup relationship in brackets for alias entitie
            foreach (EntityMetadata entityMetadata in response.EntityMetadata)
            {
                // Get the entity
                EntityQuery entityQuery = EntityLookup[entityMetadata.LogicalName];
                entityQuery.DisplayName           = entityMetadata.DisplayName.UserLocalizedLabel.Label;
                entityQuery.DisplayCollectionName = entityMetadata.DisplayCollectionName.UserLocalizedLabel.Label;
                entityQuery.PrimaryImageAttribute = entityMetadata.PrimaryImageAttribute;
                entityQuery.EntityTypeCode        = entityMetadata.ObjectTypeCode;
                foreach (AttributeMetadata attribute in entityMetadata.Attributes)
                {
                    if (entityQuery.Attributes.ContainsKey(attribute.LogicalName))
                    {
                        // Set the type
                        AttributeQuery attributeQuery = entityQuery.Attributes[attribute.LogicalName];
                        attributeQuery.AttributeType = attribute.AttributeType;
                        switch (attribute.AttributeType)
                        {
                        case AttributeTypeCode.Lookup:
                        case AttributeTypeCode.Picklist:
                        case AttributeTypeCode.Customer:
                        case AttributeTypeCode.Owner:
                        case AttributeTypeCode.Status:
                        case AttributeTypeCode.State:
                        case AttributeTypeCode.Boolean_:
                            LookupAttributes[attribute.LogicalName] = attributeQuery;
                            break;
                        }

                        attributeQuery.IsPrimaryName = attribute.IsPrimaryName;

                        // If the type is a lookup, then add the 'name' on to the end in the fetchxml
                        // this is so that we search the text value and not the numeric/guid value
                        foreach (Column col in attributeQuery.Columns)
                        {
                            col.Name     = attribute.DisplayName.UserLocalizedLabel.Label;
                            col.DataType = attribute.IsPrimaryName.Value ? "PrimaryNameLookup" : attribute.AttributeType.ToString();
                        }
                    }
                }
            }
        }