2

What is the fastest way to write files to a SharePoint document library server-side, considering we only need to write lots of files (byte[]s) and we do not need to consider metadata or other stuff (only files)?

For example:

SPFileCollection files = library.RootFolder.Files;
foreach (LargeFile largeFile in MyFiles)
{
  SPFile file = files.Add(largeFile.FileName, largeFile.Bytes);
}

Are there faster ways?

Or with metadata:

SPFileCollection files = library.RootFolder.Files;
foreach (LargeFile largeFile in MyFiles)
{
  SPFile file = files.Add(largeFile.FileName, largeFile.Bytes);
  SPListItem item = file.ListItemAllFields;
  item["Title"] = "Test";
  item.Update();
}

(a related question might be if SharePoint converts CSOM batches to this kind of code first)

0

For example, you can use WebClient and UploadFile method, see example below:

        public static void UploadToSP(string[] files, string serverPath)
        {
            foreach (var file in files)
            {
                Console.Write("Upload file: {0}", file);

                WebClient wc = new WebClient();
                wc.UseDefaultCredentials = true;

                var fileName = System.IO.Path.GetFileName(file);
                Uri uri = new Uri(serverPath + fileName);
                wc.UploadFile(uri, "PUT", file);

                Console.WriteLine(" - Uploaded");
            }
        }

And how to use:

var serverPath      = "http://dev2/sites/test01/Shared Documents/Folder1/";
var uploadDirectory = @"C:\temp\upload";

UploadToSP(System.IO.Directory.GetFiles(uploadDirectory), serverPath);

Also you can change this example and upload files asynchronously.

P.S.: Don't forget about filename limitations described on msdn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.