1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| using System;
using System.IO;
using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
namespace FileUploader
{
class Program
{
static void Main(string[] args)
{
string lockFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ".LOCK");
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "configuration.json");
string data = System.IO.File.ReadAllText(configPath);
Configuration configuration = JsonConvert.DeserializeObject<Configuration>(data);
if (System.IO.File.Exists(lockFilePath))
{
return;
}
System.IO.File.AppendAllText(lockFilePath, "");
try
{
foreach (Location location in configuration.locations)
{
Console.WriteLine("\n --> " + location.sourceFolder + "\n");
ClientContext context = new ClientContext(location.spSite);
Console.Write("Connexion au serveur SharePoint...");
context.Load(context.Web);
context.ExecuteQuery();
Console.Write(" OK!\n");
Console.Write("Scan du répertoire en cours...");
string[] files = Directory.GetFiles(location.sourceFolder);
Console.Write(" {0} fichiers trouvés.\n", files.Length);
Console.WriteLine("\nUpload des {0} fichiers...\n", files.Length);
for (int i = 0; i < files.Length; i++)
{
List library = context.Web.Lists.GetById(new Guid(location.spLibraryGuid));
context.Load(library.RootFolder);
context.ExecuteQuery();
Folder targetFolder = library.RootFolder;
string originalFileName = Path.GetFileName(files[i]);
System.Collections.Generic.List<string> destinationFolders = new System.Collections.Generic.List<string>();
// Si dans la config on a défini un sous répertoire
if (location.destinationFolder != null)
{
destinationFolders.Add(location.destinationFolder);
}
// Si dans la config on dit d'utiliser le premier mot avant le _ comme sous répertoire.
if (location.useFirstWordAsSubDirectory)
{
string[] parts = originalFileName.Split('_');
if (parts.Length >= 2)
{
destinationFolders.Add(parts[0]);
}
}
string destinationFolder = String.Join("/", destinationFolders.ToArray());
if (destinationFolder != null)
{
targetFolder = EnsureFolderPath(context, library.RootFolder, destinationFolder);
}
if (UploadFile(context, targetFolder, files[i]))
{
if (location.deleteOnSuccess)
{
System.IO.File.Delete(files[i]);
}
else if (location.moveOnSuccess)
{
string movePath = System.IO.Path.Combine(location.moveFolder, originalFileName);
if (System.IO.File.Exists(movePath))
{
System.IO.File.Delete(movePath);
}
System.IO.File.Move(files[i], movePath);
}
Console.Write(" OK!\n");
}
else
{
Console.Write(" FAIL!\n");
}
}
Console.WriteLine("\nUpload terminé !");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
System.Threading.Thread.Sleep(5000);
}
System.IO.File.Delete(lockFilePath);
System.Threading.Thread.Sleep(10000);
}
public static bool UploadFile(ClientContext context, Folder destinationFolder, string originalFilePath)
{
try
{
context.Load(destinationFolder);
context.ExecuteQuery();
var fileCreationInfo = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(originalFilePath),
Overwrite = true,
Url = Path.Combine(destinationFolder.ServerRelativeUrl, Path.GetFileName(originalFilePath)),
};
var uploadFile = destinationFolder.Files.Add(fileCreationInfo);
context.Load(uploadFile);
context.ExecuteQuery();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
System.Threading.Thread.Sleep(5000);
return false;
}
}
static Folder EnsureFolderPath(ClientContext context, Folder parentFolder, string folderPath)
{
string[] folderNames = folderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
Folder currentFolder = parentFolder;
foreach (string folderName in folderNames)
{
FolderCollection subFolders = currentFolder.Folders;
context.Load(subFolders);
context.ExecuteQuery();
Folder existingFolder = null;
foreach (Folder folder in subFolders)
{
if (folder.Name == folderName)
{
existingFolder = folder;
break;
}
}
if (existingFolder == null)
{
currentFolder = subFolders.Add(folderName);
context.ExecuteQuery();
}
else
{
currentFolder = existingFolder;
}
}
return currentFolder;
}
}
} |
Partager