Search code examples
c#microsoft-graph-apiblazormicrosoft-graph-files

GraphAPI request content Read/Write Timeout Throwing Invalid Operation Exception


The code below creates a new folder within the SharePoint which will then copy a power point and add it to the newly created folder. I am making 2 requests, one to get the file itself and another to get the file contents for an appropriate copy. When performing the follow line

var tester = await graphClient.Sites[sharepoint.Id].Drive.Root.ItemWithPath("FolderPath/Template.pptx").Content.Request().GetAsync();

It doesn't seem to be working on creating the copy to the newly created folder. When examining in debugger I see my tester of stype System.IO.MemoryStream is getting an invalid operation exception with the Read.Timeout and Write.Timeout. Any assistance would be great.

  public async Task<string> Sharepoint_FolderCreate(string NewFolderName, string sharepoint_folder_path = "/SomeFolderPath")
        {
            var item = new DriveItem
            {

                Name = NewFolderName.Replace("?", " ").Replace("/", " ").Replace("\\", " ").Replace("<", " ").Replace(">", " ").Replace("*", " ").Replace("\"", " ").Replace(":", " ").Replace("|", " "),
                Folder = new Folder { },
                AdditionalData = new Dictionary<string, object>()
                    {
                        {"@microsoft.graph.conflictBehavior","rename"}
                    }
            };
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };
            // https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
            var clientSecretCredential = new ClientSecretCredential(
                tenantID, clientId, clientSecret, options);

            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
            var sharepoint = await graphClient.Sites.GetByPath("/sites/SiteFolder", "localhost.sharepoint.com").Request().GetAsync();

            await graphClient.Sites[sharepoint.Id].Drive.Root.ItemWithPath(sharepoint_folder_path).Children.Request().AddAsync(item);
            var NewFolder = await graphClient.Sites[sharepoint.Id].Drive.Root.ItemWithPath($"{sharepoint_folder_path}/{item.Name}").Request().GetAsync();
            return NewFolder.WebUrl;

        }

Solution

  • Thank you to the above comments. My goal was achieved with the following code which creates a new folder then copies a powerpoint saved within a different folder in the sharepoint to the newly created folder.

     public async Task<string> Sharepoint_FolderCreate(string NewFolderName, string sharepoint_folder_path = "/FolderPath")
            {
                var item = new DriveItem
                {
    
                    Name = NewFolderName.Replace("?", " ").Replace("/", " ").Replace("\\", " ").Replace("<", " ").Replace(">", " ").Replace("*", " ").Replace("\"", " ").Replace(":", " ").Replace("|", " ").Trim(),
                    Folder = new Folder { },
                    AdditionalData = new Dictionary<string, object>()
                        {
                            {"@microsoft.graph.conflictBehavior","rename"}
                        }
                };
                var scopes = new[] { "https://graph.microsoft.com/.default" };
                var options = new TokenCredentialOptions
                {
                    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
                };
    
                var clientSecretCredential = new ClientSecretCredential(tenantID, clientId, clientSecret, options);
                var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
                var sharepoint = await graphClient.Sites.GetByPath("/sites/Folder", "localhost.sharepoint.com").Request().GetAsync();
                await graphClient.Sites[sharepoint.Id].Drive.Root.ItemWithPath(sharepoint_folder_path).Children.Request().AddAsync(item);
                var NewFolder = await graphClient.Sites[sharepoint.Id].Drive.Root.ItemWithPath($"{sharepoint_folder_path}/{item.Name}").Request().GetAsync();
    
                var parentReference = new ItemReference
                {
                    DriveId = NewFolder.ParentReference.DriveId,
                    Id = NewFolder.Id
                };
    
                await graphClient.Sites[sharepoint.Id].Drive.Root.ItemWithPath("/FolderPath/Template.pptx").Copy($"{item.Name}.pptx", parentReference).Request().PostAsync();
    
                return NewFolder.WebUrl;
    
            }