Ejemplo n.º 1
0
        private static void ExecuteMultipleRequests(TraceLogger logger, CrmServiceClient service,
                                                    OrganizationRequestCollection requests, int batchIndex)
        {
            var response = (ExecuteMultipleResponse)service.Execute(new ExecuteMultipleRequest
            {
                Requests = requests,
                Settings = new ExecuteMultipleSettings
                {
                    ContinueOnError = true,
                    ReturnResponses = false
                }
            });

            if (response.IsFaulted)
            {
                foreach (var item in response.Responses)
                {
                    var itemIndex = (batchIndex * QUEUEITEMBATCHSIZE) + item.RequestIndex;

                    var itemRequest = (AddToQueueRequest)requests[item.RequestIndex];

                    logger.Log(
                        $"Item {itemIndex} - There was an error queuing the item with the id:{itemRequest.Target} to queue:{itemRequest.DestinationQueueId}",
                        TraceEventType.Information);
                    logger.Log(item.Fault.Message);
                }
            }

            requests.Clear();
        }
Ejemplo n.º 2
0
        internal static void UpdateDataWithEntities(Context context, IOrganizationService service, ref List <DataLine> data)
        {
            OrganizationRequestCollection retrieveMultipleRequestsCollection = new OrganizationRequestCollection( );
            int firstDataIndex = 0;
            int dataTotalCount = data.Count;

            for (int i = 0; i < dataTotalCount; i++)
            {
                if (data [i].IsOk)
                {
                    QueryExpression queryExpression = new QueryExpression
                    {
                        TopCount  = 1,
                        ColumnSet = new ColumnSet(false)
                    };
                    foreach (var item in context.ColumnItems.Where(a => a.ColumnType == ColumnTypeCode.DynamicConditionExpression))
                    {
                        queryExpression.Criteria.AddCondition(item.LogicalName, item.ConditionOperatorCode, data [i].LineData [context.ColumnItems.IndexOf(item)]);
                    }

                    retrieveMultipleRequestsCollection.Add(new RetrieveMultipleRequest( )
                    {
                        Query = queryExpression
                    });

                    if (retrieveMultipleRequestsCollection.Count( ) >= context.BatchSize)
                    {
                        ExecuteRetrieveMultiple(service, retrieveMultipleRequestsCollection, firstDataIndex, ref data);
                        //initialisation
                        retrieveMultipleRequestsCollection.Clear( );
                        firstDataIndex = i + 1;
                    }
                }
            }
            if (retrieveMultipleRequestsCollection.Count != 0)
            {
                ExecuteRetrieveMultiple(service, retrieveMultipleRequestsCollection, firstDataIndex, ref data);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Execute Multiple Request
        /// </summary>
        /// <param name="service">Organization Service</param>
        /// <param name="crmRequestType">Request Type</param>
        /// <param name="entityCollection">Entity Collection</param>
        /// <param name="continueOnError">Continue on Error Indicator</param>
        /// <returns>Failed Record Count</returns>
        private static int ExecuteMultipleRequest(IOrganizationService service, RequestType crmRequestType, EntityCollection entityCollection, bool continueOnError)
        {
            int failCount = 0;

            if (entityCollection != null && entityCollection.Entities != null && entityCollection.Entities.Count > 0)
            {
                OrganizationRequestCollection requestCollection = new OrganizationRequestCollection();
                foreach (var entity in entityCollection.Entities)
                {
                    switch (crmRequestType)
                    {
                    case RequestType.Create:
                        requestCollection.Add(new CreateRequest {
                            Target = entity
                        });
                        break;

                    case RequestType.Update:
                        requestCollection.Add(new UpdateRequest {
                            Target = entity
                        });
                        break;
                    }

                    if (requestCollection.Count == 1000)
                    {
                        failCount = failCount + ProcessExecuteMultipleAndReturnFailCount(requestCollection, service, continueOnError);
                        requestCollection.Clear();
                    }
                }

                if (requestCollection.Count > 0)
                {
                    failCount = failCount + ProcessExecuteMultipleAndReturnFailCount(requestCollection, service, continueOnError);
                }
            }

            return(failCount);
        }
Ejemplo n.º 4
0
        private void CopyData(IOrganizationService service, AttributeMetadata from, AttributeMetadata to, Action actions)
        {
            if (!MigrateData) { return; }

            var total = GetRecordCount(service, from);
            var count = 0;

            Trace("Copying data from {0} to {1}", from.LogicalName, to.LogicalName);
            var requests = new OrganizationRequestCollection();
            // Grab from and to, and only update if not equal.  This is to speed things up if it has failed part way through
            foreach (var entity in service.GetAllEntities<Entity>(new QueryExpression(from.EntityLogicalName) { ColumnSet = new ColumnSet(from.LogicalName, to.LogicalName) }))
            {
                if (count++ % 100 == 0 || count == total)
                {
                    if (requests.Any())
                    {
                        PerformUpdates(service, requests);
                    }

                    Trace("Copying {0} / {1}", count, total);
                    requests.Clear();
                }

                var value = entity.GetAttributeValue<Object>(from.LogicalName);
                if (actions.HasFlag(Action.ChangeType) && from.GetType() != to.GetType())
                {
                    value = CopyValue(from, to, value);
                }
                var toValue = entity.GetAttributeValue<Object>(to.LogicalName);
                
                if (value != null)
                {
                    if (value.Equals(toValue)) continue;

                    entity.Attributes[to.LogicalName] = value;
                    requests.Add(new UpdateRequest { Target = entity });
                }
                else if (toValue != null)
                {
                    entity.Attributes[to.LogicalName] = null;
                    requests.Add(new UpdateRequest { Target = entity });
                }
            }

            if (requests.Any())
            {
                PerformUpdates(service, requests);
            }

            Trace("Data Migration Complete", count, total);
        }
Ejemplo n.º 5
0
        private void CopyData(IOrganizationService service, AttributeMetadata from, AttributeMetadata to, Action actions)
        {
            if (!MigrateData)
            {
                return;
            }

            var total = GetRecordCount(service, from);
            var count = 0;

            Trace("Copying data from {0} to {1}", from.LogicalName, to.LogicalName);
            var requests = new OrganizationRequestCollection();

            // Grab from and to, and only update if not equal.  This is to speed things up if it has failed part way through
            foreach (var entity in service.GetAllEntities <Entity>(new QueryExpression(from.EntityLogicalName)
            {
                ColumnSet = new ColumnSet(from.LogicalName, to.LogicalName)
            }))
            {
                if (count++ % 100 == 0 || count == total)
                {
                    if (requests.Any())
                    {
                        PerformUpdates(service, requests);
                    }

                    Trace("Copying {0} / {1}", count, total);
                    requests.Clear();
                }

                var value = entity.GetAttributeValue <Object>(from.LogicalName);
                if (actions.HasFlag(Action.ChangeType) && from.GetType() != to.GetType())
                {
                    value = CopyValue(from, to, value);
                }
                var toValue = entity.GetAttributeValue <Object>(to.LogicalName);

                if (value != null)
                {
                    if (value.Equals(toValue))
                    {
                        continue;
                    }

                    entity.Attributes[to.LogicalName] = value;
                    requests.Add(new UpdateRequest
                    {
                        Target = entity
                    });
                }
                else if (toValue != null)
                {
                    entity.Attributes[to.LogicalName] = null;
                    requests.Add(new UpdateRequest
                    {
                        Target = entity
                    });
                }
            }

            if (requests.Any())
            {
                PerformUpdates(service, requests);
            }

            Trace("Data Migration Complete", count, total);
        }