1

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

IPS
  • 417
  • 9
  • 22
  • You could (temporarily) map your SharePoint site to a network drive as explained [here](https://www.clouddirect.net/knowledge-base/KB0011543/mapping-a-sharepoint-site-as-a-network-drive). Provided, the mapping works, you can access the directory as in an ordinary filesystem. – Axel Kemper Jun 01 '20 at 15:03

2 Answers2

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

Jerry
  • 3,480
  • 1
  • 10
  • 12
0

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:

https://learn.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

Nelson Nyland
  • 444
  • 5
  • 11
  • 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](/review/late-answers/31188574) – Patrick Mar 07 '22 at 08:25