Bonjour,

Nous avons créé le programme ci-dessous pour importer automatiquement des fichiers dans Sharepoint.

J'aimerai cependant créer automatiquement des sous-répertoires afin d'y organiser les documents importés.
Ce répertoire doit être nommé en fonction du nom du fichier (Split('_') sur le nom du fichier).


C'est la que nous bloquons. Si l'import fonctionne dans le cas ou le répertoire existe, impossible de créer un nouveau répertoire.
Auriez-vous une idée ? Avons nous oublié quelque chose ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
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;
            }
        }
    }
}
Merci