inside my .net core console application what is the differences between items and items.AsRequested

john john Pter 1,550 Reputation points
2026-06-19T18:33:28.27+00:00

I have this code to retrive items from sharepoint list and print their Titles:-


//using AngleSharp.Css.Dom;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PnP.Core.Auth;
using PnP.Core.Model.SharePoint;
using PnP.Core.Model.Teams;
using PnP.Core.QueryModel;
using PnP.Core.Services;
using PnP.Core.Services.Builder.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;


//using System.Security.Cryptography.X509Certificates;
using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext;
using static System.Net.Mime.MediaTypeNames;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ConsoleApp4
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var tenantId = "**";
            var clientId = "***
            var certificatePath = @"c:\CERT\Reporting.pfx";
            var certificatePassword = "***";

            // Initialize a new service collection
            var serviceCollection = new ServiceCollection();

            // Load the certificate
            var certificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.Exportable);

            // Configure logging
            serviceCollection.AddLogging(builder =>
            {
                builder.AddConsole();
            });

            // Add and configure PnP Core SDK
            serviceCollection.AddPnPCore(options =>
            {
                options.PnPContext.GraphFirst = true; // Set true if you prefer to use Graph over CSOM when possible
                                                      // options.HttpRequests.UserAgent = "ISV|Contoso|ProductX";
                options.Sites.Add("SiteToWorkWith", new PnPCoreSiteOptions
                {
                    SiteUrl = "https://****.sharepoint.com/sites/*****",
                    AuthenticationProvider = new X509CertificateAuthenticationProvider(clientId, tenantId, certificate)
                });
            });

            // Build the service provider
            var serviceProvider = serviceCollection.BuildServiceProvider();

            // Use the service provider to get the IPnPContextFactory instance
            var pnpContextFactory = serviceProvider.GetRequiredService<IPnPContextFactory>();

            // Now you can use the IPnPContextFactory to get a PnPContext and perform operations
            var context = await pnpContextFactory.CreateAsync("SiteToWorkWith");


            var workOrderList = context.Web.Lists.GetByTitle("Work Orders", p => p.Title,
                                                                 p => p.Fields.QueryProperties(p => p.InternalName,
                                                                                               p => p.FieldTypeKind,
                                                                                               p => p.TypeAsString,
                                                                                               p => p.Title));
            //Posting UnPosting Forms
            // Build a query that only returns these fields for the Work Orders
            string viewXml = @"<View Scope='RecursiveAll'>
                    <ViewFields>
                      <FieldRef Name='Title' />
                      <FieldRef Name='Created' />
                      <FieldRef Name='FileLeafRef' />
<FieldRef Name='LocationName' />
                    </ViewFields>
                                        <Query>
<Where>
<Eq>
        <FieldRef Name='LocationName' />
        <Value Type='Text'>Location Main</Value>
    </Eq>
</Where>
                    </Query>
<RowLimit Paged='TRUE'>5000</RowLimit>
                   </View>";

            // Load all the needed data using paged requests
            bool paging = true;
            string nextPage = null;
            while (paging)
            {
                var output = await workOrderList.LoadListDataAsStreamAsync(new RenderListDataOptions()
                {
                    ViewXml = viewXml,
                    RenderOptions = RenderListDataOptionsFlags.ListData,
                    Paging = nextPage ?? null,
                }).ConfigureAwait(false);

                if (output.ContainsKey("NextHref"))
                {
                    nextPage = output["NextHref"].ToString().Substring(1);
                }
                else
                {
                    paging = false;
                }
            }

            foreach (var item in workOrderList.Items)
            {
                Console.WriteLine(item["Title"]);
             

            }
        }
    }
}

now this is simply printing all the items, and not just the ones requested by the CAML query. now if i add AsRequested() to foreach (var item in workOrderList.Items.AsRequested()), everything will work fine.. so what is going on?

Thanks

Microsoft 365 and Office | SharePoint | Development
0 comments No comments

1 answer

Sort by: Most helpful
  1. Vergil-V 15,775 Reputation points Microsoft External Staff Moderator
    2026-06-20T05:25:45.53+00:00

    Hi john john Pter 

    The behavior you are seeing is expected and works as designed within the SDK. 

    Based on the PnP Core SDK documentation PnP Core SDK documentation on IQueryable performance considerations:

    image

    .AsRequested() is used to ensure that the SDK only works with the data that has already been loaded into memory through methods such as LoadListDataAsStreamAsync, without triggering any additional requests. 

    The documentation also highlights that directly iterating over a collection after performing an explicit load can lead to unintended additional queries. Because of this, using .AsRequested() is the recommended approach to avoid extra calls and to improve performance. 

    I hope this provides clearer insight.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.