Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Working With: Items

Patrick Rodgers edited this page Jan 20, 2017 · 24 revisions

Working With: List Items

GET

Getting items from a list is one of the basic actions that most applications require. This is made easy through the library and the following examples demonstrate these actions.

Basic Get

import pnp from "sp-pnp-js";

// get all the items from a list
pnp.sp.web.lists.getByTitle("My List").items.get().then((items: any[]) => {

    console.log(items);
});

// use odata operators for more efficient queries
pnp.sp.web.lists.getByTitle("My List").items.select("Title", "Description").top(5).orderBy("Modified", true).get().then((items: any[]) => {

    console.log(items);
});

Add Items

There are several ways to add items to a list. The simplest just uses the add method of the items collection passing in the properties as a plain object.

import { default as pnp, ItemAddResult } from "sp-pnp-js";

// add an item to the list
pnp.sp.web.lists.getByTitle("My List").items.add({
    Title: "Title",
    Description: "Description"
}).then((iar: ItemAddResult) => {

    console.log(iar);
});

Add Multiple Items

import pnp from "sp-pnp-js";

let list = pnp.sp.web.lists.getByTitle("rapidadd");

list.getListItemEntityTypeFullName().then(n => {

    let batch = pnp.sp.web.createBatch();

    list.items.inBatch(batch).add({ Title: "Batch 6" }, n).then(b => {

        console.log(b);

    });

    list.items.inBatch(batch).add({ Title: "Batch 7" }, n).then(b => {

        console.log(b);

    });

    batch.execute().then(d => console.log("Done"));
});

Add in a Batch

Update & Delete

Clone this wiki locally