I need to create several documents in my SharePoint Online library. I am using the following code to upload an existing file to a library:
ctx.Load(ctx.Web.Folders);
ctx.ExecuteQuery();
ctx.Load(ctx.Web.Folders);
ctx.ExecuteQuery();
Folder myLibrary = ctx.Web.Folders.GetByUrl("Shared Documents");
String fileName = System.IO.Path.GetFileName(fileToUpload);
FileStream fileStream = System.IO.File.OpenRead(fileToUpload);
FileCreationInformation fci = new FileCreationInformation();
fci.Overwrite = false;
fci.Url = "gg.txt";
fci.ContentStream = fileStream;
//byte[] docstream= {134,53,53,55,75,6,5};
//fci.Content = docstream;
Microsoft.SharePoint.Client.File spfile = myLibrary.Files.Add(fci);
myLibrary.Update();
ctx.ExecuteQuery();
However, all Office documents (.docx, .xlsx, .doc) seem to be corrupt and I cannot open them in Word/Excel Online after upload. No issues with .txt files.
I have been trying to use this, which would be perfect for me:
Create a Word Document in a document library programmatically
Microsoft.SharePoint.Client.File file
= docLib.RootFolder.Files.Add(fileName, memStream, true);
but SharePoint Online doesn't accept 3 arguments for .Add() function, only FileCreationInformation.
The questions:
1) How can I adapt Files.Add() for SharePoint Online? Is there a similar function that would allow me to pass in the content of the file?
2) Why are all my Office files corrupted?
3) I expected fci.Content
to create a content inside the file. Apparently it's not the case, because it doesn't seem to do anything. What does it do?