pnp / pnpcore

The PnP Core SDK is a modern .NET SDK designed to work for Microsoft 365. It provides a unified object model for working with SharePoint Online and Teams which is agnostic to the underlying API's being called
https://aka.ms/pnp/coresdk/docs
MIT License
298 stars 192 forks source link

GetComments() paging support doesn't work when selectors is passed #1424

Closed larry-lau closed 6 months ago

larry-lau commented 6 months ago

Category

Describe the bug

The new paging support introduced in 1.12 works when you call GetComments() without selectors parameters. It doesn't work when selectors is passed. i.e. It returns only 30 comments even when there are more on the page.

This is related to https://github.com/pnp/pnpcore/issues/1361

Steps to reproduce

See code block below Add the following unit test to the PagesTests.cs in PnP.Core.Test project Run the test without mocking turned off. Assertion failure since it returns 30 comments instead of 45 that get added in this test.

[TestMethod]
public async Task PageCommentingTestOnlyReturn30CommentsWithSelector()
{
    TestCommon.Instance.Mocking = false;
    using (var context = await TestCommon.Instance.GetContextAsync(TestCommon.TestSite))
    {
        IPage newPage = null;
        try
        {
            newPage = await context.Web.NewPageAsync();
            string pageName = TestCommon.GetPnPSdkTestAssetName("PageCommentingTestOnlyReturn30Comments.aspx");

            // Save the page
            await newPage.SaveAsync(pageName);

            // Publish the page, required before it can be liked
            newPage.Publish();

            // Get Page comments                
            var comments = newPage.GetComments();

            Assert.IsTrue(comments.Length == 0);

            var noCommentsAdded = 45;

            foreach (var i in Enumerable.Range(1, noCommentsAdded))
            {
                // Add a comment
                await comments.AddBatchAsync($"Comment #: {i} added by unit test");
            }

            await context.ExecuteAsync();

            comments = newPage.GetComments(
                p => p.Author,
                p => p.Text,
                p => p.ReplyCount,
                p => p.CreatedDate,
                p => p.Replies);

            // Expecting 45 but only 30 is returned. 
            Assert.IsTrue(comments.Length == noCommentsAdded);

        }
        finally
        {
            // Delete the page
            await newPage.DeleteAsync();
        }
    }
}

Expected behavior

Calling GetComments() with selectors parameter should return all comments. var comments = page.GetComments( p => p.Author, p => p.Text, p => p.ReplyCount, p => p.CreatedDate, p => p.Replies);

Environment details (development & target environment)

Additional context

After reviewing the code I noticed that in the code branch that handle when selectors is provided, the constructor call to ApiCall should have loadPages: true parameter. Specifically the BuildGetCommentsApiCallAsync method in ListItem.cs Line 1703: return new ApiCall(query.ApiCall.Request, ApiType.SPORest, receivingProperty: nameof(Comments), loadPages: true);

Thanks for your contribution! Sharing is caring.

jansenbe commented 6 months ago

@larry-lau : thanks for the detailed write up and analysis, I've fixed the code like you mentioned and updated the test case to cover this scenario. Fix will be part of the next nightly, closing issue now.