public override void LoadListItems(SharePointClientContext spContext, ListItemCollection items) { spContext.Load(items, t => t.Include(doc => doc.File), t => t.Include(doc => doc.ParentList.RootFolder.ServerRelativeUrl), t => t.Include(doc => doc.ParentList.ParentWebUrl), t => t); }
public virtual void GetListSettings(SharePointClientContext spContext, List list, string title, string destinationPath) { // extend this when needed to be more generic... if (title != "OAMPS Branding") { return; } var values = new Dictionary <string, object>(); var choices = spContext.CastTo <FieldChoice>(list.Fields.GetByTitle("Speciality")); spContext.Load(choices, f => f.Choices, f => f.DefaultValue); spContext.ExecuteQuery(); var specialities = choices.Choices; values.Add("Speciality.DefaultValue", choices.DefaultValue); values.Add("Speciality.Choices", specialities); var json = JsonConvert.SerializeObject(values); System.IO.File.WriteAllText(Path.Combine(destinationPath, "list-settings.txt"), json); }
public override void HandleListItem(SharePointClientContext spContext, ILog logger, Arguments args, string destinationPath, ListItem item, ref int updatedCount, ref int noUpdateRequiredCount) { var fileName = Path.Combine(destinationPath, item.File.Name); if (args.Overwrite || item.File.TimeLastModified > System.IO.File.GetLastWriteTime(fileName)) { logger.Info($"Updating: {fileName}"); using (var fileStream = System.IO.File.Create(fileName)) { var spFileStream = item.File.OpenBinaryStream(); spContext.ExecuteQuery(); spFileStream.Value.CopyTo(fileStream); updatedCount++; } if (fileName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) || fileName.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) { var thumbName = FileHelpers.GetThumbnailPath(fileName); using (var thumbStream = System.IO.File.Create(thumbName)) { var fileUrl = item.File.ServerRelativeUrl; var thumbUrl = FileHelpers.ReplaceLastOccurrence(FileHelpers.GetThumbnailPath(fileUrl), "/", "/_t/"); var thumb = spContext.Web.GetFileByServerRelativeUrl(thumbUrl); spContext.Load(thumb); var spThumbStream = thumb.OpenBinaryStream(); spContext.ExecuteQuery(); spThumbStream.Value.CopyTo(thumbStream); } } } else { noUpdateRequiredCount++; logger.Info($"Skipping: {fileName}"); } }
public override void HandleListItem(SharePointClientContext spContext, ILog logger, Arguments args, string destinationPath, ListItem item, ref int updatedCount, ref int noUpdateRequiredCount) { // nop }
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++; } } }
public virtual void HandleListItem(SharePointClientContext spContext, ILog logger, Arguments args, string destinationPath, ListItem item, ref int updatedCount, ref int noUpdateRequiredCount) { throw new NotImplementedException(); }