コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args">Connection string, zip file path, threads</param>
        private static void Main(string[] args)
        {
            Console.WriteLine("DataMigrationUtility 1.0");

            if (args.Length == 0)
            {
                Console.WriteLine("To use: [ConnectionString] [DataZipPath] [ThreadsNumber(5)]");
                Console.WriteLine("ex: \"ServiceUri=http://myserver/CRM;\" \"data.zip\" 5");
                return;
            }

            // ex: "ServiceUri=http://myserver/CRM;"
            string connectionString = args[0];

            // ex: parameters_data.zip
            string workingImportFolder = args[1];

            // ex: 0, 5, 10
            int threads = 5;

            if (args.Length > 2)
            {
                threads = int.Parse(args[2]);
            }

            var parser = new ImportCrmDataHandler();

            parser.CrmConnection = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);

            // more connections can be added for multi-threading
            var _importConnections = new Dictionary <int, Microsoft.Xrm.Tooling.Connector.CrmServiceClient>();

            for (int i = 1; i < threads; i++)
            {
                _importConnections.Add(i, new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString));
            }

            parser.ImportConnections = _importConnections;

            parser.AddNewProgressItem  += new EventHandler <ProgressItemEventArgs>(_parser_AddNewProgressItem);
            parser.UpdateProgressItem  += new EventHandler <ProgressItemEventArgs>(_parser_UpdateProgressItem);
            parser.UserMappingRequired += new EventHandler <UserMapRequiredEventArgs>(_parser_UserMappingRequired);

            if (!ImportCrmDataHandler.CrackZipFileAndCheckContents(workingImportFolder, workingImportFolder, out workingImportFolder))
            {
                Console.WriteLine("The zip file validation failed.");
            }
            else
            {
                parser.ValidateSchemaFile(workingImportFolder);
                parser.ImportDataToCrm(workingImportFolder, false);
            }
        }
コード例 #2
0
        public bool ImportData(string inputFile)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (string.IsNullOrWhiteSpace(inputFile))
            {
                throw new ArgumentNullException(nameof(inputFile));
            }

            inputFile = Path.GetFullPath(inputFile);
            Logger.Info(CultureInfo.InvariantCulture, "Input file: {0}.", inputFile);

            if (!File.Exists(inputFile))
            {
                throw new FileNotFoundException("Input file does not exist.");
            }

            Logger.Info("Importing data started.");

            string workingFolder = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile));

            ImportCrmDataHandler.CrackZipFileAndCheckContents(inputFile, workingFolder, out workingFolder);

            var idh = new ImportCrmDataHandler
            {
                CrmConnection     = CrmServiceClient,
                ImportConnections = new Dictionary <int, CrmServiceClient>
                {
                    { 1, CrmServiceClient }
                }
            };

            idh.AddNewProgressItem += new EventHandler <ProgressItemEventArgs>(LogMigrationProgress);
            idh.UpdateProgressItem += new EventHandler <ProgressItemEventArgs>(LogMigrationProgress);
            idh.ValidateSchemaFile(workingFolder);

            bool result = idh.ImportDataToCrm(workingFolder, false);

            var di = new DirectoryInfo(workingFolder);

            di.Delete(true);

            Logger.Info("Data imported successfully.");

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            return(result);
        }