Episode #148​ – Understanding PnP Core SDK Query Model

Here you can find the transcript of Episode #148 of PiaSys TechBites.

Welcome back to PiaSys Tech Bites.

Today, we keep on talking about the PnP Core SDK library. Specifically I want to explain you how you can query data using PnP Core SDK. In fact, in PnP Core SDK we have different queering flavors. You can load data into the domain model that you already have in memory. You can get data into a new variable based on the domain model type that we provide with PnP Core SDK or you can use the Language Integrated Query model to query collections of objects, as well as, you can eventually use the hierarchical or nested queries to include additional complex collections or properties in a query of a parent type.

Moreover, there are other options, like for example, the continuous paging or the cascading loads that you can use whenever you want to create really advanced queries. From an inner working point of view with PnP Core SDK, we always use batching by default when we make queries so that we can improve performances, reduce the number of round trips to the REST APIs that we are going to consume, and avoid as much as we can that throttling issues.

Again, remember that PnP Core SDK internally is Graph First. It relies on the SharePoint REST APIs, just as a fall-back whenever there is any, not yet available functionality in Microsoft Graph. So let me move to the demo environment. Let me show you how to play with the query model of PnP Core SDK.

Here is the official documentation page about how to request data using the Microsoft 365 PnP Core SDK library. Let me switch to the demo application that I’ve built for you. Let me explain you how you can play with the query model. For example, here we create a PnPContext object using the PnPContextFactory that we provide in PnP Core SDK. We can simply and easily use the LoadAsync method to load a bunch of properties using these lambda expressions to declare the property that we want to load.

Those properties will be loaded into the web object that we have in the current context. That we can simply say then context.Web.title. For example, to read the property that we just loaded using the LoadAsync. There is also the synchronous method, so the Load but I suggest you, and invite you to give priority to the asynchronous model when you develop your solutions.

There is also the capability to do the Load and LoadAsync with batching so that you can provide a specific batch that you want to use when you load the data. By default, the LoadAsync will do batching for you under the cover, and will simply load the declared the properties into the target object in the current context. Once you have done that, here we load for example, the collection of lists in the current web of the context. You can also browse the lists and if you do that, and you want to use the list that you just loaded in memory with this LoadAsync method, you need to specify the .AsRequested, extension method to query and to go through all of the lists that you already have in memory in the current web object in the current context.

Otherwise, if you will make for each, or any collection in PnP Core SDK the result will be a live query executed under the cover for you using the query model of a PnP Core SDK. This is to give you always the latest and fresh data, whenever you browser a collection of items like a collection of lists or a collection of messages in a chat in Teams or stuff like that.

Moreover, you can also play with the Language Integrated Query syntax. You can use the query extension methods. There are some out of the box query methods supported, as well as some custom methods that we introduced in PnP Core SDK. Here, we use the current context, the web in the current context and the collection of lists. Again, we use that collection to order the results by title using the lambda expression and to query specifically just the ID and the Title of all of the lists. The results will be a new collection in memory that you can use. Actually this object will be just the IQueryable object, the query definition. Whenever you will go through all of the items with a for each or a ToList or ToArray, we will execute the query in that point in time so that you can use and reuse the same query multiple times. Whenever you will browse the query, you will get fresh results based on the query that you previously defined.

If you want, you can also use the link query syntax. For example, from l in context.web.lists, you specify a constraint about the template type of the lists. For example, using an enumerable type that we have in a PnP Core SDK in our domain model. You order by Title and you select items. Once you’ve done that, you apply the custom extension method, that we provide QueryProperties, which will simply load the Title and the ID of all of the lists matching the query criteria that we defined with this LINQ query. Once you’ve done that again, this query list LINQ object is just the query Expression Tree. You will go through all of the items whenever you will do a foreach or a ToList, or a ToArray. Then you will be able to get back the results.

Last but not least. There is the Get model. When you use the Get model, you do Get synchronous or a GetAsync, which is the asynchronous model and the suggested one. Again you can also use the batching model if you want, but by default, we always use batching inside these methods. But using the batch one, you can specify your own custom batch if you want. But here we simply make a get asynchronously. We retrieve the properties Title and the collection of Lists of the current web, including the ID and the Title only of the lists. We get the result in a new web object, which will not be the context.web object, but a new one that you can use for whatever reason. Because maybe you want to keep a copy of the web object with this specific properties stored in this new web object. And once you’ve done that again, you can query the properties that you retrieved, or you can use, the AsRequested the collection of lists that you have got making these queries.

So really, really simple syntax. I can even execute this application to show you the result, even if it is more important to understand the the idea under the cover. Here we make the very first request. We get the Title and the collection of lists in the current web in the context, and here we are, we get the Title and we get the collection of lists. Then we make another query. But now, as you can see, first we used the AsRequested, now we go through the collection and so we make another REST query area to get the new collection of lists. Then we make the LINQ query or the by title. Here we are, we make another REST query and we get the collection of lists, ordered by Title, with ID and Title as the result. Then we will have the query filtered by template type. We will just get back the document libraries. Here we are, we just get them still ordered by Title.

The last one will be the query to get the web object. We will get the Title as you see here, as well as the collection of lists. Getting just the ID and the Title of every single list in the web object that we will create on the fly for you. This is a very useful and powerful query model, and it is up to you, based on what you want to do to use the Load or the Get model, or to use the Language Integrated model, if you want to play with complex queries against collection of objects in the domain model of PnP Core SDK.

Like always thank you for watching this video. I hope you found it interesting and I’m looking forward to seeing you next week and remember like always to subscribe to this channel. Thank you.