Skip to content

Commit

Permalink
Merge pull request #3057 from bcameron1231/fix-3055
Browse files Browse the repository at this point in the history
Update Docs for IItemAddResult
  • Loading branch information
juliemturner authored Jun 5, 2024
2 parents 8bcaffd + 3278238 commit e6f8b37
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 41 deletions.
38 changes: 0 additions & 38 deletions docs/concepts/batching.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,41 +217,3 @@ await execute();
```

> In addition you cannot continue using a batch after execute. Once execute has resolved the batch is done. You should create a new batch using one of the described methods to conduct another batched call.
## Case where batch result returns an object that can be invoked

In the following example, the results of adding items to the list is an object with a type of **IItemAddResult** which is `{data: any, item: IItem}`. Since version v1 the expectation is that the `item` object is immediately usable to make additional queries. When this object is the result of a batched call, this was not the case so we have added additional code to reset the observers using the original base from witch the batch was created, mimicing the behavior had the **IItem** been created from that base withyout a batch involved. We use [CopyFrom](../core/behaviors.md#CopyFrom) to ensure that we maintain the references to the InternalResolve and InternalReject events through the end of this timelines lifecycle.

```TypeScript
import { createBatch } from "@pnp/sp/batching";
import { SPDefault } from "@pnp/nodejs";
import { IList } from "@pnp/sp/lists";
import "@pnp/sp/items/list";

const sp = spfi("https://tenant.sharepoint.com/sites/dev").using(SPDefault({ /* ... */ }));

// in one part of your application you setup a list instance
const list: IList = sp.web.lists.getByTitle("MyList");

const [batchedListBehavior, execute] = createBatch(list);
// this list is now batching all its requests
list.using(batchedListBehavior);

let res: IItemAddResult[] = [];

// these will all occur within a single batch
list.items.add({ Title: `1: ${getRandomString(4)}` }).then(r => res.push(r));
list.items.add({ Title: `2: ${getRandomString(4)}` }).then(r => res.push(r));
list.items.add({ Title: `3: ${getRandomString(4)}` }).then(r => res.push(r));
list.items.add({ Title: `4: ${getRandomString(4)}` }).then(r => res.push(r));

await execute();

let newItems: IItem[] = [];

for(let i=0; i<res.length; i++){
//This line will correctly resolve
const newItem = await res[i].select("Title")<{Title: string}>();
newItems.push(newItem);
}
```
5 changes: 2 additions & 3 deletions docs/sp/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,16 @@ import { spfi } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import { IItemAddResult } from "@pnp/sp/items";

const sp = spfi(...);

// add an item to the list
const iar: IItemAddResult = await sp.web.lists.getByTitle("My List").items.add({
const item = await sp.web.lists.getByTitle("My List").items.add({
Title: "Title",
Description: "Description"
});

console.log(iar);
console.log(item);
```

### Content Type
Expand Down

0 comments on commit e6f8b37

Please sign in to comment.