private async IAsyncEnumerable <T> GetAllEntitiesInstance(IOrganizationServiceAsync2 service, QueryExpression qe, int?maxCount, int?pageSize, [EnumeratorCancellation] CancellationToken token) { var page = qe.PageInfo; ConditionallySetPageCount(maxCount, pageSize, page); page.PageNumber = 1; page.PagingCookie = null; EntitiesWithCookie <T> response; do { response = await GetEntitiesWithCookie(service, qe, token); UpdatePageCount(page, maxCount); page.PageNumber++; page.PagingCookie = response.Cookie; foreach (var entity in response.Entities) { yield return(entity); } } while (response.MoreRecords && response.Entities != null && (maxCount == null || maxCount.Value <= _totalRetrievedCount)); // No more records on server, return whatever has been received if (response.Entities == null) { yield break; } foreach (var entity in response.Entities) { yield return(entity); } }
private static async Task <bool> DeleteIfExistsWithRetryAsync(IOrganizationServiceAsync2 service, string entityName, Guid id, int retryCount, CancellationToken token) { bool exists; try { exists = await DeleteIfExistsInternalAsync(service, entityName, id, token); } catch (System.ServiceModel.FaultException <OrganizationServiceFault> ex) { if (retryCount < 10 && ex.Message.Equals("Generic SQL error.", StringComparison.CurrentCultureIgnoreCase)) { // This is normally caused by database deadlock issue. // Attempt to reprocess once after sleeping a random number of milliseconds await Task.Delay(new Random(Thread.CurrentThread.ManagedThreadId). Next(1000, 5000), token); exists = await DeleteIfExistsWithRetryAsync(service, entityName, id, retryCount + 1, token); } else if (ex.Message.EndsWith(id + " Does Not Exist")) { exists = false; } else if (ex.Message == "The object you tried to delete is associated with another object and cannot be deleted.") { throw new Exception("Entity " + entityName + " (" + id + ") is associated with another object and cannot be deleted."); } else { throw; } } return(exists); }
/// <summary> /// Gets all Entities where the columnNameAndValue Pairs match /// </summary> /// <typeparam name="T">Type of Entity List to return</typeparam> /// <param name="service"></param> /// <param name="anonymousTypeInitializer">An Anonymous Type Initializer where the properties of the anonymous /// type are the column names to add</param> /// <param name="columnNameAndValuePairs">List of pairs that look like this: /// (string name of the column, value of the column) ie. "name", "John Doe" </param> /// <returns></returns> public static IAsyncEnumerable <T> GetAllEntitiesAsync <T>(this IOrganizationServiceAsync2 service, Expression <Func <T, object> > anonymousTypeInitializer, params object[] columnNameAndValuePairs) where T : Entity { var columnSet = new ColumnSet(anonymousTypeInitializer.GetAttributeNamesArray()); return(service.GetAllEntitiesAsync <T>(columnSet, columnNameAndValuePairs)); }
/// <summary> /// Gets the first entity that matches the query expression. Null is returned if none are found. /// </summary> /// <typeparam name="T">The Entity Type.</typeparam> /// <param name="service">The service.</param> /// <param name="id">Id of the entity to search for.</param> /// <param name="anonymousTypeInitializer">An Anonymous Type Initializer where the properties of the anonymous /// type are the column names to add.</param> /// <param name="token"></param> /// <returns></returns> public static Task <T> GetEntityOrDefaultAsync <T>(this IOrganizationServiceAsync2 service, Guid id, Expression <Func <T, object> > anonymousTypeInitializer = null, CancellationToken token = default) where T : Entity { var idName = EntityHelper.GetIdAttributeName <T>(); return(anonymousTypeInitializer == null ? service.GetFirstOrDefaultAsync <T>(token, idName, id) : service.GetFirstOrDefaultAsync(anonymousTypeInitializer, token, idName, id)); }
/// <summary> /// Gets the entity by id. Null is returned if it isn't found. /// </summary> /// <param name="service">The service.</param> /// <param name="logicalName">Logical name of the entity.</param> /// <param name="id">Id of the entity to search for.</param> /// <param name="columnSet">Columns to retrieve.</param> /// <param name="token"></param> /// <returns></returns> public static Task <Entity> GetEntityOrDefaultAsync(this IOrganizationServiceAsync2 service, string logicalName, Guid id, ColumnSet columnSet = null, CancellationToken token = default) { var idName = EntityHelper.GetIdAttributeName(logicalName); return(columnSet == null ? service.GetFirstOrDefaultAsync(logicalName, token, idName, id) : service.GetFirstOrDefaultAsync(logicalName, columnSet, token, idName, id)); }
/// <summary> /// Assigns the supplied entity to the supplied user /// </summary> /// <param name="service"></param> /// <param name="target"></param> /// <param name="newOwner"></param> /// <param name="token"></param> public static Task AssignAsync(this IOrganizationServiceAsync2 service, EntityReference target, EntityReference newOwner, CancellationToken token = default) { return(service.UpdateAsync(new Entity(target.LogicalName) { Id = target.Id, ["ownerid"] = newOwner }, token)); }
/// <summary> /// Gets all Active Entities (with the given subset of columns only) /// where the values are in the columnName /// </summary> /// <typeparam name="T">Type of Entity List to return.</typeparam> /// <param name="service"></param> /// <param name="anonymousTypeInitializer">An Anonymous Type Initializer where the properties of the anonymous /// type are the column names to add.</param> /// <param name="columnName">The name of the column to perform the in against.</param> /// <param name="values">The list of values to search for being in the column name.</param> /// <param name="token"></param> /// <returns></returns> public static IAsyncEnumerable <T> GetAllEntitiesInAsync <T>(this IOrganizationServiceAsync2 service, Expression <Func <T, object> > anonymousTypeInitializer, string columnName, IEnumerable values, CancellationToken token) where T : Entity { var columnSet = new ColumnSet(anonymousTypeInitializer.GetAttributeNamesArray()); return(service.GetAllEntitiesInAsync <T>(columnSet, columnName, values, token)); }
/// <summary> /// Delete all active entities in the entity specified by the LogicalName and the Filter Expression /// </summary> /// <param name="service">The service.</param> /// <param name="logicalName">The logical name of the entity that will be deleted.</param> /// <param name="fe">The filter expression to use to determine what records to delete.</param> /// <param name="token"></param> /// <returns></returns> public static Task <bool> DeleteIfExistsAsync(this IOrganizationServiceAsync2 service, string logicalName, FilterExpression fe, CancellationToken token = default) { var qe = new QueryExpression(logicalName) { Criteria = fe }; return(service.DeleteIfExistsAsync(qe, token)); }
private async Task <EntitiesWithCookie <T> > GetEntitiesWithCookie(IOrganizationServiceAsync2 service, QueryExpression qe, CancellationToken token) { var response = await service.RetrieveMultipleAsync(qe, token); return(new EntitiesWithCookie <T> { Entities = response.Entities.Select(e => e.AsEntity <T>()), Cookie = response.PagingCookie, MoreRecords = response.MoreRecords }); }
/// <summary> /// Associates one or more entities to an entity. /// </summary> /// <param name="service"></param> /// <param name="entity"></param> /// <param name="relationshipLogicalName"></param> /// <param name="token"></param> /// <param name="entities"></param> public static Task AssociateAsync(this IOrganizationServiceAsync2 service, Entity entity, string relationshipLogicalName, CancellationToken token = default, params Entity[] entities) { var relationship = new Relationship(relationshipLogicalName); if (entity.LogicalName == entities.First().LogicalName) { relationship.PrimaryEntityRole = EntityRole.Referenced; } return(service.AssociateAsync(entity.LogicalName, entity.Id, relationship, new EntityReferenceCollection(entities.Select(e => e.ToEntityReference()).ToList()), token)); }
/// <summary> /// Delete all entities that are returned by the QueryExpression. /// </summary> /// <param name="service">The Service</param> /// <param name="qe">The query expression used to define the set of entities to delete</param> /// <param name="token"></param> /// <returns></returns> public static async Task <bool> DeleteIfExistsAsync(this IOrganizationServiceAsync2 service, QueryExpression qe, CancellationToken token = default) { var exists = false; var idName = EntityHelper.GetIdAttributeName(qe.EntityName); qe.ColumnSet = new ColumnSet(idName); qe.NoLock = true; var entities = await service.RetrieveMultipleAsync(qe, token); if (entities.Entities.Count > 0) { exists = true; await Task.WhenAll(entities.Entities.Select(e => service.DeleteAsync(qe.EntityName, e.Id))); } return(exists); }
/// <summary> /// Attempts to delete the Entity, eating the error if it doesn't exist /// </summary> /// <param name="service">The service.</param> /// <param name="logicalName">Logical name of the entity.</param> /// <param name="id">The id.</param> /// <param name="token"></param> /// <returns></returns> public static async Task <bool> TryDeleteAsync(this IOrganizationServiceAsync2 service, string logicalName, Guid id, CancellationToken token = default) { var exists = false; try { await service.DeleteAsync(logicalName, id, token); exists = true; } catch (System.ServiceModel.FaultException <OrganizationServiceFault> ex) { if (!ex.Message.EndsWith(id + " Does Not Exist")) { throw; } } return(exists); }
private static async Task <bool> DeleteIfExistsInternalAsync(IOrganizationServiceAsync2 service, string logicalName, Guid id, CancellationToken token) { var exists = false; var idName = EntityHelper.GetIdAttributeName(logicalName); var qe = new QueryExpression(logicalName) { ColumnSet = new ColumnSet(idName) }; qe.WhereEqual(idName, id); qe.First(); qe.NoLock = true; if ((await service.RetrieveMultipleAsync(qe, token)).Entities.Count > 0) { await service.DeleteAsync(logicalName, id, token); exists = true; } return(exists); }
/// <summary> /// Gets the first 5000 active entities with the given ids. /// </summary> /// <param name="service">The IOrganizationService.</param> /// <param name="logicalName">Logical name of the entity.</param> /// <param name="ids">Ids of the entity to search for.</param> /// <param name="token"></param> /// <returns></returns> public static Task <List <Entity> > GetEntitiesByIdAsync(this IOrganizationServiceAsync2 service, string logicalName, IEnumerable <Guid> ids, CancellationToken token = default) { return(service.GetEntitiesInAsync(logicalName, token, EntityHelper.GetIdAttributeName(logicalName), ids)); }
/// <summary> /// Gets all entities using the Query Expression /// </summary> /// <typeparam name="T">Type of Entity List to return</typeparam> /// <param name="service">The service.</param> /// <param name="qe">Query Expression to Execute.</param> /// <param name="maxCount">The maximum number of entities to retrieve. Use null for default.</param> /// <param name="pageSize">Number of records to return in each fetch. Use null for default.</param> /// <param name="token">Cancellation Token</param> /// <returns></returns> public static IAsyncEnumerable <T> GetAllEntitiesAsync <T>(this IOrganizationServiceAsync2 service, QueryExpression qe, int?maxCount = null, int?pageSize = null, CancellationToken token = default) where T : Entity { return(RetrieveAllEntitiesAsync <T> .GetAllEntities(service, qe, maxCount, pageSize, token)); }
/// <summary> /// Gets first 5000 Active Entities (with the given subset of columns only) /// where the columnNameAndValue Pairs match /// </summary> /// <param name="service"></param> /// <param name="logicalName">LogicalName of the Entity.</param> /// <param name="columnSet">Columns to retrieve</param> /// <param name="token"></param> /// <param name="columnNameAndValuePairs">List of pairs that look like this: /// (string name of the column, value of the column) ie. "name", "John Doe" </param> /// <returns></returns> public static Task <List <Entity> > GetEntitiesAsync(this IOrganizationServiceAsync2 service, string logicalName, ColumnSet columnSet, CancellationToken token, params object[] columnNameAndValuePairs) { return(service.GetEntitiesAsync <Entity>(QueryExpressionFactory.Create(logicalName, columnSet, columnNameAndValuePairs), token)); }
/// <summary> /// Attempts to delete the entity with the given id. If it doesn't exist, false is returned /// </summary> /// <param name="service">The service.</param> /// <param name="entityName">Name of the entity.</param> /// <param name="id">The id of the entity to search and potentially delete.</param> /// <param name="token"></param> /// <returns></returns> public static Task <bool> DeleteIfExistsAsync(this IOrganizationServiceAsync2 service, string entityName, Guid id, CancellationToken token = default) { return(DeleteIfExistsWithRetryAsync(service, entityName, id, 0, token)); }
/// <summary> /// Gets all Active Entities (with the given subset of columns only) /// where the values are in the columnName /// </summary> /// <typeparam name="T">Type of Entity List to return.</typeparam> /// <param name="service"></param> /// <param name="columnSet">Columns to Return.</param> /// <param name="columnName">The name of the column to perform the in against.</param> /// <param name="values">The list of values to search for being in the column name.</param> /// <param name="token"></param> /// <returns></returns> public static IAsyncEnumerable <T> GetAllEntitiesInAsync <T>(this IOrganizationServiceAsync2 service, ColumnSet columnSet, string columnName, IEnumerable values, CancellationToken token = default) where T : Entity { return(RetrieveAllEntitiesAsync <T> .GetAllEntities(service, QueryExpressionFactory.CreateIn <T>(columnSet, columnName, values), token : token)); }
/// <summary> /// Gets the first 5000 active entities with the given ids. /// </summary> /// <param name="service">The IOrganizationService.</param> /// <param name="logicalName">Logical name of the entity.</param> /// <param name="ids">Ids of the entity to search for.</param> /// <returns></returns> public static Task <List <Entity> > GetEntitiesByIdAsync(this IOrganizationServiceAsync2 service, string logicalName, params Guid[] ids) { return(service.GetEntitiesByIdAsync(logicalName, default(CancellationToken), ids)); }
/// <summary> /// Gets all Active Entities where the values are in the columnName /// </summary> /// <typeparam name="T">Type of Entity List to return.</typeparam> /// <param name="service"></param> /// <param name="columnName">The name of the column to perform the in against.</param> /// <param name="values">The list of values to search for being in the column name.</param> /// <returns></returns> public static IAsyncEnumerable <T> GetAllEntitiesInAsync <T>(this IOrganizationServiceAsync2 service, string columnName, params object[] values) where T : Entity { return(service.GetAllEntitiesInAsync <T>(default(CancellationToken), columnName, values)); }
/// <summary> /// Gets all Active Entities (with the given subset of columns only) /// where the values are in the columnName /// </summary> /// <typeparam name="T">Type of Entity List to return.</typeparam> /// <param name="service"></param> /// <param name="anonymousTypeInitializer">An Anonymous Type Initializer where the properties of the anonymous /// type are the column names to add.</param> /// <param name="columnName">The name of the column to perform the in against.</param> /// <param name="values">The list of values to search for being in the column name.</param> /// <returns></returns> public static IAsyncEnumerable <T> GetAllEntitiesInAsync <T>(this IOrganizationServiceAsync2 service, Expression <Func <T, object> > anonymousTypeInitializer, string columnName, params object[] values) where T : Entity { return(service.GetAllEntitiesInAsync <T>(anonymousTypeInitializer, default, columnName, values));
/// <summary> /// Reassigns the owner of the entity to the new owner /// </summary> /// <param name="service"></param> /// <param name="itemToChangeOwnershipOf">Must have Logical Name and Id Populated</param> /// <param name="teamId"></param> /// <param name="token"></param> public static Task AssignTeamAsync(this IOrganizationServiceAsync2 service, EntityReference itemToChangeOwnershipOf, Guid teamId, CancellationToken token = default) { return(service.AssignAsync(itemToChangeOwnershipOf, new EntityReference("team", teamId), token)); }
/// <summary> /// Reassigns the owner of the entity to the new owner /// </summary> /// <param name="service"></param> /// <param name="itemToChangeOwnershipOf">Must have Logical Name and Id Populated</param> /// <param name="userId"></param> /// <param name="token"></param> public static Task AssignAsync(this IOrganizationServiceAsync2 service, Entity itemToChangeOwnershipOf, Guid userId, CancellationToken token = default) { return(service.AssignAsync(itemToChangeOwnershipOf.ToEntityReference(), new EntityReference("systemuser", userId), token)); }
/// <summary> /// Attempts to delete the entity with the given id. If it doesn't exist, false is returned /// </summary> /// <param name="service">The service.</param> /// <param name="entity">The entity to delete if it exists.</param> /// <param name="token"></param> public static Task DeleteIfExistsAsync(this IOrganizationServiceAsync2 service, Entity entity, CancellationToken token = default) { return(service.DeleteIfExistsAsync(entity.LogicalName, entity.Id, token)); }
/// <summary> /// Gets the first 5000 active entities (with the given subset of columns only) with the given ids. /// </summary> /// <param name="service">The IOrganizationService.</param> /// <param name="logicalName">Logical name of the entity.</param> /// <param name="columnSet">Columns to retrieve.</param> /// <param name="ids">Ids of the entity to search for.</param> /// <returns></returns> public static Task <List <Entity> > GetEntitiesByIdAsync(this IOrganizationServiceAsync2 service, string logicalName, ColumnSet columnSet, params Guid[] ids) { return(service.GetEntitiesInAsync(logicalName, columnSet, logicalName, default, ids));
/// <summary> /// Gets the first 5000 active entities with the given ids. /// </summary> /// <param name="service">The IOrganizationService.</param> /// <param name="logicalName">Logical name of the entity.</param> /// <param name="token"></param> /// <param name="ids">Ids of the entity to search for.</param> /// <returns></returns> public static Task <List <Entity> > GetEntitiesByIdAsync(this IOrganizationServiceAsync2 service, string logicalName, CancellationToken token, params Guid[] ids) { return(service.GetEntitiesInAsync(logicalName, token, EntityHelper.GetIdAttributeName(logicalName), ids)); }
/// <summary> /// Gets all Entities where the columnNameAndValue Pairs match /// </summary> /// <typeparam name="T">Type of Entity List to return</typeparam> /// <param name="service"></param> /// <param name="columnSet">Columns to retrieve</param> /// <param name="columnNameAndValuePairs">List of pairs that look like this: /// (string name of the column, value of the column) ie. "name", "John Doe" </param> /// <returns></returns> public static IAsyncEnumerable <T> GetAllEntitiesAsync <T>(this IOrganizationServiceAsync2 service, ColumnSet columnSet, params object[] columnNameAndValuePairs) where T : Entity { return(service.GetAllEntitiesAsync(QueryExpressionFactory.Create <T>(columnSet, columnNameAndValuePairs))); }
/// <summary> /// Gets all Entities where the columnNameAndValue Pairs match /// </summary> /// <param name="service"></param> /// <param name="logicalName">LogicalName of the Entity.</param> /// <param name="columnSet">Columns to retrieve</param> /// <param name="columnNameAndValuePairs">List of pairs that look like this: /// (string name of the column, value of the column) ie. "name", "John Doe" </param> /// <returns></returns> public static IAsyncEnumerable <Entity> GetAllEntitiesAsync(this IOrganizationServiceAsync2 service, string logicalName, ColumnSet columnSet, params object[] columnNameAndValuePairs) { return(service.GetAllEntitiesAsync <Entity>(QueryExpressionFactory.Create(logicalName, columnSet, columnNameAndValuePairs))); }
/// <summary> /// Gets first 5000 Active Entities (with the given subset of columns only) /// where the columnNameAndValue Pairs match /// </summary> /// <param name="service"></param> /// <param name="logicalName">LogicalName of the Entity.</param> /// <param name="columnSet">Columns to retrieve</param> /// <param name="columnNameAndValuePairs">List of pairs that look like this: /// (string name of the column, value of the column) ie. "name", "John Doe" </param> /// <returns></returns> public static Task <List <Entity> > GetEntitiesAsync(this IOrganizationServiceAsync2 service, string logicalName, ColumnSet columnSet, params object[] columnNameAndValuePairs) { return(service.GetEntitiesAsync(logicalName, columnSet, default, columnNameAndValuePairs));
/// <summary> /// Associates one or more entities to an entity. /// </summary> /// <param name="service"></param> /// <param name="entity"></param> /// <param name="relationshipLogicalName"></param> /// <param name="entities"></param> public static Task AssociateAsync(this IOrganizationServiceAsync2 service, Entity entity, string relationshipLogicalName, params Entity[] entities) { return(service.AssociateAsync(entity, relationshipLogicalName, default, entities));