question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EnsureFolderAsync troubles

See original GitHub issue

Category

  • Bug

Describe the bug

I’m trying to use await folder.EnsureFolderAsync to create nested folder on the fly before adding files in it. File are not upload in the good folder.

Steps to reproduce

You can reproduce with this code.

I’m trying to upload in list with folder in it. /MyList/Daily/Today is working /MyList/Test/Test not working /MyList/Weekly/Test not working and file are in the second folder.

using var context = await _pnpContext.GetContextAsync();
  var folder = (await context.Web.Lists.GetByIdAsync(libraryId, p => p.RootFolder)).RootFolder;
  var suby = await folder.EnsureFolderAsync(CleanFolderName(folderName));
  var subfolder = await context.Web.GetFolderByIdAsync(suby.UniqueId);
await folder.Files.AddAsync(fileName, stream);
 private static string CleanFolderName(string folderName)
    {
        if (folderName.StartsWith("/"))
        {
            return folderName.Remove(0, 1);
        }
        return folderName;
    }

the var suby always return /MyList/Test/Test.

Thank in advance for your help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pINKmUNstercommented, Mar 18, 2021

@jansenbe i try with the nightly release and it works as expected. 👍🏻 All my file are upload in the correct folder.

Thank a lot, you rock !!

0reactions
jansenbecommented, Mar 17, 2021

@pINKmUNster : below approach adds the 3 files in the right folder:

                var list = await context.Web.Lists.AddAsync("Bert", ListTemplateType.DocumentLibrary);
                await list.EnsurePropertiesAsync(p => p.RootFolder);

                //var suby = await list.RootFolder.EnsureFolderAsync("Daily/Today");
                //var suby = await list.RootFolder.EnsureFolderAsync("Test/Test");
                var suby = await list.RootFolder.EnsureFolderAsync("MyList/Weekly/Test");

                await suby.EnsurePropertiesAsync(p => p.Files);
                IFile addedFile = await suby.Files.AddAsync("bert.docx", System.IO.File.OpenRead($".{Path.DirectorySeparatorChar}TestAssets{Path.DirectorySeparatorChar}test.docx"));
                IFile addedFile1 = await suby.Files.AddAsync("bert2.docx", System.IO.File.OpenRead($".{Path.DirectorySeparatorChar}TestAssets{Path.DirectorySeparatorChar}test.docx"));
                IFile addedFile2 = await suby.Files.AddAsync("bert3.docx", System.IO.File.OpenRead($".{Path.DirectorySeparatorChar}TestAssets{Path.DirectorySeparatorChar}test.docx"));

Using your code however exposed an issue with the EnsureFolderAsync() method, it did not work when ran on an existing folder. I did not get the same behavior you saw, but adding multiple documents in the same folder failed due to this. I’ve just pushed a fix (https://github.com/pnp/pnpcore/commit/e1947c191e74189a7e9549779944a402da4bc948) for that, will be part of tomorrow’s nightly release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pnpcore/docs/using-the-sdk/folders-intro.md at dev
When adding folders a very convenient method to use is the EnsureFolderAsync method as this one can take a folder path and verify...
Read more >
Is it possible to create a Document Set in library using ...
EnsureFolderAsync ("myNewFolder"); // load items for the document library var listWithItems = await list.GetAsync( p => p.Items, p => p.
Read more >
rushstack/libraries/node-core-library/src/FileSystem.ts at main
Monorepo for tools developed by the Rush Stack community - rushstack/libraries/node-core-library/src/FileSystem.ts at main · microsoft/rushstack.
Read more >
Sharepoint Integration (Extended) 2: Using PnP.Core
EnsureFolderAsync where EnsureFolder works like a GetOrCreate, so there are no problems if the folder is already present.
Read more >
Untitled
... adding a new IFolder into an IFolderCollection by using the Add methods or using EnsureFolderAsync. ... WebbI realize that the problem is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found