Example #1
0
        private void ImportSolution(CrmConnection connection)
        {
            if (string.IsNullOrWhiteSpace(this.Extension))
            {
                Log.LogError("Required parameter missing: Extension");
                return;
            }

            string directoryPath = string.IsNullOrEmpty(this.Path)
                ? System.IO.Path.GetDirectoryName(this.BuildEngine.ProjectFileOfTaskNode)
                : this.Path;

            // ReSharper disable once AssignNullToNotNullAttribute
            string solutioneFile = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", System.IO.Path.Combine(directoryPath, this.Name), this.Extension);

            if (!File.Exists(solutioneFile))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The given Solution file for import does not exist. {0}", solutioneFile));
                return;
            }

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled;
                    });

                    byte[] customizationFile = File.ReadAllBytes(solutioneFile);
                    var    request           = new ImportSolutionRequest
                    {
                        CustomizationFile = customizationFile,
                        OverwriteUnmanagedCustomizations = this.overwriteCustomizations,
                        PublishWorkflows = this.EnableSdkProcessingSteps
                    };

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing Solution {0}. Please wait...", this.Name));
                    serviceContext.Execute(request);
                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported Solution {0} to organization with Url {1}.", this.Name, this.OrganizationUrl));
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                     CultureInfo.CurrentCulture,
                                     "An error occurred while importing Solution {0} to Organization with Url {1}. [{2}]",
                                     this.Name,
                                     this.OrganizationUrl,
                                     exception.Message));
                }
            }
        }
Example #2
0
        public IOrganizationService GetOrganisationService()
        {
            CrmConnection connection = null;

            try
            {
                connection = _CrmConnectionProvider.GetOrganisationServiceConnection();
                connection.UserTokenExpiryWindow = new TimeSpan(0, 3, 0);
                // var service = new OrganizationService(connection);
                var context = new CrmOrganizationServiceContext(connection);
                // Optionally disbale caching?
                context.TryAccessCache(cache => cache.Mode = OrganizationServiceCacheMode.Disabled);
                return(context);
            }
            catch (Exception ex)
            {
                throw new FailedToConnectToCrmException(connection, ex);
            }
        }
        public IOrganizationService GetOrganisationService()
        {
            CrmConnection connection = null;
            try
            {

                connection = _CrmConnectionProvider.GetOrganisationServiceConnection();
                connection.UserTokenExpiryWindow = new TimeSpan(0, 3, 0);
                // var service = new OrganizationService(connection);
                var context = new CrmOrganizationServiceContext(connection);
                // Optionally disbale caching?
                context.TryAccessCache(cache => cache.Mode = OrganizationServiceCacheMode.Disabled);
                return context;
            }
            catch (Exception ex)
            {
                throw new FailedToConnectToCrmException(connection, ex);
            }
        }
Example #4
0
        private void ImportData()
        {
            if (!File.Exists(this.FilePath))
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find import data file {0}", this.FilePath));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var    connection       = CrmConnection.Parse(connectionString);

            string content = File.ReadAllText(this.FilePath);

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var dataMap = serviceContext.CreateQuery("importmap").FirstOrDefault(s => s.GetAttributeValue <string>("name") == this.DataMapName);
                    if (dataMap == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find data map {0} in the organization with Url {1}", this.DataMapName, this.OrganizationUrl));
                        return;
                    }

                    var importId = CreateImportEntity(serviceContext, this.SourceEntityName);
                    this.CreateImportFileEntity(serviceContext, content, importId, dataMap.Id);

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing data from File {0} to entity {1}", this.FilePath, this.TargetEntityName));
                    serviceContext.Execute(new ParseImportRequest {
                        ImportId = importId
                    });
                    serviceContext.Execute(new TransformImportRequest {
                        ImportId = importId
                    });
                    serviceContext.Execute(new ImportRecordsImportRequest {
                        ImportId = importId
                    });
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled;
                    });

                    int  waitCount       = 0;
                    bool importCompleted = false;
                    do
                    {
                        int statusCode = GetImportStatus(serviceContext, importId);
                        switch (statusCode)
                        {
                        case DataImportStatusSuccess:
                            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported data file {0} to entity {1}.", this.FilePath, this.TargetEntityName));
                            importCompleted = true;
                            break;

                        case DataImportStatusFailed:
                            Log.LogError(string.Format(CultureInfo.CurrentCulture, "Import of data file {0} to entity {1} failed.", this.FilePath, this.TargetEntityName));
                            importCompleted = true;
                            break;
                        }

                        if (!importCompleted)
                        {
                            Log.LogMessage("Importing...");

                            Thread.Sleep(WaitIntervalInMilliseconds);
                            if (++waitCount > this.timeoutInMinutes)
                            {
                                Log.LogError("Import failed to complete during the maximum allocated time");
                                break;
                            }
                        }
                    }while (!importCompleted);
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                     CultureInfo.CurrentCulture,
                                     "An error occurred while importing Data file {0} to Entity {1} for Organization with Url {2}. [{3}]",
                                     this.FilePath,
                                     this.TargetEntityName,
                                     this.OrganizationUrl,
                                     exception.Message));
                }
            }
        }
        private void ImportSolution(CrmConnection connection)
        {
            if (string.IsNullOrWhiteSpace(this.Extension))
            {
                Log.LogError("Required parameter missing: Extension");
                return;
            }

            string directoryPath = string.IsNullOrEmpty(this.Path)
                ? System.IO.Path.GetDirectoryName(this.BuildEngine.ProjectFileOfTaskNode)
                : this.Path;

            // ReSharper disable once AssignNullToNotNullAttribute
            string solutioneFile = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", System.IO.Path.Combine(directoryPath, this.Name), this.Extension);
            if (!File.Exists(solutioneFile))
            {
                Log.LogError(string.Format(CultureInfo.CurrentCulture, "The given Solution file for import does not exist. {0}", solutioneFile));
                return;
            }

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled;
                    });

                    byte[] customizationFile = File.ReadAllBytes(solutioneFile);
                    var request = new ImportSolutionRequest
                    {
                        CustomizationFile = customizationFile,
                        OverwriteUnmanagedCustomizations = this.overwriteCustomizations,
                        PublishWorkflows = this.EnableSdkProcessingSteps
                    };

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing Solution {0}. Please wait...", this.Name));
                    serviceContext.Execute(request);
                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported Solution {0} to organization with Url {1}.", this.Name, this.OrganizationUrl));
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                    CultureInfo.CurrentCulture,
                                    "An error occurred while importing Solution {0} to Organization with Url {1}. [{2}]",
                                    this.Name, 
                                    this.OrganizationUrl,
                                    exception.Message));
                }
            }
        }
Example #6
0
        private void ImportData()
        {
            if (!File.Exists(this.FilePath))
            {
                this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find import data file {0}", this.FilePath));
                return;
            }

            Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Connecting to Organization {0}.", this.OrganizationUrl));
            string connectionString = string.Format(CultureInfo.CurrentCulture, "Server={0};Timeout={1}", this.OrganizationUrl, this.ConnectionTimeout);
            var connection = CrmConnection.Parse(connectionString);

            string content = File.ReadAllText(this.FilePath);

            using (var serviceContext = new CrmOrganizationServiceContext(connection))
            {
                try
                {
                    var dataMap = serviceContext.CreateQuery("importmap").FirstOrDefault(s => s.GetAttributeValue<string>("name") == this.DataMapName);
                    if (dataMap == null)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentCulture, "Could not find data map {0} in the organization with Url {1}", this.DataMapName, this.OrganizationUrl));
                        return;
                    }

                    var importId = CreateImportEntity(serviceContext, this.SourceEntityName);
                    this.CreateImportFileEntity(serviceContext, content, importId, dataMap.Id);

                    Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Importing data from File {0} to entity {1}", this.FilePath, this.TargetEntityName));
                    serviceContext.Execute(new ParseImportRequest { ImportId = importId });
                    serviceContext.Execute(new TransformImportRequest { ImportId = importId });
                    serviceContext.Execute(new ImportRecordsImportRequest { ImportId = importId });
                    serviceContext.TryAccessCache(delegate(IOrganizationServiceCache cache)
                    {
                        cache.Mode = OrganizationServiceCacheMode.Disabled; 
                    });

                    int waitCount = 0;
                    bool importCompleted = false;
                    do
                    {
                        int statusCode = GetImportStatus(serviceContext, importId);
                        switch (statusCode)
                        {
                            case DataImportStatusSuccess:
                                Log.LogMessage(MessageImportance.Normal, string.Format(CultureInfo.CurrentCulture, "Successfully imported data file {0} to entity {1}.", this.FilePath, this.TargetEntityName));
                                importCompleted = true;
                                break;

                            case DataImportStatusFailed:
                                Log.LogError(string.Format(CultureInfo.CurrentCulture, "Import of data file {0} to entity {1} failed.", this.FilePath, this.TargetEntityName));
                                importCompleted = true;
                                break;
                        }

                        if (!importCompleted)
                        {
                            Log.LogMessage("Importing...");

                            Thread.Sleep(WaitIntervalInMilliseconds);
                            if (++waitCount > this.timeoutInMinutes)
                            {
                                Log.LogError("Import failed to complete during the maximum allocated time");
                                break;
                            }                            
                        }
                    } 
                    while (!importCompleted);
                }
                catch (Exception exception)
                {
                    Log.LogError(string.Format(
                                    CultureInfo.CurrentCulture,
                                    "An error occurred while importing Data file {0} to Entity {1} for Organization with Url {2}. [{3}]",
                                    this.FilePath,
                                    this.TargetEntityName,
                                    this.OrganizationUrl,
                                    exception.Message));
                }
            }
        }