Exemple #1
0
        public void BasicRunMultipleTestParallelExecute()
        {
            CrmConnection c = new CrmConnection("CRM");

            List <IOrganizationService> services = new List <IOrganizationService>();

            for (int i = 0; i < 10; i++)
            {
                OrganizationService service = new OrganizationService(c);
                services.Add(service);
            }


            CrmBulkServiceManager mgr = new CrmBulkServiceManager(services);

            List <OrganizationRequest> requests = new List <OrganizationRequest>();

            for (int i = 0; i < 100; i++)
            {
                WhoAmIRequest req = new WhoAmIRequest();
                requests.Add(req);
            }

            var results = mgr.RunMultipleRequests(requests, batchSize: 1);
        }
Exemple #2
0
        public void BulkRunWorkflowTest1()
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            for (int i = 0; i < 10; i++)
            {
                Entity entity = new Entity("account");
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            var results = mgr.BulkInsert(entityList);

            entityList.Clear();
            foreach (var item in results.ResultItems)
            {
                Entity entity = new Entity("account");
                entity.Id      = item.ItemID;
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            var workflowID = GetWorkflowID(service, "account", "UnitTest Account");

            results = mgr.BulkRunWorkflow(entityList, workflowID);
        }
Exemple #3
0
        public void BulkSetStateTest1()
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            for (int i = 0; i < 10; i++)
            {
                Entity entity = new Entity("account");
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            var results = mgr.BulkInsert(entityList);

            entityList.Clear();
            foreach (var item in results.ResultItems)
            {
                Entity entity = new Entity("account");
                entity.Id      = item.ItemID;
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            results = mgr.BulkSetState(entityList, new OptionSetValue(1), new OptionSetValue(2));
        }
Exemple #4
0
        public void ApplyChanges(IOrganizationService targetService, string entityName, ColumnSet columns, bool reprocessFailed = true)
        {
            RetrieveEntityChangesRequest changeRequest = new RetrieveEntityChangesRequest();

            changeRequest.EntityName = entityName;
            changeRequest.Columns    = columns;
            changeRequest.PageInfo   = new PagingInfo()
            {
                Count = _DefaultPageSize, PageNumber = 1, ReturnTotalRecordCount = false
            };
            var trackEntity = RetrieveTrackingEntity(targetService, entityName);

            if (trackEntity.Contains("ctccrm_datatoken"))
            {
                changeRequest.DataVersion = trackEntity.GetAttributeValue <string>("ctccrm_datatoken");
            }

            CrmBulkServiceManager bulkMgr = new CrmBulkServiceManager(targetService);

            ChangesToToAction(changeRequest, (response) =>
            {
                List <Entity> entitiesToUpSert          = new List <Entity>();
                List <EntityReference> entitiesToDelete = new List <EntityReference>();

                PrepareUpsertDeleteLists(response, entitiesToUpSert, entitiesToDelete);

                trackEntity["ctccrm_datatoken"] = response.EntityChanges.DataToken;
                entitiesToUpSert.Add(trackEntity);
                try
                {
                    bulkMgr.BulkUpsertAndDelete(entitiesToUpSert, entitiesToDelete,
                                                transactionMode: CTCBulkTransactionMode.Single);
                }
                catch (Exception ex)
                {
                    if (changeRequest.PageInfo.Count == 1)
                    {
                        RecordFailedRecord(entityName, trackEntity, bulkMgr, entitiesToUpSert, ex);
                    }
                    else
                    {
                        throw ex;
                    }
                }
            });

            if (reprocessFailed)
            {
                ReProcessFailedForEntity(targetService, entityName, columns, bulkMgr);
            }
        }
Exemple #5
0
        private static void RunWhoAmITest(IOrganizationService service)
        {
            CrmBulkServiceManager mgr = new CrmBulkServiceManager(service);

            List <OrganizationRequest> requests = new List <OrganizationRequest>();

            for (int i = 0; i < 50; i++)
            {
                WhoAmIRequest req = new WhoAmIRequest();
                requests.Add(req);
            }

            var results = mgr.RunMultipleRequests(requests, batchSize: 5);
        }
Exemple #6
0
        private static void RunBulkInsertTest(IOrganizationService service, int recordsToInsert = 50)
        {
            CrmBulkServiceManager mgr = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            for (int i = 0; i < recordsToInsert; i++)
            {
                Entity entity = new Entity("account");
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            var results = mgr.BulkInsert(entityList, batchSize: 1000);
        }
Exemple #7
0
        private static void ExecuteExcessiveBatchSize(CTCBulkTransactionMode transactionMode = CTCBulkTransactionMode.None)
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <OrganizationRequest> requests = new List <OrganizationRequest>();

            for (int i = 0; i < 1100; i++)
            {
                WhoAmIRequest req = new WhoAmIRequest();
                requests.Add(req);
            }

            var results = mgr.RunMultipleRequests(requests, batchSize: 1100, transactionMode: transactionMode);
        }
        static void Main(string[] args)
        {
            int TotalRecordsToProcess = 1000;
            int ThreadsToUse          = 2;
            int BatchSize             = 50;

            var connectionString = ConfigurationManager.ConnectionStrings["CRM"].ConnectionString;


            List <IOrganizationService> services = new List <IOrganizationService>();

            for (int i = 0; i < ThreadsToUse; i++)
            {
                CrmServiceClient     connection = new CrmServiceClient(connectionString);
                IOrganizationService service    = connection.OrganizationServiceProxy as IOrganizationService;
                services.Add(service);
            }

            CrmBulkServiceManager mgr = new CrmBulkServiceManager(services, statusAction: LogProgress);

            List <Entity> entityList = new List <Entity>();

            for (int i = 0; i < TotalRecordsToProcess; i++)
            {
                Entity entity = new Entity("account");
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }
            Stopwatch sw = new Stopwatch();

            sw.Start();
            var results = mgr.BulkInsert(entityList, batchSize: BatchSize);

            sw.Stop();
            Console.WriteLine("Import took " + sw.ElapsedMilliseconds / 1000 + " seconds ");

            List <EntityReference> refList = new List <EntityReference>();

            foreach (var result in results.ResultItems)
            {
                var refItem = new EntityReference("account", result.ItemID);
                refList.Add(refItem);
            }
            Console.WriteLine("Cleanup Starting");
            // mgr.BulkDelete(refList,batchSize:50);
            Console.WriteLine("Cleanup completed");
        }
Exemple #9
0
        public void ApplyChanges(IOrganizationService targetService, ApplyChangesOptions[] entities, bool reprocessFailed = true)
        {
            foreach (var entity in entities)
            {
                ApplyChanges(targetService, entity.EntityName, entity.Columns, reprocessFailed: false);
            }

            if (reprocessFailed)
            {
                CrmBulkServiceManager bulkMgr = new CrmBulkServiceManager(targetService);

                foreach (var entity in entities)
                {
                    ReProcessFailedForEntity(targetService, entity.EntityName, entity.Columns, bulkMgr);
                }
            }
        }
Exemple #10
0
        public void BulkSetStateQueryTest1()
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            QueryExpression q = new QueryExpression("account");

            q.ColumnSet = new ColumnSet();
            q.ColumnSet.AddColumn("accountid");
            q.Criteria = new FilterExpression();
            q.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);

            var results = mgr.BulkSetState(q, new OptionSetValue(0), new OptionSetValue(1));
        }
Exemple #11
0
        public void BulkDeleteTest1()
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            QueryExpression q = new QueryExpression("account");

            q.ColumnSet = new ColumnSet();
            q.ColumnSet.AddColumn("accountid");
            q.Criteria = new FilterExpression();
            q.Criteria.AddCondition("name", ConditionOperator.BeginsWith, "bulk updated");

            var results = mgr.BulkDelete(q, "Bulk Delete Test" + DateTime.Now.ToString());
        }
Exemple #12
0
        public void BulkUpsertTestInsert()
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            for (int i = 0; i < 10; i++)
            {
                Entity entity = new Entity("account");
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            var results = mgr.BulkUpdate(entityList, useUpsert: true);
        }
Exemple #13
0
        private static void ExecuteBulkInsertTest(CTCBulkTransactionMode transactionMode = CTCBulkTransactionMode.None)
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            for (int i = 0; i < 10; i++)
            {
                Entity entity = new Entity("account");
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            var results = mgr.BulkInsert(entityList, transactionMode: transactionMode);
        }
Exemple #14
0
        private void RecordFailedRecord(string entityName, Entity trackEntity, CrmBulkServiceManager bulkMgr, List <Entity> entitiesToUpSert, Exception ex)
        {
            Entity failedEntity = new Entity("ctccrm_entitychangefailed");

            failedEntity["ctccrm_name"]     = entityName;
            failedEntity["ctccrm_recordid"] = entitiesToUpSert[0].Id.ToString();
            failedEntity.KeyAttributes      = new KeyAttributeCollection();
            failedEntity.KeyAttributes.Add("ctccrm_name", entityName);
            failedEntity.KeyAttributes.Add("ctccrm_recordid", entitiesToUpSert[0].Id.ToString());
            failedEntity["ctccrm_errormessage"] = ex.Message;
            List <Entity> failedLog = new List <Entity>();

            failedLog.Add(failedEntity);
            trackEntity.KeyAttributes = new KeyAttributeCollection();
            trackEntity.KeyAttributes.Add("ctccrm_name", entityName);
            failedLog.Add(trackEntity);
            bulkMgr.BulkUpdate(failedLog, transactionMode: CTCBulkTransactionMode.Single, useUpsert: true);
        }
Exemple #15
0
        public void BulkUpdateQueryTest1()
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            QueryExpression q = new QueryExpression("account");

            q.ColumnSet = new ColumnSet();
            q.ColumnSet.AddColumn("accountid");
            Entity entityData = new Entity("account");

            entityData["name"] = "bulk updated " + DateTime.Now.ToString();

            var results = mgr.BulkUpdate(q, entityData);
        }
Exemple #16
0
        public void BulkRunWorkflowQueryTest1()
        {
            CrmConnection         c       = new CrmConnection("CRM");
            OrganizationService   service = new OrganizationService(c);
            CrmBulkServiceManager mgr     = new CrmBulkServiceManager(service);

            List <Entity> entityList = new List <Entity>();

            QueryExpression q = new QueryExpression("account");

            q.ColumnSet = new ColumnSet();
            q.ColumnSet.AddColumn("accountid");
            q.Criteria = new FilterExpression();
            q.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);

            var workflowID = GetWorkflowID(service, "account", "UnitTest Account");

            var results = mgr.BulkRunWorkflow(q, workflowID);
        }
Exemple #17
0
        public void BulkInsertTest2()
        {
            CrmConnection c = new CrmConnection("CRM");
            List <IOrganizationService> services = new List <IOrganizationService>();

            for (int i = 0; i < 4; i++)
            {
                OrganizationService service = new OrganizationService(c);
                services.Add(service);
            }

            CrmBulkServiceManager mgr = new CrmBulkServiceManager(services);

            List <Entity> entityList = new List <Entity>();

            for (int i = 0; i < 480; i++)
            {
                Entity entity = new Entity("account");
                entity["name"] = "account " + DateTime.Now.ToString();
                entityList.Add(entity);
            }

            var results = mgr.BulkInsert(entityList, batchSize: 120);
        }
Exemple #18
0
        private void ReProcessFailedForEntity(IOrganizationService targetService, string entityName, ColumnSet columns, CrmBulkServiceManager bulkMgr)
        {
            QueryExpression queryFailed = new QueryExpression("ctccrm_entitychangefailed");

            queryFailed.ColumnSet = new ColumnSet(new string[] { "ctccrm_recordid", "ctccrm_retrycount" });
            queryFailed.Criteria  = new FilterExpression();
            queryFailed.Criteria.AddCondition("ctccrm_name", ConditionOperator.Equal, entityName);

            ConditionExpression limitRetry  = new ConditionExpression("ctccrm_retrycount", ConditionOperator.LessThan, 6);
            ConditionExpression retryNullOK = new ConditionExpression("ctccrm_retrycount", ConditionOperator.Null);
            var filterRetry = new FilterExpression(LogicalOperator.Or);

            filterRetry.AddCondition(limitRetry);
            filterRetry.AddCondition(retryNullOK);
            queryFailed.Criteria.Filters.Add(filterRetry);


            QueryToAction(targetService, queryFailed, (entityList) =>
            {
                foreach (var entity in entityList.Entities)
                {
                    try
                    {
                        var sourceEntity = _Service.Retrieve(entityName, Guid.Parse(entity.GetAttributeValue <string>("ctccrm_recordid")), columns);
                        TrimFieldsFromChanges(sourceEntity);
                        UpsertRequest targetReq = new UpsertRequest();
                        targetReq.Target        = sourceEntity;
                        DeleteRequest deleteReq = new DeleteRequest();
                        deleteReq.Target        = entity.ToEntityReference();
                        bulkMgr.BulkTransaction(new OrganizationRequest[] { targetReq, deleteReq });
                    }
                    catch (Exception ex)
                    {
                        int retryCount = entity.GetAttributeValue <int>("ctccrm_retrycount");
                        retryCount++;
                        entity["ctccrm_retrycount"] = retryCount;
                        targetService.Update(entity);
                    }
                }
            });
        }