I want to upload file in my sharepoint using .net core c#, but I would't find the microsoft dll or CSOM package in .net core. PLease let me know if you have something that can work in .net core with C#.
2 Answers
Microsoft haven't released official .NET Core CSOM Library, please see the UserVoice, the status is still "Working On it":
Nowadays, it's suggested to use .NET Standard platform instead if possible
Comments
Sounds like what you are looking for is Microsoft Graph API.
I chose to use their .NET SDK which for implementation looked like this in C#:
/// <summary>
/// Takes a path that includes the expected file and uploads it to the specified location.
/// Takes the byte array in the DriveItemRequest Content property and streams it
/// to SharePoint. Returns true if successful. Maximum file upload is 34MB.
/// </summary>
/// <param name="request">DriveItemRequest</param>
/// <returns>bool</returns>
public async Task<bool> UploadSharePointItem(DriveItemRequest request)
{
var result = false;
if (!string.IsNullOrEmpty(request.SiteId) &&
!string.IsNullOrEmpty(request.DriveId) &&
!string.IsNullOrEmpty(request.Paths[0]) &&
request.Content != null)
{
using (var fileStream = new MemoryStream(request.Content))
{
var response = new DriveItem();
// small downloads
if (request.Content.Length <= 4000000) // 4MB
{
response = await _client.Sites[request.SiteId].Drives[request.DriveId]
.Root.ItemWithPath(request.Paths[0]).Content.Request()
.PutAsync<DriveItem>(fileStream);
result = response.Size > 0 ? true : false;
}
// large downloads
else if (request.Content.Length < 34000000) // 34MB
{
// https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=csharp
// Use properties to specify the conflict behavior
var uploadProps = new DriveItemUploadableProperties
{
AdditionalData = new Dictionary<string, object>
{
{ "@microsoft.graph.conflictBehavior", "replace" }
}
};
// Create the upload session
var uploadSession = await _client.Sites[request.SiteId]
.Drives[request.DriveId].Root.ItemWithPath(request.Paths[0])
.CreateUploadSession(uploadProps).Request().PostAsync();
// Max slice size must be a multiple of 320 KiB
int maxSliceSize = 320 * 1024;
var fileUploadTask = new LargeFileUploadTask<DriveItem>
(uploadSession, fileStream, maxSliceSize);
try
{
// Upload the file
var uploadResult = await fileUploadTask.UploadAsync();
response = uploadResult.ItemResponse;
result = uploadResult.UploadSucceeded;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error uploading: {ex}");
}
}
else
{
return result;
}
// check-in item
await _client.Sites[request.SiteId].Drives[request.DriveId]
.Items[response.Id].Checkin().Request().PostAsync();
// update size
if (response.Size == 0)
{
response.Size = request.Content.Length;
await _client.Sites[request.SiteId].Drives[request.DriveId]
.Items[response.Id].Request().UpdateAsync(response);
}
}
}
return result;
}
Links:
1 Comment
Patrick
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review