Exemple #1
0
        /// <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);
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
 /// <summary>
 /// Deletes the specified entity
 /// </summary>
 /// <param name="service"></param>
 /// <param name="entity"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public static Task DeleteAsync(this IOrganizationServiceAsync2 service, EntityReference entity, CancellationToken token = default)
 {
     return(service.DeleteAsync(entity.LogicalName, entity.Id, token));
 }