Requirement:
We have IDs of 1000 list items and want to fetch list items for only those particular IDs using Rest API with OR condition in the filter.
Problem Statement:
You can find batch utility code here on GitHub. Here is the code snippet for this solution:
We have IDs of 1000 list items and want to fetch list items for only those particular IDs using Rest API with OR condition in the filter.
Problem Statement:
- If we append all IDs in the filter parameter with OR condition, it will give REST API length limitation (255 characters).
- If we use API with OR condition for all IDs and use this API in a batch request, batch API will return a blank array.
- If we create API for each ID and create an array of APIs & use this array in a batch request, it will work for maximum up to 100 APIs only. As we have 1000 list items to be retrieved, this will also not work due to the 100 APIs limit with a single batch request.
Solution:
We can create API for 100 Item IDs with OR condition in the filter API and create a bunch of 100 APIs in an array and then use this array in a batch request.
You can find batch utility code here on GitHub. Here is the code snippet for this solution:
//Here we have imported batchutility.ts
import { BatchUtils } from "../../BatchUtils";
public componentDidMount() {
var listItemIds = [1, 2, 3, 4, 5, 6, ..............................., 1000];
var url = this.props.SPUrl + "/_api/web/lists/getbytitle('" + this.props.projectPhaseListName + "')/items?$filter=";
this.getDashboardBatchData(listItemIds, 0, listItemIds.length, url);
}
//Below function will create bunch of filters with max of 100 id in filter with or condition
public getDashboardBatchData(listItemIds, Index, totalCount, url) {
var loopLen = Index + 100;
if (Index <= totalCount) {
var filterString = "";
var tempApi = '';
var callNext = true;
if (totalCount > Index && totalCount < loopLen) {
loopLen = totalCount;
callNext = false;
}
for (var i = Index; i < loopLen; i++) {
if (filterString == '') {
filterString = "ID eq " + listItemIds[i];
}
else {
filterString += " or ID eq " + listItemIds[i];
}
}
tempApi = url + filterString;
dashboardBatchArray.push(tempApi);
if (callNext) {
this.getDashboardBatchData(listItemIds, loopLen, totalCount, url);
}
else {
//dashboardBatchArray will have all APIs with max of 100 filters for ID
this.processBatch(dashboardBatchArray);
}
}
else {
//dashboardBatchArray will have all APIs with max of 100 filters for ID
this.processBatch(dashboardBatchArray);
}
}
//Below funcion will create bunch of 100 APIs and will be used in batch request
public processBatch(dashboardBatchArray){
var index = 0;
var arrayLength = dashboardBatchArray.length;
var tempArray = [];
var chunk_size = 100;
//Below code will create array of 100 APIs in single bunch
for (index = 0; index < arrayLength; index += chunk_size) {
var myChunk = dashboardBatchArray.slice(index, index + chunk_size);
tempArray.push(myChunk);
}
//Below code will execute all apis of tempArray
let listItemsArray = [];
for (var i = 0; i < tempArray.length; i++) {
await BatchUtils.GetBatchAll({ rootUrl: this.props.SPUrl, FormDigestValue: '', batchUrls: tempArray[i] }).then((batchResult) => {
if (batchResult.length > 0) {
for (var i = 0; i < batchResult.length; i++) {
for (var j = 0; j < batchResult[i].d.results.length; j++) {
listItemsArray.push(batchResult[i].d.results[j]);
}
}
}
}
}
//You will see all 1000 items in listItemsArray variable
console.log(listItemsArray);
}
If you have any questions you can reach out our SharePoint Consulting team here.