Handling batch request exception #1311
-
I'm in a case where I have to post all items in a JSON file to the SP list. I intend to group each 20 items into 1 batch. The thing seems to work well but sometimes I get an exception that
Also, the total number of items on the Sharepoint list exceeds the number in my JSON file (2356 vs 2000). I don't know which part I did wrong, also is there any way to handle the batch request like removing the item that caused an exception from the batch and putting it into another patch to re-process later? int addCount = 0;
int updateCount = 0;
string viewXml = CAMLQueryGenerator();
var listExistingItem = await GetWithCalmQuery(myList, viewXml);
var addBatch = new Batch();
var updateBatch = new Batch();
foreach (var user in Data)
{
try
{
// Execute the upsert procedure
if (!listExistingItem.Any()
|| !listExistingItem.Where(p => p["EmployeeNumber"].ToString() == user.personIdExternal).Any())
{
if (addCount == 20)
{
await this._context.ExecuteAsync(addBatch);
addBatch = new Batch();
addCount = 0;
}
await Add(this._context, myList, user, addBatch);
addCount++;
}
else
{
if (updateCount == 20)
{
await this._context.ExecuteAsync(updateBatch);
updateBatch = new Batch();
}
await Update(this._context, myList, viewXml, user, updateBatch);
updateCount++;
}
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
Console.WriteLine("Here is user ID employee: " + user.personIdExternal);
}
}
await this._context.ExecuteAsync(addBatch);
await this._context.ExecuteAsync(updateBatch); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@ngqtrieu99 : see https://pnp.github.io/pnpcore/using-the-sdk/basics-batching.html#handling-batch-failures. You can execute the batch without it throwing errors, instead you get back a collection of errors and you can decide how to handle them (e.g. re submit via a new batch). Also you don't have to worry about the number of requests in the batch, if a batch is too big we'll automatically split it into multiple batch calls (see https://pnp.github.io/pnpcore/using-the-sdk/basics-batching.html#batch-limits) |
Beta Was this translation helpful? Give feedback.
@ngqtrieu99 : see https://pnp.github.io/pnpcore/using-the-sdk/basics-batching.html#handling-batch-failures. You can execute the batch without it throwing errors, instead you get back a collection of errors and you can decide how to handle them (e.g. re submit via a new batch).
Also you don't have to worry about the number of requests in the batch, if a batch is too big we'll automatically split it into multiple batch calls (see https://pnp.github.io/pnpcore/using-the-sdk/basics-batching.html#batch-limits)