'How to upload file in Sharepoint using .net core with C#?

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#.



Solution 1:[1]

Microsoft haven't released official .NET Core CSOM Library, please see the UserVoice, the status is still "Working On it":

Support .NET Core with CSOM

Nowadays, it's suggested to use .NET Standard platform instead if possible

Solution 2:[2]

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://docs.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:

https://docs.microsoft.com/en-us/graph/sdks/create-requests?tabs=CS#make-a-post-request-to-create-a-new-entity

Upload file to SharePoint drive using Microsoft Graph

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jerry
Solution 2