Beispiel #1
0
        public void UpdateServiceEndpoint(string primaryKey, string namespaceAddress, string sharedAccessKey, string serviceNamespace)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (string.IsNullOrWhiteSpace(primaryKey))
            {
                throw new ArgumentNullException(nameof(primaryKey));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Primary key: {0}.", primaryKey);

            bool isValidGuid = Guid.TryParse(primaryKey, out Guid key);

            if (!isValidGuid)
            {
                throw new ArgumentException("Primary key argument is invalid.");
            }

            if (string.IsNullOrWhiteSpace(namespaceAddress))
            {
                throw new ArgumentNullException(nameof(namespaceAddress));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Namespace address: {0}.", namespaceAddress);

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

            if (string.IsNullOrWhiteSpace(serviceNamespace))
            {
                throw new ArgumentNullException(nameof(serviceNamespace));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Service namespace: {0}.", serviceNamespace);

            ServiceEndpoint serviceEndpoint = CrmService.GetServiceEndpoint(key);

            if (serviceEndpoint == null)
            {
                throw new EntityNotFoundException("Service endpoint not found.");
            }

            OrganizationServiceContext.ClearChanges();

            ServiceEndpoint se = new ServiceEndpoint
            {
                ServiceEndpointId       = serviceEndpoint.Id,
                ContentTypeOfTheMessage = ServiceEndpointMessageFormat.Json,
                NamespaceAddress        = namespaceAddress,
                //SASKey = sharedAccessKey, //TO-DO
                ServiceNamespace = serviceNamespace
            };

            OrganizationServiceContext.Attach(se);

            OrganizationServiceContext.UpdateObject(se);

            CrmService.SaveChanges(OrganizationServiceContext, SaveChangesOptions.None);

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Beispiel #2
0
        public void ExportWebFiles(string outputPath, string websitePrimaryKey)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (string.IsNullOrWhiteSpace(outputPath))
            {
                throw new ArgumentNullException(nameof(outputPath));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Output path: {0}.", outputPath);

            if (!Directory.Exists(Path.GetDirectoryName(outputPath)))
            {
                throw new ArgumentException("The output file path does not exist.");
            }

            if (string.IsNullOrWhiteSpace(websitePrimaryKey))
            {
                throw new ArgumentNullException(nameof(websitePrimaryKey));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Web site primary key: {0}.", websitePrimaryKey);

            bool isValid = Guid.TryParse(websitePrimaryKey, out Guid websiteId);

            if (!isValid)
            {
                throw new ArgumentException("The web site primary key is not valid");
            }

            IList <FileInfo> files = new List <FileInfo>();

            EnumerateFiles(outputPath, files);

            IList <WebFile> webFiles = CrmService.GetWebsiteFiles(websiteId);

            foreach (WebFile webFile in webFiles.OrderBy(x => x.Name))
            {
                Logger.Info(CultureInfo.InvariantCulture, "Processing web file {0}.", webFile.Name);

                Note annotation = CrmService.GetAnnotations(webFile.Id).OrderByDescending(x => x.CreatedOn).FirstOrDefault();
                if (annotation == null)
                {
                    Logger.Warn(CultureInfo.InvariantCulture, "No annotation exists for {0} web file.", webFile.Name);
                }
                else
                {
                    string path  = Path.Combine(outputPath, annotation.FileName);
                    byte[] bytes = Convert.FromBase64String(annotation.Document);
                    File.WriteAllBytes(path, bytes);

                    Logger.Info(CultureInfo.InvariantCulture, "File {0} written.", path);
                }
            }

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Beispiel #3
0
 void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             if (CrmService != null)
             {
                 CrmService.Dispose();
             }
             if (OrganizationServiceContext != null)
             {
                 OrganizationServiceContext.Dispose();
             }
         }
         disposedValue = true;
     }
 }
Beispiel #4
0
        public void UpdateData(string entityName, string primaryKey, string attributeName, string attributeValue)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (string.IsNullOrWhiteSpace(entityName))
            {
                throw new ArgumentNullException(nameof(entityName));
            }
            entityName = entityName.ToLowerInvariant();
            Logger.Info(CultureInfo.InvariantCulture, "Entity name: {0}.", entityName);

            if (string.IsNullOrWhiteSpace(primaryKey))
            {
                throw new ArgumentNullException(nameof(primaryKey));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Primary key: {0}.", primaryKey);

            if (string.IsNullOrWhiteSpace(attributeName))
            {
                throw new ArgumentNullException(nameof(attributeName));
            }
            attributeName = attributeName.ToLowerInvariant();
            Logger.Info(CultureInfo.InvariantCulture, "Attribute name: {0}.", attributeName);

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

            Logger.Info("Updating entity started.");

            Entity entity = new Entity(entityName);

            entity[string.Format(CultureInfo.InvariantCulture, "{0}id", entityName)] = new Guid(primaryKey);
            entity[attributeName] = attributeValue;

            CrmService.UpdateEntity(entity);

            Logger.Info("Entity updated successfully.");

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Beispiel #5
0
        private void UpdateOrganizationSettings(string replaceString, string blockedAttachments)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

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

            Logger.Info(CultureInfo.InvariantCulture, "Updating blocked attachments.");

            Organization organizationSettings = CrmService.GetOrganizationSettings();

            if (organizationSettings == null)
            {
                throw new EntityNotFoundException("No organization record can be found in the system.");
            }

            OrganizationServiceContext.ClearChanges();

            Organization updatedOrganizationSettings = new Organization
            {
                Id = organizationSettings.Id,
                BlockAttachments = blockedAttachments.Replace(replaceString, string.Empty),
            };

            OrganizationServiceContext.Attach(updatedOrganizationSettings);

            OrganizationServiceContext.UpdateObject(updatedOrganizationSettings);

            CrmService.SaveChanges(OrganizationServiceContext, SaveChangesOptions.None);

            Thread.Sleep(60000);

            Logger.Info(CultureInfo.InvariantCulture, "Blocked attachments updated to: {0}.", updatedOrganizationSettings.BlockAttachments);

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Beispiel #6
0
        public void UpdateOrganizationSettings(Organization organization)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (organization == null)
            {
                throw new ArgumentNullException(nameof(organization));
            }

            Organization organizationSettings = CrmService.GetOrganizationSettings();

            if (organizationSettings == null)
            {
                throw new EntityNotFoundException("No organization record can be found in the system.");
            }

            OrganizationServiceContext.ClearChanges();

            //TO-DO: this is in experimental mode

            Organization updatedOrganizationSettings = new Organization
            {
                Id = organizationSettings.Id,
                //BlockAttachments = organization.BlockAttachments,
                OrganizationName = organization.OrganizationName
            };

            OrganizationServiceContext.Attach(updatedOrganizationSettings);

            OrganizationServiceContext.UpdateObject(updatedOrganizationSettings);

            CrmService.SaveChanges(OrganizationServiceContext, SaveChangesOptions.None);

            //Logger.Info(CultureInfo.InvariantCulture, "Blocked attachments updated to: {0}.", updatedOrganizationSettings.BlockAttachments);

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Beispiel #7
0
        public void UpdateWebFiles(string inputPath, string websitePrimaryKey, string blockedAttachments)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (string.IsNullOrWhiteSpace(inputPath))
            {
                throw new ArgumentNullException(nameof(inputPath));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Input path: {0}.", inputPath);

            if (!Directory.Exists(Path.GetDirectoryName(inputPath)))
            {
                throw new FileNotFoundException("The input file path does not exist.");
            }

            if (string.IsNullOrWhiteSpace(websitePrimaryKey))
            {
                throw new ArgumentNullException(nameof(websitePrimaryKey));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Web site primary key: {0}.", websitePrimaryKey);

            bool isValid = Guid.TryParse(websitePrimaryKey, out Guid websiteId);

            if (!isValid)
            {
                throw new ArgumentException("The web site primary key is not valid");
            }

            IList <FileInfo> files = new List <FileInfo>();

            EnumerateFiles(inputPath, files);

            UpdateOrganizationSettings("js;", blockedAttachments);

            IList <WebFile> webFiles = CrmService.GetWebsiteFiles(websiteId);

            OrganizationServiceContext.ClearChanges();

            foreach (FileInfo file in files)
            {
                Logger.Info(CultureInfo.InvariantCulture, "Processing file {0}.", file.FullName);
                WebFile webFile = webFiles.SingleOrDefault(x => x.Name.ToUpperInvariant().Equals(file.Name.ToUpperInvariant()));
                if (webFile == null)
                {
                    Logger.Warn(CultureInfo.InvariantCulture, "There is no portal web file entity for the file {0}.", file.Name);
                    continue;
                }

                Note annotation = CrmService.GetAnnotations(webFile.Id).OrderByDescending(x => x.CreatedOn).FirstOrDefault();

                if (annotation == null)
                {
                    Logger.Info(CultureInfo.InvariantCulture, "A new annotation {0} will be created.", file.Name);

                    annotation = new Note {
                        Document   = Convert.ToBase64String(File.ReadAllBytes(file.FullName)),
                        FileName   = file.Name,
                        IsDocument = true,
                        MimeType   = MimeMapping.GetMimeMapping(file.Name),
                        ObjectType = webFile.LogicalName,
                        Regarding  = new EntityReference(webFile.LogicalName, webFile.Id),
                    };

                    OrganizationServiceContext.AddObject(annotation);
                }
                else if (!annotation.Document.Equals(Convert.ToBase64String(File.ReadAllBytes(file.FullName))))
                {
                    Logger.Info(CultureInfo.InvariantCulture, "The annotation {0} will be updated.", file.Name);

                    annotation = new Note
                    {
                        AnnotationId = annotation.Id,
                        Document     = Convert.ToBase64String(File.ReadAllBytes(file.FullName))
                    };

                    OrganizationServiceContext.Attach(annotation);
                    OrganizationServiceContext.UpdateObject(annotation);
                }
            }

            CrmService.SaveChanges(OrganizationServiceContext, SaveChangesOptions.None);

            UpdateOrganizationSettings("nothing really", blockedAttachments);

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Beispiel #8
0
        public void UpdateWebTemplates(string inputPath, string websitePrimaryKey)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (string.IsNullOrWhiteSpace(inputPath))
            {
                throw new ArgumentNullException(nameof(inputPath));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Input path: {0}.", inputPath);

            if (!Directory.Exists(Path.GetDirectoryName(inputPath)))
            {
                throw new FileNotFoundException("The input file path does not exist.");
            }

            if (string.IsNullOrWhiteSpace(websitePrimaryKey))
            {
                throw new ArgumentNullException(nameof(websitePrimaryKey));
            }
            Logger.Info(CultureInfo.InvariantCulture, "Web site primary key: {0}.", websitePrimaryKey);

            bool isValid = Guid.TryParse(websitePrimaryKey, out Guid websiteId);

            if (!isValid)
            {
                throw new ArgumentException("The web site primary key is not valid");
            }

            IList <FileInfo> files = new List <FileInfo>();

            EnumerateFiles(inputPath, files);

            IList <WebTemplate> webTemplates = CrmService.GetWebTemplates(websiteId);

            OrganizationServiceContext.ClearChanges();

            foreach (FileInfo file in files)
            {
                Logger.Info(CultureInfo.InvariantCulture, "Processing file {0}.", file.FullName);

                string fileName = file.Name.ToUpperInvariant().Replace(".HTML", string.Empty);

                WebTemplate webTemplate = webTemplates.SingleOrDefault(x => x.Name.ToUpperInvariant().Equals(fileName));

                if (webTemplate == null)
                {
                    Logger.Warn(CultureInfo.InvariantCulture, "There is no portal web template entity for the file {0}.", file.Name);
                    continue;
                }

                string source = File.ReadAllText(file.FullName);
                if (source.Equals(webTemplate.Source))
                {
                    continue;
                }

                var updatedWebTemplate = new WebTemplate
                {
                    Id     = webTemplate.Id,
                    Source = source
                };

                OrganizationServiceContext.Attach(updatedWebTemplate);
                OrganizationServiceContext.UpdateObject(updatedWebTemplate);
            }

            CrmService.SaveChanges(OrganizationServiceContext, SaveChangesOptions.None);

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