IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Sharepoint .NET Discussion :

C# CSOM création d'un répertoire


Sujet :

Développement Sharepoint .NET

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Juillet 2022
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Juillet 2022
    Messages : 45
    Points : 27
    Points
    27
    Par défaut C# CSOM création d'un répertoire
    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

  2. #2
    Membre confirmé Avatar de licardentaistor
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Juillet 2021
    Messages
    341
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Juillet 2021
    Messages : 341
    Points : 491
    Points
    491
    Par défaut
    Est ce que dejà dans sharepoint avec la configuration actuelle tu peux créer un nouveau répertoire?

    quel est le message d'erreur dans le script?

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Juillet 2022
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Juillet 2022
    Messages : 45
    Points : 27
    Points
    27
    Par défaut
    Citation Envoyé par licardentaistor Voir le message
    Est ce que dejà dans sharepoint avec la configuration actuelle tu peux créer un nouveau répertoire?

    quel est le message d'erreur dans le script?
    Avec la configuration actuelle nous pouvons créer le répertoire à la main dans sharepoint.
    Voici à quoi correspond uploadPath dans le script:
    Documents partages/Commandes/TEST/TEST_CDE1.pdf

    Dans le cas ou le répertoire TEST n'existe pas le message d'erreur est le suivant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    TEST_CDE1.pdf...Fichier introuvable.
       à Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
       à Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
       à Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
       à Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
       à FileUploader.Program.UploadFile(ClientContext context, String uploadPath, String originalFilePath) dans C:\FileUploader\FileUploader\Program.cs:ligne 115

    Si le répertoire TEST existe alors le fichier est importé correctement.

    Merci pour votre aide

  4. #4
    Membre confirmé Avatar de licardentaistor
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Juillet 2021
    Messages
    341
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Juillet 2021
    Messages : 341
    Points : 491
    Points
    491
    Par défaut
    oui, je pense qu'il faut créer explicitement le répertoire avant.

    Code C# : 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
     
    using System;
    using Microsoft.SharePoint.Client;
     
    namespace SharePointFolderCreator
    {
        class Program
        {
            static void Main(string[] args)
            {
                string siteUrl = "https://yoursharepointsiteurl";
                string folderPath = "Documents partages/Commandes/TEST";
     
                ClientContext context = new ClientContext(siteUrl);
                // Assume you have set up your context authentication
     
                try
                {
                    CreateFolder(context, folderPath);
                    Console.WriteLine("Folder created successfully.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
     
            static void CreateFolder(ClientContext context, string folderPath)
            {
                string[] pathSegments = folderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                string libraryName = pathSegments[0];
                string subFolderPath = string.Join("/", pathSegments, 1, pathSegments.Length - 1);
     
                List library = context.Web.Lists.GetByTitle(libraryName);
                context.Load(library.RootFolder);
                context.ExecuteQuery();
     
                EnsureFolderPath(context, library.RootFolder, subFolderPath);
            }
     
            static void 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)
                    {
                        FolderCreationInformation newFolderInfo = new FolderCreationInformation
                        {
                            LeafName = folderName
                        };
                        currentFolder = currentFolder.Folders.Add(newFolderInfo);
                        context.ExecuteQuery();
                    }
                    else
                    {
                        currentFolder = existingFolder;
                    }
                }
            }
        }
    }

  5. #5
    Nouveau membre du Club
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Juillet 2022
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Juillet 2022
    Messages : 45
    Points : 27
    Points
    27
    Par défaut
    Citation Envoyé par licardentaistor Voir le message
    oui, je pense qu'il faut créer explicitement le répertoire avant.

    Code C# : 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
     
    using System;
    using Microsoft.SharePoint.Client;
     
    namespace SharePointFolderCreator
    {
        class Program
        {
            static void Main(string[] args)
            {
                string siteUrl = "https://yoursharepointsiteurl";
                string folderPath = "Documents partages/Commandes/TEST";
     
                ClientContext context = new ClientContext(siteUrl);
                // Assume you have set up your context authentication
     
                try
                {
                    CreateFolder(context, folderPath);
                    Console.WriteLine("Folder created successfully.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
     
            static void CreateFolder(ClientContext context, string folderPath)
            {
                string[] pathSegments = folderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                string libraryName = pathSegments[0];
                string subFolderPath = string.Join("/", pathSegments, 1, pathSegments.Length - 1);
     
                List library = context.Web.Lists.GetByTitle(libraryName);
                context.Load(library.RootFolder);
                context.ExecuteQuery();
     
                EnsureFolderPath(context, library.RootFolder, subFolderPath);
            }
     
            static void 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)
                    {
                        FolderCreationInformation newFolderInfo = new FolderCreationInformation
                        {
                            LeafName = folderName
                        };
                        currentFolder = currentFolder.Folders.Add(newFolderInfo);
                        context.ExecuteQuery();
                    }
                    else
                    {
                        currentFolder = existingFolder;
                    }
                }
            }
        }
    }
    Merci pour votre retour, nous sommes en train de tester mais à première vue, FolderCreationInformation semble ne pas\plus exister.
    Nom : Capture d’écran 2024-06-18 104031.png
Affichages : 14
Taille : 72,2 Ko
    Images attachées Images attachées  

  6. #6
    Membre confirmé Avatar de licardentaistor
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Juillet 2021
    Messages
    341
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Juillet 2021
    Messages : 341
    Points : 491
    Points
    491
    Par défaut
    il faut utiliser FolderCollection:

    Code C# : 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
     
    using System;
    using Microsoft.SharePoint.Client;
     
    namespace SharePointFolderCreator
    {
        class Program
        {
            static void Main(string[] args)
            {
                string siteUrl = "https://yoursharepointsiteurl";
                string folderPath = "Documents partages/Commandes/TEST";
     
                ClientContext context = new ClientContext(siteUrl);
                // Assume you have set up your context authentication
     
                try
                {
                    CreateFolder(context, folderPath);
                    Console.WriteLine("Folder created successfully.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                }
            }
     
            static void CreateFolder(ClientContext context, string folderPath)
            {
                string[] pathSegments = folderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                string libraryName = pathSegments[0];
                string subFolderPath = string.Join("/", pathSegments, 1, pathSegments.Length - 1);
     
                List library = context.Web.Lists.GetByTitle(libraryName);
                context.Load(library.RootFolder);
                context.ExecuteQuery();
     
                EnsureFolderPath(context, library.RootFolder, subFolderPath);
            }
     
            static void 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;
                    }
                }
            }
        }
    }

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Technicien Help Desk
    Inscrit en
    Juillet 2022
    Messages
    45
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Technicien Help Desk

    Informations forums :
    Inscription : Juillet 2022
    Messages : 45
    Points : 27
    Points
    27
    Par défaut
    Merci pour votre aide ! ça fonctionne parfaitement maintenant.

    Si ça peut aider d'autres personnes, voici le code final :

    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
    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;
            }
        }
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. cration d'un fichier xml à partir d'une BDD
    Par noutazia20 dans le forum C#
    Réponses: 1
    Dernier message: 14/05/2008, 00h57
  2. Vs C++ 2005 Express - Cration de Variables globales
    Par Thonm dans le forum VC++ .NET
    Réponses: 13
    Dernier message: 28/07/2007, 22h34
  3. cration d'une subform
    Par jamjam19 dans le forum Oracle
    Réponses: 1
    Dernier message: 29/06/2006, 13h27
  4. probleme de cration de dossier
    Par la-breche dans le forum Windows XP
    Réponses: 2
    Dernier message: 26/05/2006, 17h05
  5. Cration d'un module de recherche
    Par cyberbiker dans le forum Access
    Réponses: 1
    Dernier message: 16/03/2006, 15h25

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo