I am trying to upload a file to SharePoint using Graph API and in order to execute that I need to convert the file to a binary stream and pass that value in to my request body of graph api. Can anybody help me with this issue?
Asked
Active
Viewed 451 times
1 Answers
0
@Rishi Roshan,
Please take a reference of below console app:
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharePointSite.msgraph
{
class UploadFile
{
static void Main(string[] args)
{
string clientId = "e0cefc2c-1004-4622-81ab-f7b421063112"; //e.g. 01e54f9a-81bc-4dee-b15d-e661ae13f382
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.WithRedirectUri("msale0cefc2c-1104-4622-81ab-f7b421063112://auth")
.Build();
string[] scopes = new string[] { "Sites.ReadWrite.All" };
InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(publicClientApplication, scopes);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
using var stream = System.IO.File.OpenRead(@"C:\Users\admin\Downloads\test.txt");
DriveItem uploadfile = graphClient.Sites.Root.Drive.Root.ItemWithPath("test-111.txt").Content.Request().PutAsync<DriveItem>(stream).Result;
Console.WriteLine(uploadfile);
}
}
}
BR

Baker_Kong
- 1,739
- 1
- 4
- 9