Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

Uploading Multiple files and folders to Sharepoint Online and maintaining the file structure using CSOM

$
0
0

Hi guys,

I would like to upload files and folders to Sharepoint Online while keeping the file structure. how will I do that using Managed Code?

I have the following code below but it only adds a single file at a time.

Thanks in advance.

 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Net;
using MSDN.Samples.ClaimsAuth;


using System.Diagnostics;
using System.IO;


namespace Sp_Ctx
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {


            using (ClientContext context = ClaimClientContext.GetAuthenticatedContext(targetSite))
            {
                try
                {

                      if (context != null)
                            {
                                    //context.Load(context.Web); // Query for Web

                          // creating a document library

                                    //var web = context.Web;
                                    //var lci = new ListCreationInformation();
                                    //lci.Title = "Project Documents";
                                    //lci.TemplateType = (int)ListTemplateType.DocumentLibrary;
                                    //var list = web.Lists.Add(lci);
                          
                          // Uploading Documents to the document Library

                                var web = context.Web;
                                var list = web.Lists.GetByTitle("Documents");
                                var filePath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                                        @"\Cleints.zip";
                                var fci = new FileCreationInformation();
                                var data = System.IO.File.ReadAllBytes(filePath);
                               fci.Content = data;

                                
                                
                                fci.Url = "Cleints.zip";
                                fci.Overwrite = true;
                                var file = list.RootFolder.Files.Add(fci);

                                //var item = file.ListItemAllFields;
                                //item["Year"] = DateTime.Now.Year;
                                //item["Coordinator"] = web.CurrentUser;
                                //item.Update();


                                    context.ExecuteQuery(); // Execute
                                    Console.WriteLine("Success");
                         }

                     }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.Message);
                }
  
            }
            Console.ReadLine();
        }
    }


Viewing all articles
Browse latest Browse all 25064

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>