uploading local files from file share to SharePoint Online using MSGraph

Copper Contributor

Hi There,

I am desperately searching for any blog or any article that explains the steps about how to upload local file share documents to SharePoint Online using MSGraph through a c# console application. Thank you so much for your help..

 

As of now i am using this code below to upload files using a PUT call. the code executes fine without error but when i open the file in SharePoint Online i get error stating "Sorry, this document cant be opened for editing". can anyone help me here please?

 

public async Task CallWebApiAndProcessResultASyncFileUpload(string webApiUrlFileUpload ,string accessTokenFileUpload)
{
using (HttpClient client = new HttpClient())
{
//String requestURI = webApiUrlFileUpload;
//HttpContent httpContent = new StringContent("This Message is posted from Visual Studio Lab", Encoding.UTF8, "text/plain");
//HttpRequestMessage newPostRequest = new HttpRequestMessage(HttpMethod.Put, requestURI);
//newPostRequest.Content = httpContent;
//newPostRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessTokenFileUpload);
//var newResponse = await client.SendAsync(newPostRequest);
//var theResponseString = await newResponse.Content.ReadAsStringAsync();

 


String requestURI = webApiUrlFileUpload;

var path = @"C:\Users\Name\Desktop\TestDoc.docx";
var multiForm = new MultipartFormDataContent();
FileStream fs = File.OpenRead(path);
multiForm.Add(new StreamContent(fs), "file", Path.GetFileName(path));

 

HttpRequestMessage newPostRequest = new HttpRequestMessage(HttpMethod.Put, requestURI);
newPostRequest.Content = multiForm;
newPostRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessTokenFileUpload);
var newResponse = await client.SendAsync(newPostRequest);
var theResponseString = await newResponse.Content.ReadAsStringAsync();
}
}

1 Reply
Hello,

Is it possible to open the file in preview? Perhaps the files are checked out by default when migrated?