0

I'm developing a node service, that pushes file to SharePoint online. We are using the official Graph SDK for JavaScript (..and Types).

so far - everything's fine, but if the folder contains a file that has 0-length, I can not find a way to push these files to the drive.

If I try to use the MicrosoftGraph.LargeFileUploadTask JavaScript Class or the OneDriveLargeFileUploadTask the upload doesn't even start, because the formatting of the "Content-Length" header in uploadSlice() fails.

We can skip the class's code and just upload an empty slice by ourself (all code only applies to empty files):

uploadedFile = await client
    .api(uploadSession.url)
    .headers({
        "Content-Length": `0`,
        "Content-Range": `bytes 0-0/0`,
    })
    .put(file); //file is an empty Buffer

Results in: Error: The Content-Range header is missing or malformed

I've tried to commit the empty file without uploading a slice: according to https://learn.microsoft.com/de-de/graph/api/driveitem-createuploadsession?view=graph-rest-1.0#completing-a-file (I've explictly set deferCommit=true for the upload session)

After the final byte range of the file is PUT to the upload URL, send a final POST request to the upload URL with zero-length content (currently only supported on OneDrive for Business and SharePoint).

uploadedFile = await client.api(requestUrl)
    .headers({
        "Content-Length": `0`,
    })
    .post(null);

No Luck, this is the response: SharePoint / Graph expects the missing bytes of unkown lenght, I think:

{
    "@odata.context":"...",
    "expirationDateTime":"2020-11-10T16:04:31.594Z",
    "nextExpectedRanges":["0-"],
    "uploadUrl":"..."
}

The file is not present.

I can not find any documentation on how to handle empty files with Microsoft Graph. I've thought about creating an empty file like using touch online, instead of uploading it, but I can not find a way to do that either.

Maybe this is an unsupported szenario?

halliba
  • 309
  • 2
  • 8
  • Sounds like an unsupported scenario here; even when i tried from Sharepoint online, it's the same case - so i won't blame Microsoft Graph API here. BTW, why you want to push a 0 byte to sharepoint online? Is there any business case behind this? – Dev Nov 11 '20 at 20:25
  • Hmm. Seems so.. We need to one-way-sync folders that contain 0-byte files as kind of markers (like an .gitkeep file does) Funny think: You can add a 0-byte file via OneDrive Windows Client to an SharePoint Online library without problems. – halliba Nov 12 '20 at 14:56
  • Interesting ask. If you still think this feature adds more value & Microsoft to implement it, then consider filing it in Microsoft uservoice (https://microsoftgraph.uservoice.com/forums/920506-microsoft-graph-feature-requests?category_id=359620) - so they can consider it. Let us know how it goes. – Dev Nov 12 '20 at 15:07

2 Answers2

0
  • Sounds like an unsupported scenario here; even when i tried from SharePoint online, it's the same case - so i won't blame Microsoft Graph API here. If you still think this feature adds more value & Microsoft to implement it, then consider filing it in Microsoft user voice so they can consider it!!
Dev
  • 2,428
  • 2
  • 14
  • 15
0

Just don't use LargeFileUploadTask for zero-size files. I had a similar problem but in C#. look at this answer : https://learn.microsoft.com/en-us/answers/questions/938993/have-to-upload-files-into-sharepoint-online-using

Or look at this (from that answer) C# code:

var fileName = "smallfile.txt";  
var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);  
Console.WriteLine("Uploading file: " + fileName);  
  
FileStream fileStream = new FileStream(filePath, FileMode.Open);  
var uploadedFile = client.Me.Drive.Root  
                              .ItemWithPath("smallfile.txt")  
                              .Content  
                              .Request()  
                              .PutAsync<DriveItem>(fileStream)  
                              .Result;  
Console.WriteLine("File uploaded to: " + uploadedFile.WebUrl);