Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: Batch Query Using sp-pnp-js
In general, if we want to send multiple queries to SharePoint, we use a batch statement like below.
Example
If I want to filter multiple lists with the below queries in page load/any other events.
- Loading all lists available in the site.
- Check available of list “XYZ”
- Employee list whose department is “IT”
- Retrieving regional settings information.
Generally, we will follow the following way.
The above code will work and give the expected result, but here, if the number of batch operations increases, the code itself will be odd to read (if we consider reusability of the code as developer’s main goal).
Resolution
Here, we need to think about the usage of For loops in this scenario. So, we require an array/object for looping all the queries, if we consider the below portion of code as an array object.
Code Implementation Process
If I want to retrieve data from a SharePoint list (single query) based on the implemented code, I will follow the below process.
In SP-PnP-JS, web is an object of O365 Web(pnp.sp.web). This object has lists property and it is an object of Lists class(pnp.sp.web.lists). This lists property has a method named getByTitle(pnp.sp.web.lists.getByTitle(“XYZ”)) which accepts list name as a parameter and which will return the slected list as List class Object. This List Class has property named items which will return Items class object(pnp.sp.web.lists.getByTitle(“XYZ”).items) . This Items class is extending SharePointQuerable class (so all methods in this are internally used for our original items class) which has get method which will return our data.
Based on the above process of execution and based on JavaScript object feasibility, I thought if we form these all into one array object, which will have information about our query chaining. So, I have just followed the below way of approach to achieve our requirement.
For getting data in a SharePoint List (single query), we need to follow the below steps for forming input array object for reusable method.
- By default, we need to create SharePoint Web object (for any kind of GET operations we need to have this is a basic object)
1let webObject = pnp.sp.web; - With the help above web object, our next goal to access lists property which will return an object Lists class, so I have used this is my first string in the array.
1let batchLoadQueries = [“lists"]; - Based on above Lists class, it has method getByTitle() which accepts list name as a parameter and returns selected list, so it needs two parameters under one array index so we can use this as key-value pair and push it to our above batchLoadQueries array and our array will be as below.
1["lists", { "getByTitle": "Employee" }] - Based on the above query, we are navigated up to lists, so we need to retrieve the items of the list. It is the single property that doesn’t need any kind of key-value pair. So array will be like below
1["lists", { "getByTitle": "Employee" },"items"]
Up to the above object, it will return items, If it has any kind query/select and these two are key-value pairs so we need to form array as below
1["lists", { "getByTitle": "Employee" }, "items", { "filter": "Dept eq IT" }]
If we want to send multiple batch statements, we need to form array of array objects.
Eg
1 |
let batchLoadQueries = [[“lists", { "filter": "Hidden eq false and BaseTemplate eq 100" }],["lists", { "getByTitle": "XYZ" }], ["lists", { "getByTitle": "Employee" }, "items", { "filter": "Dept eq IT" }], ["regionalSettings", "timeZone"]]; |
Reusable Method(Complete Code please download from attachment)
Print article | This entry was posted by Peter on July 11, 2019 at 7:13 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |