We want to import documents into sharepoint by sorting them into sub-directories. To do this, we need to create the directory if it doesn’t already exist.
The import works fine if the directory exists, but we can’t create the new directories without getting errors.
Here is what we got so far. Are we missing something ?
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++)
{
string originalFileName = Path.GetFileName(files[i]);
string uploadFolderPath = location.spLibrary;
if (location.useFirstWordAsSubDirectory)
{
string[] parts = originalFileName.Split('_');
if (parts.Length >= 2)
{
uploadFolderPath = String.Format("{0}/{1}", uploadFolderPath, parts[0]);
}
}
uploadFolderPath = String.Format("{0}/{1}", uploadFolderPath, originalFileName);
Console.Write(" {0}...", originalFileName);
if (UploadFile(context, uploadFolderPath, 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);
}
public static bool UploadFile(ClientContext context, string uploadPath, string originalFilePath)
{
try
{
var fileCreationInfo = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(originalFilePath),
Overwrite = true,
Url = uploadPath
};
var targetFolder = context.Web.GetFolderByServerRelativeUrl(Path.GetDirectoryName(uploadPath));
var uploadFile = targetFolder.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;
}
}
}
}
If the directory doesn’t exist, we create it based on the name of the file to be imported.:
originalFileName.Split('_');