Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I receive "Cannot create an upload session on a folder" when trying to create upload session #1353

Closed
davidbabayan opened this issue May 20, 2022 · 2 comments

Comments

@davidbabayan
Copy link

I created a function which is identical to the function described in documentation except I identify the folder where I want to put the file with SharePoint object ID instead of the path.

Here is my function code

    public async Task<DriveItem> UploadFile(string fileName, string folderId, MemoryStream fileContent)
        {
            int maxSliceSize = 320 * 1024;
            long totalLength = fileContent.Length;
            DriveItemUploadableProperties uploadProps = new()
            {
                AdditionalData = new Dictionary<string, object>
                {
                    { "@microsoft.graph.conflictBehavior", "replace" }
                }
            };

            var uploadSession = await GraphServiceClient.Sites[this.SiteId].Drive.Items[folderId]
                                    .CreateUploadSession(uploadProps)
                                    .Request()
                                    .PostAsync();

            LargeFileUploadTask<DriveItem> fileUploadTask = new(uploadSession, fileContent, maxSliceSize);

            IProgress<long> progress = new Progress<long>(prog =>
            {
                Console.WriteLine($"Uploaded {prog} bytes of {totalLength} bytes");
            });

            UploadResult<DriveItem> uploadResult = new();

            try
            {
                uploadResult = await fileUploadTask.UploadAsync(progress);

                Console.WriteLine(uploadResult.UploadSucceeded ?
                    $"Upload complete, item ID: {uploadResult.ItemResponse.Id}" :
                    "Upload failed");
            }
            catch (ServiceException ex)
            {
                Console.WriteLine($"Error uploading: {ex.ToString()}");
            }

            return uploadResult.ItemResponse;
        }

Here is the outcome of the code:

var uploadSession = await GraphServiceClient.Sites[this.SiteId].Drive.Items[folderId]
                                    .CreateUploadSession(uploadProps)
                                    .Request()
                                    .PostAsync();

An unhandled exception occurred while processing the request.
ServiceException: Code: nameAlreadyExists
Message: Cannot create an upload session on a folder
Inner error:
AdditionalData:
date: 2022-05-20T02:37:28
request-id: b7ff5418-47c4-4522-b682-36d8ee435987
client-request-id: b7ff5418-47c4-4522-b682-36d8ee435987
ClientRequestId: b7ff5418-47c4-4522-b682-36d8ee435987

Help me understand what am I doing wrong?

@ghost ghost added the Needs: Triage label May 20, 2022
@andrueastman
Copy link
Member

Hey @davidbabayan,

Thanks for raising this.

I believe the issue is that you are trying to create an upload session for a folder rather than a file. Any chance you have any luck when you add a segment for a file ItemWithPath("file.txt)" like in the snippet below?

var uploadSession = await graphClient.Sites["site-id"].Drive.Items["folderId"].ItemWithPath("file.txt")
                            .CreateUploadSession(uploadProps)
                            .Request()
                            .PostAsync();

@ghost
Copy link

ghost commented May 24, 2022

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@ghost ghost closed this as completed May 27, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jun 26, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants