0

I have some function (shown below that works as expected) that creates a SharePoint folder. For simplicity sake we can ignore the operation of creating a folder and assume the folder already exists within the SharePoint site... I am curious as to how to open a given folder using the API and add a file to it that is a PowerPoint. The purposes of the PowerPoint is that each newly created folder will contain a template PowerPoint which can then be copy/changed by the user removing the need for the user to download the template themselves and add the PowerPoint to the folder manually. For simplicity as mentioned earlier we can assume the folder already exists so I would just need to access it using

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

Then perform a similar Add operation that is being used to create a new folder. I know I'd either need to read the binary data from the PowerPoint and pass that to some File object or if there's a simpler way to use the direct link to the template PowerPoint and simply create a copy of it and insert it into the SharePoint folder.

  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

0 Answers0