Sharepoint/Azure
Ber GitHub CoPilot om en enkel app för att ladda upp dokument till ett dokumentbibliotek i shrepoint.
Svaret blir
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp2
{
public partial class Form1 : Form
{
private static string clientId = "YOUR_CLIENT_ID";
private static string tenantId = "YOUR_TENANT_ID";
private static string clientSecret = "YOUR_CLIENT_SECRET";
private static string siteId = "YOUR_SITE_ID";
private static string driveId = "YOUR_DRIVE_ID";
public Form1()
{
InitializeComponent();
}
private async void UploadButton_Click(object sender, EventArgs e)
{
var filePath = "path_to_your_file";
await UploadFileToSharePoint(filePath);
}
private async Task UploadFileToSharePoint(string filePath)
{
var confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(clientSecret)
.Build();
var authProvider = new ClientCredentialProvider(confidentialClientApplication);
var graphClient = new GraphServiceClient(authProvider);
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
var uploadSession = await graphClient
.Sites[siteId]
.Drives[driveId]
.Root
.ItemWithPath(Path.GetFileName(filePath))
.CreateUploadSession()
.Request()
.PostAsync();
var maxChunkSize = 320 * 1024; // 320 KB
var provider = new ChunkedUploadProvider(uploadSession, graphClient, fileStream, maxChunkSize);
var uploadResult = await provider.UploadAsync();
if (uploadResult.UploadSucceeded)
{
MessageBox.Show("File uploaded successfully!");
}
else
{
MessageBox.Show("File upload failed.");
}
}
}
}
}
men ClientCredentialProvider, .Root och ChunkedUploadProvider får röda squigglies, för de inte finns.
har paketen Microsoft.Graph och Microsoft.Identity.Client installerade i projektet.
Blir inte klokare av googling desssvärre, någon som vet, kör .Net 8, är svaret anpassat för en annan version av DotNet eller antas jag veta att något annat NuGet-paket skall installeras?