private List <EntityMetadata> GetEntitiesDisplayNameWithPrivileges() { MetadataPropertiesExpression entityProperties = new MetadataPropertiesExpression ( nameof(EntityMetadata.LogicalName) , nameof(EntityMetadata.DisplayName) , nameof(EntityMetadata.SchemaName) , nameof(EntityMetadata.Description) , nameof(EntityMetadata.ObjectTypeCode) , nameof(EntityMetadata.Privileges) ) { AllProperties = false }; EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = entityProperties, }; RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)_service.Execute(request); return(response.EntityMetadata.OrderBy(ent => ent.LogicalName).ToList()); }
private async Task LoadPackEntities(string[] pack) { if (_cancellationTokenSource.IsCancellationRequested) { return; } EntityQueryExpression entityQueryExpression = GetEntityQueryExpression(); entityQueryExpression.Criteria.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.In, pack)); RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; try { var service = await GetServiceAsync(); if (service == null) { return; } RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)service.Execute(request); } catch (Exception ex) { DTEHelper.WriteExceptionToLog(ex); _service = null; } }
public static string geEntityLogicalName(int ObjectTypeCode) { //http://www.dynamicscrm.blog/object-type-codes-cheat-sheet-for-dynamics-365/ //https://msdynamicscrmblog.wordpress.com/2013/07/18/entity-type-codes-in-dynamics-crm-2011/ string entityLogicalName = String.Empty; MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And); EntityFilter.Conditions.Add(new MetadataConditionExpression("ObjectTypeCode", MetadataConditionOperator.Equals, ObjectTypeCode)); MetadataPropertiesExpression mpe = new MetadataPropertiesExpression(); mpe.AllProperties = false; mpe.PropertyNames.Add("ObjectTypeCode"); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter, Properties = mpe }; RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(entityQueryExpression, null, DeletedMetadataFilters.OptionSet); if (initialRequest.EntityMetadata.Count == 1) { entityLogicalName = initialRequest.EntityMetadata[0].LogicalName; } return(entityLogicalName); }
public static List <EntityMetadata> GetStringFieldsFieldListForEntity(IOrganizationService service, string entityLogicalName) { MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And); EntityFilter.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, entityLogicalName)); MetadataConditionExpression metadataExpression = new MetadataConditionExpression("AttributeType", MetadataConditionOperator.Equals, AttributeTypeCode.String); MetadataFilterExpression AttributeFilter = new MetadataFilterExpression(LogicalOperator.And); AttributeFilter.Conditions.Add(metadataExpression); AttributeQueryExpression Attributefilters = new AttributeQueryExpression() { Criteria = AttributeFilter }; EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter, AttributeQuery = Attributefilters }; RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(service, entityQueryExpression, null, DeletedMetadataFilters.All); return(initialRequest.EntityMetadata.ToList()); }
public Result GetEntityDetials(MetadataConditionExpression expression) { try { logSystem.CreateLog(System.Reflection.MethodBase.GetCurrentMethod().ToString() + " is called"); MetadataFilterExpression entityFilter = new MetadataFilterExpression(LogicalOperator.And); entityFilter.Conditions.Add(expression); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = entityFilter }; RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, ClientVersionStamp = null }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)xrmService.Execute(retrieveMetadataChangesRequest); EntityMetadata entityMetadata = (EntityMetadata)response.EntityMetadata[0]; return(new Result(false, string.Empty, entityMetadata, logSystem)); } catch (Exception ex) { return(new Result(true, MethodBase.GetCurrentMethod().ToString() + " : " + ExceptionHandler.HandleException(ex), null, logSystem)); } }
public static List <ComboBoxEntities> GetListEntities(IOrganizationService service) { MetadataPropertiesExpression EntityProperties = new MetadataPropertiesExpression() { AllProperties = false }; EntityProperties.PropertyNames.AddRange(new string[] { "IsAuditEnabled", "DisplayName", "LogicalName", "ObjectTypeCode" }); MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And); EntityFilter.Conditions.Add(new MetadataConditionExpression("IsIntersect", MetadataConditionOperator.Equals, false)); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = EntityProperties, }; RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(service, entityQueryExpression, null, DeletedMetadataFilters.OptionSet); List <ComboBoxEntities> lsEntities = initialRequest.EntityMetadata.Where(f => f.DisplayName.UserLocalizedLabel != null).Select(x => new ComboBoxEntities() { DisplayName = x.DisplayName.UserLocalizedLabel.Label, IsAuditEnabled = x.IsAuditEnabled.Value, ObjectTypeCode = x.ObjectTypeCode.Value, LogicalName = x.LogicalName }).OrderBy(o => o.DisplayName).ToList(); return(lsEntities); }
public void RetrieveMetadataChangesRequest(string name, object callback, string errorhandler) { RetrieveMetadataChangesResponse r = Service.Execute(new Microsoft.Xrm.Sdk.Messages.RetrieveMetadataChangesRequest { Query = new Microsoft.Xrm.Sdk.Metadata.Query.EntityQueryExpression() { Criteria = new Microsoft.Xrm.Sdk.Metadata.Query.MetadataFilterExpression { } } }) as RetrieveMetadataChangesResponse; string result = Newtonsoft.Json.JsonConvert.SerializeObject(r.EntityMetadata .Select(x => new { x.SchemaName, x.EntitySetName, x.LogicalName, x.ObjectTypeCode, x.DisplayName })); Action <string> asynccallback = delegate(string response) { callback.GetType().InvokeMember(string.Empty, BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, callback, new object[] { result }); }; Task.Run(delegate { this.Process(asynccallback, result); }); }
public static string GetEntityNameFromEtc(int etc, IOrganizationService service) { EntityQueryExpression metadataQuery = new EntityQueryExpression(); metadataQuery.Properties = new MetadataPropertiesExpression(); metadataQuery.Properties.PropertyNames.Add("LogicalName"); metadataQuery.Criteria.Conditions.Add(new MetadataConditionExpression("ObjectTypeCode", MetadataConditionOperator.Equals, etc)); var retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest { Query = metadataQuery, ClientVersionStamp = null, DeletedMetadataFilters = DeletedMetadataFilters.OptionSet }; RetrieveMetadataChangesResponse response = service.Execute(retrieveMetadataChangesRequest) as RetrieveMetadataChangesResponse; if (response.EntityMetadata.Count > 0) { return(response.EntityMetadata[0].LogicalName); } else { throw new Exception(string.Format("Entity with Object Type Code '{0}' couldn't be found", etc)); } }
/// <summary> /// Retrieves all OOB or Custom Entities Metadata depending on isOOB's value (True or False). /// </summary> /// <param name="serviceClient"></param> /// <param name="includedEntities"></param> /// <param name="isOOB"></param> /// <returns></returns> public static EntityMetadata[] RetrieveEntityMetadata(CrmServiceClient serviceClient, string[] includedEntities, bool isOOB) { MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And); if (includedEntities != null && includedEntities.Length > 0) // If the list of included entities does contain entities { EntityFilter.Conditions.Add(new MetadataConditionExpression("SchemaName", MetadataConditionOperator.In, includedEntities)); } else // Otherwise we need to verify the OOB flag to decide which type of entities to return (OOB or Custom) { if (!isOOB) // Return Custom Entities (Object Type Code >= 10000) { EntityFilter.Conditions.Add(new MetadataConditionExpression("ObjectTypeCode", MetadataConditionOperator.GreaterThan, 9999)); } } // An entity query expression to combine the filter expressions and property expressions for the query. EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter }; RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)serviceClient.Execute(request); return(response.EntityMetadata.ToArray()); }
private List <EntityMetadata> GetEntitiesProperties(string entityName, int?entityTypeCode, params string[] properties) { MetadataPropertiesExpression entityProperties = new MetadataPropertiesExpression(properties) { AllProperties = false }; EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = entityProperties, AttributeQuery = new AttributeQueryExpression() { Properties = new MetadataPropertiesExpression ( nameof(AttributeMetadata.LogicalName) , nameof(AttributeMetadata.AttributeType) , nameof(AttributeMetadata.IsValidForRead) ), }, }; if (!string.IsNullOrEmpty(entityName) || entityTypeCode.HasValue) { var criteria = new MetadataFilterExpression(LogicalOperator.Or); if (!string.IsNullOrEmpty(entityName)) { criteria.Conditions.Add(new MetadataConditionExpression(nameof(EntityMetadata.LogicalName), MetadataConditionOperator.Equals, entityName)); } if (entityTypeCode.HasValue) { criteria.Conditions.Add(new MetadataConditionExpression(nameof(EntityMetadata.ObjectTypeCode), MetadataConditionOperator.Equals, entityTypeCode.Value)); } entityQueryExpression.Criteria = criteria; } RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)_service.Execute(request); if (response.EntityMetadata.Any()) { return(response.EntityMetadata.OrderBy(ent => ent.LogicalName).ToList()); } else if (!string.IsNullOrEmpty(entityName) || entityTypeCode.HasValue) { return(GetEntitiesProperties(null, null, properties)); } return(new List <EntityMetadata>()); }
public void Experiment_For_Saving_Entity_Metadata_To_File(string entityName) { var connectionString = ConfigurationManager.ConnectionStrings["CrmOrganisation"]; var serviceProvider = new CrmServiceProvider(new ExplicitConnectionStringProviderWithFallbackToConfig() { OrganisationServiceConnectionString = connectionString.ConnectionString }, new CrmClientCredentialsProvider()); var orgService = serviceProvider.GetOrganisationService(); using (orgService as IDisposable) { MetadataFilterExpression entityFilter = new MetadataFilterExpression(LogicalOperator.And); entityFilter.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, entityName)); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = entityFilter, Properties = new MetadataPropertiesExpression() { AllProperties = true } }; RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, ClientVersionStamp = null }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)orgService.Execute(retrieveMetadataChangesRequest); var entityMetadata = response.EntityMetadata[0]; var path = Environment.CurrentDirectory; var shortFileName = entityName + "Metadata.xml"; var fileName = System.IO.Path.Combine(path, shortFileName); var serialised = EntityMetadataUtils.SerializeMetaData(entityMetadata, System.Xml.Formatting.Indented); using (var writer = new System.IO.StreamWriter(fileName)) { writer.Write(serialised); writer.Flush(); writer.Close(); } if (!File.Exists(fileName)) { throw new FileNotFoundException("Could not save metadata file for entity " + entityName); } } }
public bool QueryAttributeDisplayNamesForTwoEntities() { MetadataQueryBuilder builder = new MetadataQueryBuilder(); builder.AddEntities(new List <string>("account", "contact"), new List <string>("Attributes", "DisplayName", "DisplayCollectionName")); builder.AddAttributes(new List <string>("name", "firstname", "statecode", "statuscode"), new List <string>("DisplayName")); RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)OrganizationServiceProxy.Execute(builder.Request); return(true); }
public static void QueryAllEntities() { string _timestamp = ""; int DaysSinceModified = 0; var spinner = new Spinner(5, 0); Console.ForegroundColor = ConsoleColor.Green; spinner.Start(); EntityMetadata md; if (_ClientVersionStamp != null) { _timestamp = _ClientVersionStamp; } else { try { _timestamp = GetStoredTimestamp(); _ClientVersionStamp = _timestamp; Console.WriteLine("Stored timestamp found: " + _timestamp); }catch (Exception e) { Console.WriteLine("Error getting stored timestamp: " + e.Message); } } var EntityFilter = new MetadataFilterExpression(LogicalOperator.And); //EntityFilter.Conditions.Add(new MetadataConditionExpression("SchemaName", MetadataConditionOperator.Equals, "ServiceAppointment")); var entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter }; RetrieveMetadataChangesRequest req = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, ClientVersionStamp = _timestamp }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)service.Execute(req); spinner.Stop(); Console.WriteLine("\n Query Completed, presenting findings... \n"); Console.ForegroundColor = ConsoleColor.White; StoreChangedMetaData(response.DeletedMetadata, response.EntityMetadata); Console.WriteLine("\n===END==="); Console.ForegroundColor = ConsoleColor.White; }
public bool QueryNameAttributeForAccount() { MetadataQueryBuilder builder = new MetadataQueryBuilder(); builder.AddEntities(new List <string>("account"), new List <string>("PrimaryNameAttribute")); RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)OrganizationServiceProxy.Execute(builder.Request); Assert.AreEqual(response.EntityMetadata[0].PrimaryNameAttribute, "name"); return(true); }
private List <EntityMetadata> GetEntitiesWithAttributes() { MetadataPropertiesExpression entityProperties = new MetadataPropertiesExpression ( nameof(EntityMetadata.LogicalName) , nameof(EntityMetadata.DisplayName) , nameof(EntityMetadata.SchemaName) , nameof(EntityMetadata.Description) , nameof(EntityMetadata.DisplayCollectionName) , nameof(EntityMetadata.ObjectTypeCode) , nameof(EntityMetadata.IsIntersect) , nameof(EntityMetadata.IsCustomEntity) , nameof(EntityMetadata.Attributes) , nameof(EntityMetadata.OwnershipType) ) { AllProperties = false, }; FillAdditionalEntityProperties(_service, entityProperties); MetadataPropertiesExpression attributeProperties = new MetadataPropertiesExpression ( nameof(AttributeMetadata.LogicalName) , nameof(AttributeMetadata.AttributeOf) , nameof(AttributeMetadata.EntityLogicalName) , nameof(AttributeMetadata.SchemaName) , nameof(AttributeMetadata.DisplayName) , nameof(EnumAttributeMetadata.OptionSet) ) { AllProperties = false }; EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = entityProperties, AttributeQuery = new AttributeQueryExpression() { Properties = attributeProperties }, }; RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)_service.Execute(request); return(response.EntityMetadata.OrderBy(ent => ent.LogicalName).ToList()); }
public static List <EntityMetadata> GetListFieldsForEntity(IOrganizationService service, string entityLogicalName) { MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And); EntityFilter.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, entityLogicalName)); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter, }; RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(service, entityQueryExpression, null, DeletedMetadataFilters.All); return(initialRequest.EntityMetadata.ToList()); }
private EntityMetadata GetEntityMetadataWithAttributes(string entityName) { MetadataFilterExpression entityFilter = new MetadataFilterExpression(LogicalOperator.And); entityFilter.Conditions.Add(new MetadataConditionExpression(nameof(EntityMetadata.LogicalName), MetadataConditionOperator.Equals, entityName)); MetadataPropertiesExpression entityProperties = new MetadataPropertiesExpression ( nameof(EntityMetadata.LogicalName) , nameof(EntityMetadata.DisplayName) , nameof(EntityMetadata.SchemaName) , nameof(EntityMetadata.Description) , nameof(EntityMetadata.PrimaryIdAttribute) , nameof(EntityMetadata.PrimaryNameAttribute) , nameof(EntityMetadata.ObjectTypeCode) , nameof(EntityMetadata.IsIntersect) , nameof(EntityMetadata.IsCustomEntity) , nameof(EntityMetadata.Attributes) ) { AllProperties = false, }; FillAdditionalEntityProperties(_service, entityProperties); MetadataPropertiesExpression attributeProperties = new MetadataPropertiesExpression() { AllProperties = true, }; EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = entityProperties, AttributeQuery = new AttributeQueryExpression() { Properties = attributeProperties }, Criteria = entityFilter, }; RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)_service.Execute(request); return(response.EntityMetadata.SingleOrDefault()); }
private List <EntityMetadata> GetEntitiesWithAttributesFull(IEnumerable <string> entityList) { MetadataPropertiesExpression entityProperties = new MetadataPropertiesExpression ( nameof(EntityMetadata.LogicalName) , nameof(EntityMetadata.DisplayName) , nameof(EntityMetadata.SchemaName) , nameof(EntityMetadata.Description) , nameof(EntityMetadata.DisplayCollectionName) , nameof(EntityMetadata.OwnershipType) , nameof(EntityMetadata.ObjectTypeCode) , nameof(EntityMetadata.IsIntersect) , nameof(EntityMetadata.IsCustomEntity) , nameof(EntityMetadata.Attributes) ) { AllProperties = false, }; FillAdditionalEntityProperties(_service, entityProperties); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = entityProperties, AttributeQuery = new AttributeQueryExpression() { Properties = new MetadataPropertiesExpression() { AllProperties = true, }, }, Criteria = new MetadataFilterExpression(LogicalOperator.And) { Conditions = { new MetadataConditionExpression(nameof(EntityMetadata.LogicalName), MetadataConditionOperator.In, entityList.ToArray()), }, }, }; RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)_service.Execute(request); return(response.EntityMetadata.OrderBy(ent => ent.LogicalName).ToList()); }
/// <summary> /// Alternate keys may not be active immediately after the ChangeTrackingSample /// solution is installed.This method polls the metadata for the sample_book /// entity to delay execution of the rest of the sample until the alternate keys are ready. /// </summary> /// <param name="service">Specifies the service to connect to.</param> /// <param name="entityLogicalName">The entity logical name, i.e. sample_product.</param> private static void WaitForEntityAndKeysToBeActive(IOrganizationService service, string entityLogicalName) { EntityQueryExpression entityQuery = new EntityQueryExpression(); entityQuery.Criteria = new MetadataFilterExpression(LogicalOperator.And) { Conditions = { { new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, entityLogicalName) } } }; entityQuery.Properties = new MetadataPropertiesExpression("Keys"); RetrieveMetadataChangesRequest metadataRequest = new RetrieveMetadataChangesRequest() { Query = entityQuery }; bool allKeysReady = false; do { System.Threading.Thread.Sleep(5000); Console.WriteLine("Check for Entity..."); RetrieveMetadataChangesResponse metadataResponse = (RetrieveMetadataChangesResponse)service.Execute(metadataRequest); if (metadataResponse.EntityMetadata.Count > 0) { EntityKeyMetadata[] keys = metadataResponse.EntityMetadata[0].Keys; allKeysReady = true; if (keys.Length == 0) { Console.WriteLine("No Keys Found!!!"); allKeysReady = false; } else { foreach (var key in keys) { Console.WriteLine(" Key {0} status {1}", key.SchemaName, key.EntityKeyIndexStatus); allKeysReady = allKeysReady && (key.EntityKeyIndexStatus == EntityKeyIndexStatus.Active); } } } } while (!allKeysReady); Console.WriteLine("Waiting 30 seconds for metadata caches to all synchronize..."); System.Threading.Thread.Sleep(TimeSpan.FromSeconds(30)); }
public static List <EntityMetadata> GetEntitiesWithAuditListEnabledOrDisabled(IOrganizationService service, bool auditEnabled) { MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And); EntityFilter.Conditions.Add(new MetadataConditionExpression("IsAuditEnabled", MetadataConditionOperator.Equals, auditEnabled)); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter }; RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(service, entityQueryExpression, null, DeletedMetadataFilters.All); return(initialRequest.EntityMetadata.ToList()); }
private async Task GetListEntityHeaderDataAsync(HashSet <string> hashEntities) { if (_cancellationTokenSource.IsCancellationRequested) { return; } var service = await GetServiceAsync(); if (service == null) { return; } EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = new MetadataPropertiesExpression(_entityMetadataShortFieldsToQuery), }; if (hashEntities.Any()) { entityQueryExpression.Criteria.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.NotIn, hashEntities.ToArray())); } RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; try { RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)service.Execute(request); lock (syncObjectTaskListEntityHeader) { _taskListEntityHeaderCompleted = true; } } catch (Exception ex) { DTEHelper.WriteExceptionToLog(ex); _service = null; } Task.Run(GetEntitiesFullInfoAsync, _cancellationTokenSource.Token); //Task.Run(() => GetEntitiesFullInfoOneByOneAsync(entities), _cancellationTokenSource.Token); }
internal override OrganizationResponse Execute(OrganizationRequest orgRequest, EntityReference userRef) { var request = MakeRequest <RetrieveMetadataChangesRequest>(orgRequest); var q = request.Query; var c = q.Criteria.Conditions.First(); var m = metadata.EntityMetadata.Where(x => x.Value.ObjectTypeCode.Value == (int)c.Value); var resp = new RetrieveMetadataChangesResponse(); var col = new EntityMetadataCollection(); col.Add(m.Single().Value); resp.Results["EntityMetadata"] = col; return(resp); }
/// <summary> /// Query the Metadata to get the Entity Schema Name from the Object Type Code /// </summary> /// <param name="ObjectTypeCode"></param> /// <param name="service"></param> /// <returns>Entity Schema Name</returns> public string sGetEntityNameFromCode(string ObjectTypeCode, IOrganizationService service) { MetadataFilterExpression entityFilter = new MetadataFilterExpression(LogicalOperator.And); entityFilter.Conditions.Add(new MetadataConditionExpression("ObjectTypeCode", MetadataConditionOperator.Equals, Convert.ToInt32(ObjectTypeCode))); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = entityFilter }; RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, ClientVersionStamp = null }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)service.Execute(retrieveMetadataChangesRequest); EntityMetadata entityMetadata = (EntityMetadata)response.EntityMetadata[0]; return entityMetadata.SchemaName.ToLower(); }
public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) { RetrieveMetadataChangesResponse response = new RetrieveMetadataChangesResponse(); EntityMetadataCollection metadataCollection = new EntityMetadataCollection(); EntityMetadata entityMetadata = new EntityMetadata { LogicalName = _logicalName }; metadataCollection.Add(entityMetadata); ParameterCollection results = new ParameterCollection { { "EntityMetadata", metadataCollection } }; response.Results = results; return(response); }
private List <EntityMetadata> GetEntitiesWithAttributesForAudit() { MetadataPropertiesExpression entityProperties = new MetadataPropertiesExpression ( nameof(EntityMetadata.LogicalName) , nameof(EntityMetadata.IsAuditEnabled) , nameof(EntityMetadata.Attributes) ) { AllProperties = false, }; FillAdditionalEntityProperties(_service, entityProperties); MetadataPropertiesExpression attributeProperties = new MetadataPropertiesExpression ( nameof(AttributeMetadata.LogicalName) , nameof(AttributeMetadata.EntityLogicalName) , nameof(AttributeMetadata.IsAuditEnabled) , nameof(AttributeMetadata.AttributeOf) ) { AllProperties = false }; EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Properties = entityProperties, AttributeQuery = new AttributeQueryExpression() { Properties = attributeProperties }, }; RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)_service.Execute(request); return(response.EntityMetadata.OrderBy(ent => ent.LogicalName).ToList()); }
public bool QueryAllMetaData() { RetrieveMetadataChangesRequest request = new RetrieveMetadataChangesRequest(); request.Query = new EntityQueryExpression(); request.Query.Criteria = new MetadataFilterExpression(); request.Query.Criteria.FilterOperator = LogicalOperator.Or; request.Query.Criteria.Conditions = new List <MetadataConditionExpression>(); // Which entitiy to return MetadataConditionExpression condition = new MetadataConditionExpression(); condition.ConditionOperator = MetadataConditionOperator.Equals; condition.PropertyName = "LogicalName"; condition.Value = "account"; request.Query.Criteria.Conditions.Add(condition); request.Query.Properties = new MetadataPropertiesExpression(); request.Query.Properties.PropertyNames = new List <string>("Attributes"); // Attribute Query Properties - Which Properties to return AttributeQueryExpression attributeQuery = new AttributeQueryExpression(); attributeQuery.Properties = new MetadataPropertiesExpression(); attributeQuery.Properties.PropertyNames = new List <string>("OptionSet"); //attributeQuery.Properties.AllProperties = true; request.Query.AttributeQuery = attributeQuery; // Attribute Query Criteria - Which Attributes to return MetadataFilterExpression critiera = new MetadataFilterExpression(); attributeQuery.Criteria = critiera; critiera.FilterOperator = LogicalOperator.And; critiera.Conditions = new List <MetadataConditionExpression>(); RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)OrganizationServiceProxy.Execute(request); return(true); }
private void SerialiseEntities(RetrieveMetadataChangesResponse metadataRequest) { var serialiser = new DataContractSerializer(typeof(AttributeMetadata)); // Serialise each attribute and relationship foreach (var entity in metadataRequest.EntityMetadata) { var entityFolder = GetFolder(_rootFolderPath, entity.LogicalName); _lookupAttributes = new Dictionary <string, AttributeMetadata>(); foreach (var attribute in entity.Attributes) { if (attribute.DeprecatedVersion != null) { continue; } if (attribute.IsLogical == true) { continue; } if (IsAttributeIgnored(attribute.LogicalName)) { continue; } if (attribute.AttributeType == AttributeTypeCode.Lookup) { // Don't output here - output as part of the relationship metadata _lookupAttributes.Add(attribute.LogicalName, attribute); continue; } var xmlOutput = SerialiseToStringBuilder(serialiser, attribute); var attributeFilePath = Path.Combine(entityFolder, attribute.LogicalName + ".xml"); StripAndWriteXml(xmlOutput, attributeFilePath); } // Output the relationship xml SerialiseRelationships(entity, entityFolder); } }
/// <summary> /// エンティティのフィールドを取得します。 /// </summary> /// <param name="entityName"></param> /// <returns></returns> public AttributeMetadata[] getAttributes(string entityName) { MetadataFilterExpression EntityFilter = new MetadataFilterExpression(); EntityFilter.Conditions.Add(new MetadataConditionExpression( "LogicalName", MetadataConditionOperator.Equals, entityName)); MetadataPropertiesExpression EntityProperties = new MetadataPropertiesExpression() { AllProperties = true }; MetadataPropertiesExpression AttributeProperties = new MetadataPropertiesExpression() { AllProperties = true }; LabelQueryExpression labelQuery = new LabelQueryExpression(); labelQuery.FilterLanguages.Add(1041); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = EntityFilter, // エンティティのフィルター Properties = EntityProperties, // エンティティのプロパティ指定 AttributeQuery = new AttributeQueryExpression() { Properties = AttributeProperties // フィールドのプロパティの指定 }, LabelQuery = labelQuery // 表示言語の指定 }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)_service.Execute( new RetrieveMetadataChangesRequest() { Query = entityQueryExpression } ); return(response.EntityMetadata[0].Attributes); }
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 Experiment_For_Selecting_Entity_Metadata() { var connectionString = ConfigurationManager.ConnectionStrings["CrmOrganisation"]; var serviceProvider = new CrmServiceProvider(new ExplicitConnectionStringProviderWithFallbackToConfig() { OrganisationServiceConnectionString = connectionString.ConnectionString }, new CrmClientCredentialsProvider()); var orgService = serviceProvider.GetOrganisationService(); using (orgService as IDisposable) { MetadataFilterExpression entityFilter = new MetadataFilterExpression(LogicalOperator.And); // entityFilter.Conditions.Add(new MetadataConditionExpression("OneToManyRelationships", MetadataConditionOperator.NotEquals, null)); var relationShipQuery = new RelationshipQueryExpression(); MetadataFilterExpression relationShipFilter = new MetadataFilterExpression(LogicalOperator.And); relationShipFilter.Conditions.Add(new MetadataConditionExpression("RelationshipType", MetadataConditionOperator.Equals, RelationshipType.OneToManyRelationship)); relationShipQuery.Criteria = relationShipFilter; var props = new MetadataPropertiesExpression(); props.AllProperties = false; props.PropertyNames.Add("OneToManyRelationships"); EntityQueryExpression entityQueryExpression = new EntityQueryExpression() { Criteria = entityFilter, Properties = props, RelationshipQuery = relationShipQuery }; RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest() { Query = entityQueryExpression, ClientVersionStamp = null }; RetrieveMetadataChangesResponse response = (RetrieveMetadataChangesResponse)orgService.Execute(retrieveMetadataChangesRequest); } }
/// <summary> /// Evaluate the expressions and set their values based on the metadata response /// </summary> /// <param name="expressions"></param> /// <param name="metadataResponse"></param> /// <param name="service"></param> private static void SetValues(List<MetadataExpression> expressions, RetrieveMetadataChangesResponse metadataResponse, IOrganizationService service) { string formatString = ""; foreach (var expr in expressions) { if (expr.ExpressionType == TokenExpressionType.EntityMetadata) { var entityMetadata = metadataResponse.EntityMetadata.Where(m => m.LogicalName == expr.Entity).FirstOrDefault(); if (entityMetadata == null) continue; string propertyName = expr.PropertyName; try { if (expr.Attribute == MetadataExpression.EntityProperties) { // Get properties from entity object value = entityMetadata.GetType().GetProperty(expr.PropertyName).GetValue(entityMetadata); expr.SerialiseValue(value); } else { // Get property from attribute var attributeMetadata = entityMetadata.Attributes.Where(m => m.LogicalName == expr.Attribute).FirstOrDefault(); object value = attributeMetadata.GetType().GetProperty(expr.PropertyName).GetValue(attributeMetadata); expr.SerialiseValue(value); } if (!string.IsNullOrWhiteSpace(formatString)) expr.Value = String.Format(formatString, expr.Value, expr.Entity, expr.Attribute, expr.PropertyName); } catch (Exception ex) { expr.SerialiseValue(ex.ToString()); } } else if (expr.ExpressionType==TokenExpressionType.Function) { switch (expr.FunctionName.ToLower()) { case "fetch": try { // Execute the fetchxml FetchExpression query = new FetchExpression(expr.Paramaters); var response = service.RetrieveMultiple(query); expr.SerialiseValue(response); } catch (Exception ex) { expr.SerialiseValue(ex.Message); } break; case "format": // Use a format string formatString = expr.Paramaters; break; } } } }