0

Upload fail for all the DrivesItems in Drive associated with SitePage list in a sharepoint site.

Graph API request:

UploadSession uploadSession = GraphCLient.Sites[{SiteId}].Drives[{DriveId of SitePages List }]. Items[{DriveFolderID}].ItemWithPath(driveItem.Item.Name).CreateUploadSession().Request().PostAsync().Result;

The uploadSession will be created sucessfully but chunkuploadprovider gives error.

Error response: Code: accessDenied Message: The caller does not have permission to perform the action. Inner error

Code Snippet:

private void UploadItem(OneDriveJsonStructure driveItem)
        {
            try
            {
                MemoryStream memStream = (MemoryStream)driveItem.Content;
                byte[] buffer = memStream.ToArray();
                DriveItem item = null;
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer))
                {

                    UploadSession uploadSession = this._SharepointOperations._GraphCLient.Sites[this._SiteId].Drives[this._DriveId].Items[this._DriveFolderId].ItemWithPath(driveItem.Item.Name).CreateUploadSession().Request().PostAsync().Result;
                    var provider = new ChunkedUploadProvider(uploadSession, this._SharepointOperations._GraphCLient, memStream);
                    var chunkRequests = provider.GetUploadChunkRequests();
                    var readBuffer = new byte[buffer.Length];
                    var trackedExceptions = new List<Exception>();
                    DriveItem itemResult = null;

                    foreach (var request in chunkRequests)
                    {
                        // Send chunk request
                        var result = provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions).Result;
                        if (result.UploadSucceeded)
                        {
                            itemResult = result.ItemResponse;
                            item = result.ItemResponse;
                        }
                    }
                    if (itemResult == null)
                    {
                        UploadChunkResult result = null;
                        // Retry the upload ...
                        foreach (var request in chunkRequests)
                        {
                            // Send chunk request
                            result = provider.GetChunkRequestResponseAsync(request, readBuffer, trackedExceptions).Result;
                        }
                        item = result.ItemResponse;
                    }
                }
                item.Permissions = driveItem.Item.Permissions;
                GivePermission(item);

Permission Provide to Client App from Azure AD:

Graph Permission: Graph Permission

Sharepoint Permissions: Sharepoint Psermissions

Even after all these permissions it gives this error message:

"The caller does not have permission to perform the action".

Which permissions are needed to perform this action?

Upload for driveItems of other drive execute sucessfully.

phalteman
  • 3,442
  • 1
  • 29
  • 46

1 Answers1

1

Now that uploading for driveItems of other drive execute successfully, your code should be OK.

So the key to the issue lies in the user's SharePoint permissions.

Please check if the user account can upload files into the target folder in your SharePoint site.

If the user doesn't have the permission to do that, you need to use an admin account to grant edit permission to the user.

Detailed steps:

  1. Next to the folder name, click the Ellipsis…
  2. On the file popup window, click Share.
  3. On the Share dialog box, click Shared with, and then click Advanced.
  4. Click Stop Inheriting Permissions.
  5. Add the user into an SharePoint Group which has Edit or Contribute permission. (If you don't want to modify the default SharePoint Group, just create a new one)

A quick method: Share this folder (with edit permission) with the user.

Next to the folder name, click the Ellipsis…. On the file popup window, click Share. On the Share dialog box, follow the screenshot. enter image description here


UPDATE:

Note that we can't upload any documents into Site Pages document library. Using API is also not supported.

Allen Wu
  • 15,529
  • 1
  • 9
  • 20
  • I tried your steps but it didn't work for me. Still get the same error. Also, I am using **App only authentication** to authenticate my graphclient . Therefore I don't think User permissions will make any effect. – Subhanu Sharma Dec 12 '19 at 09:13
  • Can the user account upload files on SharePoint site? Go to yourdomain.sharepoint.com to navigate to the target folder/drive to have a test. – Allen Wu Dec 12 '19 at 09:16
  • The user account can upload files on other document libraries on the sharepoint site but cannot upload on SitePages document Library (it is a system generated List of type Document Library). – Subhanu Sharma Dec 12 '19 at 09:21
  • 1
    @SubhanuSharma I think I know the reason. For normal document Library, you can use the method I mentioned above to grant permission to upload. But you can’t upload any documents into SitePages document Library. Of course you also can't use API to implement it. – Allen Wu Dec 12 '19 at 09:38
  • Yes, it is an admin account. The document Library name is **Site Pages**. It is a Site Page Library. – Subhanu Sharma Dec 12 '19 at 10:06
  • But how can I create Pages on a SharePoint Site Programmatically? – Subhanu Sharma Dec 12 '19 at 10:08
  • @AllweWu I saw the update in your answer. My goal is to backup the SitePages and restores them when required. For which I backed up the SitePages doc library, but they are unable to restore. Do you have any method to accomplish my goal? – Subhanu Sharma Dec 12 '19 at 10:25
  • @SubhanuSharma Let's talk here: https://chat.stackoverflow.com/rooms/204163/discussion-between-allen-wu-and-subhanu-sharma – Allen Wu Dec 13 '19 at 05:26