I have list column where save unique department number and I want to lock thread when add new list item, because when two users click in the same time button save - department number is duplicated.

I want to skip use of custom API where use C# lock statement..

Any idea how to do this?

This is my pnp code:

  let travelRequestNumber: string = null;

  let travelItem = await pnp.sp.web.lists.getByTitle(this.props.travelsListTitle).items
    .filter(`TravelsOffice eq  + '${officeName}'`)
    .orderBy("ID", false).top(1).get();

  if (travelItem.length === 0) {
    travelRequestNumber = officeName + ", №" + 1;
  else {
    let prevTravelNumberForOffice: string = travelItem[0].Title.split('№')[1];
    travelRequestNumber = officeName + ", №" + (parseInt(prevTravelNumberForOffice) + 1);

  return travelRequestNumber;

Later i save this number in list.

  • This is a bit hacky, but how about having a hidden dummy-list in SharePoint just to get an incremented number? When you create a item in that list SharePoint will give you a unique number (the item ID). It will be atomic, but it's ugly. – eirikb Apr 22 '18 at 18:09
  • Another option is to use an external service, e.g., Azure Functions with a sequential (1 batchSize) queue. Basically: webbook from the list to a http function, which adds to the queue, then a function trigger on the queue would update the item. – eirikb Apr 24 '18 at 8:46

When a list item is updated its Etag version passed is matched with the ETag version of list item currently on server.With ETag value we can check whether the values do match each other by the time the update operation is performed. If the values match, an update will be performed. If not, an error will be returned.

First extract Etag value from your list item when fetched.Then pass this Etag value to item.update() method of pnp js. In case of concurrency, error will be thrown by the server so you have to handle that exception. In there you will have to again call update method with updated value and ETag.

There will be additional hits on the server but it is the right way.

pnp js call is like this

listItem(PropertyObject,Etag value,listItemEntityTypeFullName)

by default * is passed as value for Etag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.