0

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;

        }
freyfrey01
  • 53
  • 7
  • 1
    Try another approach. Does this post https://stackoverflow.com/questions/59729254/copy-a-file-using-microsoft-graph-c-sharp-sdk help ? – Rivo R. Oct 26 '22 at 22:01
  • 1
    Hi @freyfrey01 , hope the stackoverflow link posted by Rivo help you to solve your problem , please let me know if you need further help ,thanks – vicky kumar Oct 27 '22 at 13:14
  • @vickykumar thank you for reaching out, as you can see I use ItemWithPath as opposed it Items. Not sure how to get the destination drive id and destination folder Id. My current idea is to add the following lines right before returning the newfolder url.... `var parentReference = new ItemReference { DriveId = sharepoint.Id, Id = NewFolder.Id }; await graphClient.Sites[sharepoint.Id].Drive.Root.ItemWithPath("/OldFolderPath/Template.pptx").Copy("test.pptx", parentReference).Request().PostAsync();` – freyfrey01 Oct 28 '22 at 02:57
  • @vickykumar I do believe the Id attribute is correct but doesn't seem that the DriveId is correct. How could I get that through the debugger? – freyfrey01 Oct 28 '22 at 03:04
  • @vickykumar modification has been made but file is still not copied... `var parentReference = new ItemReference{DriveId = NewFolder.ParentReference.DriveId,Id = NewFolder.ParentReference.Id};` – freyfrey01 Oct 28 '22 at 03:23

1 Answers1

0

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;

        }
freyfrey01
  • 53
  • 7