Example #1
0
        public void Synchronize(log4net.ILog logger, Arguments args)
        {
            logger.Info($"\n\n*** Synchronisation of fragments started - {DateTime.Now} ***\n\n");

            var updateCounter     = new UpdateCounter();
            var updateLog         = new UpdateLog();
            var updateLogFileName = Path.Combine(args.Directory.FullName, "UpdateLog.txt");

            if (File.Exists(updateLogFileName))
            {
                var data = File.ReadAllText(updateLogFileName);
                updateLog = JsonConvert.DeserializeObject <UpdateLog>(data);
            }

            try
            {
                if (args.Clean)
                {
                    logger.Info($"\nCleaning out target directory {args.Directory.Name}..\n\n");
                    ClearFolder(args.Directory);
                }
                Directory.CreateDirectory(args.Directory.FullName);

                var contextUrl = args.Source;
                logger.Info("\nConnecting to SharePoint online..\n\n");

                using (var spContext = new SharePointClientContext(contextUrl))
                {
                    logger.Info($"Loading fragments from site: {contextUrl}");

                    var syncConf = FragmentSettings.GetConfig();
                    var fileSync = new FileSynchroniser();
                    var listSync = new ListSynchroniser();
                    foreach (Fragment fragment in syncConf.Fragments)
                    {
                        try
                        {
                            if (fragment.Type == "library")
                            {
                                fileSync.Synchronise(spContext, fragment, updateCounter, updateLog, logger, args);
                            }
                            else if (fragment.Type == "list")
                            {
                                listSync.Synchronise(spContext, fragment, updateCounter, updateLog, logger, args);
                            }
                            else
                            {
                                logger.Warn($"\n\nWarning: Unsupported synchronisation settig found; type:{fragment.Type}, source:{fragment.Source}, destination:{fragment.Destination}\n\n");
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error($"Failed synchronising {fragment.Source}", ex);
                            if (args.BreakOnError)
                            {
                                throw;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("An exception was thrown", ex);
                if (args.BreakOnError)
                {
                    throw;
                }
            }

            var json = JsonConvert.SerializeObject(updateLog);

            System.IO.File.WriteAllText(Path.Combine(args.Directory.FullName, "UpdateLog.txt"), json);

            logger.Info($"\n\nSynchronisation of fragments finished - {DateTime.Now}.\n");
            logger.Info($"Total documents processed {updateCounter.DocumentCount}, {updateCounter.DocumentUpdateCount} templates were updated\n");
            logger.Info($"Total fragments processed {updateCounter.ListItemCount}, {updateCounter.ListItemUpdateCount} items were updated\n\n");
        }
        public void Synchronise(SharePointClientContext spContext, Fragment fragment, UpdateCounter updateCounter, UpdateLog updateLog, log4net.ILog logger, Arguments args)
        {
            var list  = spContext.Web.Lists.GetByTitle(fragment.Source);
            var items = list.GetItems(new CamlQuery());

            LoadListItems(spContext, items);
            spContext.ExecuteQuery();

            var noUpdateRequiredCount = 0;
            var updatedCount          = 0;

            var destinationPath = args.Directory.FullName;

            if (fragment.Destination.Length > 0)
            {
                var destination = args.Directory.CreateSubdirectory(fragment.Destination);
                destinationPath = destination.FullName;
            }

            logger.Info($"\n\nProcessing {fragment.Source} list settings...");
            GetListSettings(spContext, list, fragment.Source, destinationPath);

            logger.Info($"\n\nProcessing {fragment.Source} into {destinationPath}, found {items.Count} fragments");

            foreach (var item in items)
            {
                if (item.FileSystemObjectType == FileSystemObjectType.Folder)
                {
                    continue;
                }

                HandleListItem(spContext, logger, args, destinationPath, item, ref updatedCount, ref noUpdateRequiredCount);

                var fileName = GetFileNameForFieldValue(item, destinationPath);
                SaveItemFieldValues(logger, args.Overwrite, item, fileName, ref updatedCount, ref noUpdateRequiredCount);
            }

            logger.Info($"\nFinished processing {fragment.Source}, {noUpdateRequiredCount} items were up to date, {updatedCount} items were updated");
            UpdateCounters(updateCounter, noUpdateRequiredCount, updatedCount);

            var log = updateLog.Updates.FirstOrDefault(x => x.Source.Equals(fragment.Source, StringComparison.InvariantCultureIgnoreCase));

            if (log == null)
            {
                log = new UpdateLog.UpdateLogEntry()
                {
                    Source       = fragment.Source,
                    Destination  = fragment.Destination,
                    LastModified = DateTime.Now,
                    Version      = 1
                };
                updateLog.Updates.Add(log);
            }
            else
            {
                log.Destination = fragment.Destination;
                if (updatedCount > 0)
                {
                    log.LastModified = DateTime.Now;
                    log.Version++;
                }
            }
        }
Example #3
0
 public override void UpdateCounters(UpdateCounter updateCounter, int noUpdateRequiredCount, int updatedCount)
 {
     updateCounter.DocumentCount       += (noUpdateRequiredCount + updatedCount);
     updateCounter.DocumentUpdateCount += updatedCount;
 }
 public virtual void UpdateCounters(UpdateCounter updateCounter, int noUpdateRequiredCount, int updatedCount)
 {
     updateCounter.ListItemCount       += (noUpdateRequiredCount + updatedCount);
     updateCounter.ListItemUpdateCount += updatedCount;
 }