Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Folder Async hits the Sharepoint Limit of 5000 #793

Closed
1 task done
ChrTall opened this issue Mar 21, 2022 · 4 comments
Closed
1 task done

Ensure Folder Async hits the Sharepoint Limit of 5000 #793

ChrTall opened this issue Mar 21, 2022 · 4 comments
Assignees
Labels
area: model 📐 Related to the core SDK models bug Something isn't working

Comments

@ChrTall
Copy link

ChrTall commented Mar 21, 2022

Category

  • Bug

Describe the bug

If you use EnsureFolderAsync for a Folder in a large document Library that has more than 5000 SubFolders a SPQueryThrottledException is thrown, because we hit the 5000 ListItem Limit of Sharepoint Online, which can not be increased, can it?

Steps to reproduce

You need a large document Library with lots of SubFolders, in order to hit the 5000 ListItem Limit.
Load the Folder into a variable and execute EnsureFolderAsync("TestFolder/Test/Test").

  1. See code var site = await targetSiteContext.Web.Lists.GetByTitleAsync("VeryBigLib"); var rootFolder = await site.RootFolder.GetAsync(); await rootFolder.EnsureFolderAsync("/FolderWithManySubFolders/TEST/TEST/TEST");

Expected behavior

It would be great if this could be implemented in a way that you can rely on it even when your library grows over time and eventually will hit the limit.

Environment details (development & target environment)

  • SDK version: 1.5.0
  • OS: Windows 10
  • SDK used in: Console App and Asp.Net Core
  • Framework: .NET 6
  • Browser(s): --
  • Tooling: Jetbrains Rider
@MathijsVerbeeck
Copy link
Contributor

@jansenbe I've replicated this issue and the error happens in the function "EnsureFolderAsync" in Folder.cs on line 324 when the subfolders are being loaded. The error being thrown is that the list view item limit has been exceeded. What we might do is check the value of "ItemCount" of the currentfolder and when this exceeds 5000 we'll have to make an adjustment to load the subfolders on a different way than using the REST API call.

@Ironbell
Copy link

@MathijsVerbeeck @ChrTall as we had this problem as well with the current inefficient implementation of EnsureFolderAsync, see my workaround here:

private static async Task<IFolder> EnsureFolderAsync(string directoryPath, IFolder rootFolder)
        {
            var currentFolder = rootFolder;

            var childFolderNames = directoryPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            var context = rootFolder.PnPContext;

            await rootFolder.LoadAsync(p => p.ServerRelativeUrl);

            var currentUrl = rootFolder.ServerRelativeUrl;
            var currentFolderWasCreated = false;

            foreach (var folderName in childFolderNames)
            {
                currentUrl = $"{currentUrl}/{folderName}";

                if (!currentFolderWasCreated)
                {
                    try
                    {
                        var nextFolder = await context.Web.GetFolderByServerRelativeUrlAsync(currentUrl);

                        currentFolder = nextFolder;
                    }
                    catch (PnP.Core.SharePointRestServiceException)
                    {
                        currentFolderWasCreated = true;
                        currentFolder = await currentFolder.AddFolderAsync(folderName);
                    }
                }
                else
                {
                    currentFolder = await currentFolder.AddFolderAsync(folderName);
                }
            }

            return currentFolder;
        }

Hope it helps!
Cheers,
Isabelle

@jansenbe
Copy link
Contributor

@Ironbell : excellent approach! I'll update our implementation with your code. Thanks for sharing the fix :-)

@jansenbe jansenbe self-assigned this Mar 23, 2022
@jansenbe jansenbe added bug Something isn't working area: model 📐 Related to the core SDK models labels Mar 23, 2022
jansenbe added a commit that referenced this issue Mar 23, 2022
…t the need to load all folders on a level, this way the method works on libraries with a large amount of folders #793
@jansenbe
Copy link
Contributor

@Ironbell : just integrated and tested your code, works like a charm in our test cases! @ChrTall : the updated implementation will be part of the next nightly release (version 1.5.99 or higher). Will be closing this issue now, but don't hesitate to re-open in case you still see issues with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: model 📐 Related to the core SDK models bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants