예제 #1
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);
        }
예제 #2
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);
        }