Ejemplo n.º 1
0
        public ExecuteMultipleRequest GetRequestCancel(List <Entity> entities)
        {
            // Build new ExecuteMultipleSettings request from SDK
            ExecuteMultipleSettings executeSettings = new ExecuteMultipleSettings()
            {
                ContinueOnError = true,
                ReturnResponses = true
            };

            var requestWithResults = new ExecuteMultipleRequest()
            {
                Requests = new OrganizationRequestCollection(),
                Settings = executeSettings
            };

            // iterate over the passed entities to delete adding the cancel request to the execute multiple request
            foreach (Entity entity in entities)
            {
                UpdateRequest upd = new UpdateRequest();
                entity["statecode"]  = new OptionSetValue(3);
                entity["statuscode"] = new OptionSetValue(0);
                upd.Target           = entity;
                requestWithResults.Requests.Add(upd);
            }
            return(requestWithResults);
        }
Ejemplo n.º 2
0
        public ExecuteMultipleRequest GetRequestDelete(List <Entity> entities)
        {
            // Build new ExecuteMultipleSettings request from SDK
            ExecuteMultipleSettings executeSettings = new ExecuteMultipleSettings()
            {
                ContinueOnError = true,
                ReturnResponses = true
            };

            var requestWithResults = new ExecuteMultipleRequest()
            {
                Requests = new OrganizationRequestCollection(),
                Settings = executeSettings
            };

            // iterate over the passed entities to delete adding the delete request to the execute multiple request
            foreach (Entity entity in entities)
            {
                DeleteRequest del = new DeleteRequest {
                    Target = new EntityReference("asyncoperation", entity.Id)
                };
                requestWithResults.Requests.Add(del);
            }
            return(requestWithResults);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes an ExecuteMultipleRequest for a collection of requests.
        /// </summary>
        /// <param name="service">IOrganizationService.</param>
        /// <param name="requestCollection">Collection of requests to execute.</param>
        /// <param name="continueOnError">If set to <c>true</c>, continue executing requests if one encounters an error.</param>
        /// <param name="returnResponses">If set to <c>true</c>, return responses for successful requests.</param>
        /// <returns>ExecuteMultipleResponse.</returns>
        /// <exception cref="System.ArgumentException">requestCollection must be 1000 requests or less</exception>
        public static ExecuteMultipleResponse ExecuteMultiple(this IOrganizationService service, IEnumerable <OrganizationRequest> requestCollection,
                                                              bool continueOnError, bool returnResponses)
        {
            if (requestCollection.Count() > 1000)
            {
                throw new ArgumentException("requestCollection must be 1000 requests or less");
            }

            var settings = new ExecuteMultipleSettings
            {
                ContinueOnError = continueOnError,
                ReturnResponses = returnResponses
            };

            return(service.ExecuteMultiple(requestCollection, settings));
        }
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="returnResponses">True to return responses, False to not return responses</param>
 /// <param name="continueOnError">True to continue if anyone item trips an error, False to stop on the first error. </param>
 /// <param name="batchName">String name of the batch, if blank, a GUID is used</param>
 public RequestBatch(string batchName = "", bool returnResponses = true, bool continueOnError = false)
 {
     // Create the batch ID and name.
     BatchId = Guid.NewGuid();
     if (!string.IsNullOrWhiteSpace(batchName))
     {
         BatchName = batchName;
     }
     else
     {
         BatchName = string.Format(CultureInfo.InvariantCulture, "BATCH-{0}-{1}", DateTime.UtcNow.Ticks, BatchId.ToString());                  // Generate a reasonably Unique Name.
     }
     BatchRequestSettings = new ExecuteMultipleSettings()
     {
         ContinueOnError = continueOnError, ReturnResponses = returnResponses
     };
     BatchItems = new List <BatchItemOrganizationRequest>();            //new OrganizationRequestCollection();
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Executes an ExecuteMultipleRequest for a collection of requests with specified settings.
        /// </summary>
        /// <param name="service">IOrganizationService.</param>
        /// <param name="requestCollection">Collection of requests to execute.</param>
        /// <param name="requestSettings">The request settings.</param>
        /// <returns>ExecuteMultipleResponse.</returns>
        public static ExecuteMultipleResponse ExecuteMultiple(this IOrganizationService service, IEnumerable <OrganizationRequest> requestCollection,
                                                              ExecuteMultipleSettings requestSettings)
        {
            if (requestCollection.Count() > 1000)
            {
                throw new ArgumentException("requestCollection must be 1000 requests or less");
            }

            var executeCollection = new OrganizationRequestCollection();

            executeCollection.AddRange(requestCollection);

            var request = new ExecuteMultipleRequest
            {
                Settings = requestSettings,
                Requests = executeCollection
            };

            return((ExecuteMultipleResponse)service.Execute(request));
        }
Ejemplo n.º 6
0
 /// <summary>Initializes a new instance of the  <see cref="T:Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest"></see>.</summary>
 public ExecuteMultipleRequest()
 {
     this.RequestName = "ExecuteMultiple";
     this.Requests    = (OrganizationRequestCollection)null;
     this.Settings    = (ExecuteMultipleSettings)null;
 }
        /// <summary>
        /// Converts a collection of type <see cref="OrganizationRequest"/> to a single <see cref="ExecuteMultipleRequest"/> instance
        /// </summary>
        /// <typeparam name="T">The typeof<see cref="OrganizationRequest"/></typeparam>
        /// <param name="requests">The collection of requests representing the batch</param>
        /// <param name="batchSettings">The desired settings</param>
        /// <returns>A single <see cref="ExecuteMultipleRequest"/> instance</returns>
        public static ExecuteMultipleRequest AsBatch <T>(this IEnumerable <T> requests, ExecuteMultipleSettings batchSettings)
            where T : OrganizationRequest
        {
            var batch = new OrganizationRequestCollection();

            batch.AddRange(requests);

            return(new ExecuteMultipleRequest()
            {
                Requests = batch,
                Settings = batchSettings
            });
        }
        /// <summary>
        /// Converts a collection of type <see cref="OrganizationRequest"/> to <see cref="ExecuteMultipleRequest"/> batches
        /// </summary>
        /// <typeparam name="T">The typeof<see cref="OrganizationRequest"/></typeparam>
        /// <param name="requests">The collection of requests to partition into batches</param>
        /// <param name="batchSize">The size of each batch</param>
        /// <param name="batchSettings">The desired settings</param>
        /// <returns>A keyed collection of type <see cref="ExecuteMultipleRequest"/> representing the request batches</returns>
        public static IDictionary <string, ExecuteMultipleRequest> AsBatches <T>(this IEnumerable <T> requests, int batchSize, ExecuteMultipleSettings batchSettings)
            where T : OrganizationRequest
        {
            if (batchSize <= 0)
            {
                throw new ArgumentException("Batch size must be greater than 0", "batchSize");
            }
            if (batchSize > maxBatchSize)
            {
                throw new ArgumentException(String.Format("Batch size of {0} exceeds max batch size of 1000", batchSize), "batchSize");
            }
            if (batchSettings == null)
            {
                throw new ArgumentNullException("batchSettings");
            }

            // Index each request
            var indexedRequests = requests.Select((r, i) => new { Index = i, Value = r });

            // Partition the indexed requests by batch size
            var partitions = indexedRequests.GroupBy(ir => ir.Index / batchSize);

            // Convert each partition to an ExecuteMultilpleRequest batch
            IEnumerable <ExecuteMultipleRequest> batches = partitions.Select(p => p.Select(ir => ir.Value).AsBatch(batchSettings));

            // Index each batch
            var indexedBatches = batches.Select((b, i) => new { Index = i, Value = b });

            // Return indexed batches as dictionary
            return(indexedBatches.ToDictionary(ib => ib.Index.ToString(), ib => ib.Value));
        }