npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sp-batch-builder

v0.0.1

Published

A utility to simplify building batch requests in SharePoint.

Downloads

6

Readme

SharePoint Batch Builder

A utility to simplify building batch requests in SharePoint.

This utility was adapted and extended from https://github.com/SteveCurran/sp-rest-batch-execution/blob/master/RestBatchExecutor.js.

For the Microsoft documentation on batch requests, please see https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/make-batch-requests-with-the-rest-apis.

Batch requests allow you to send multiple create/read/update/delete operations all with one request. While this SharePoint REST API greatly reduces network chatter, building a batch request is not so straight forward.

A batch request is sent in the body of a POST request even though you can send GET, POST, MERGE, and DELETE requests together.

From the batch body example below, it's easy to tell building a batch request is hard:

--batch_8890ae8a-f656-475b-a47b-d46e194fa574
Content-Type: multipart/mixed; boundary=changeset_f9c96a07-641a-4897-90ed-d285d2dbfc2e
Content-Length: 1762
Content-Transfer-Encoding: binary

--changeset_f9c96a07-641a-4897-90ed-d285d2dbfc2e
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1
processData: true

POST https://<my-sp-site-url>/_api/web/lists(guid'<my-list-guid>')/items HTTP/1.1
accept:application/json;odata=verbose
Content-Type: application/json;odata=verbose

{"Title":"My Title 1","__metadata":{"type":"SP.Data.<SomeType>ListItem"}}

--changeset_f9c96a07-641a-4897-90ed-d285d2dbfc2e
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2
processData: true

POST https://<my-sp-site-url>/_api/web/lists(guid'<my-list-guid>')/items HTTP/1.1
accept:application/json;odata=verbose
Content-Type: application/json;odata=verbose

{"Title":"My Title 2","__metadata":{"type":"SP.Data.<SomeType>ListItem"}}

--changeset_f9c96a07-641a-4897-90ed-d285d2dbfc2e
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 3
processData: true

DELETE https://<my-sp-site-url>/_api/web/lists(guid'<my-list-guid>')/items(25) HTTP/1.1
If-Match: "1"
accept:application/json;odata=verbose

--changeset_f9c96a07-641a-4897-90ed-d285d2dbfc2e
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 4
processData: true

DELETE https://<my-sp-site-url>/_api/web/lists(guid'<my-list-guid>')/items(1) HTTP/1.1
If-Match: "2"
accept:application/json;odata=verbose

--changeset_f9c96a07-641a-4897-90ed-d285d2dbfc2e--

--batch_8890ae8a-f656-475b-a47b-d46e194fa574--

The Batch Builder utility greatly simplifies building a batch request. For example, sending 2 insert, 2 update, and 2 delete requests:


const siteUrl = 'https://my-sharepoint-site.com/sites/my-site';
const listGuid = '<my-list-guid>';
const listName = '<PascalCaseListName>';
const listItemType = `SP.Data.${listName}ListItem`;

// Instantiate batch builder.
const batchExec = new SpBatchBuilder(siteUrl);

// New list items to insert.
const toInsert = [{Title: 'My Title 1'}, {Title: 'My Title 2'}];

// Existing list item values to update.
const toUpdate = [{Id: 1, Title: 'My Title 3', etag: '*'}, {Id: 2, Title: 'My Title 4', etag: '*'}];

// Existing list items to delete.
const toDelete = [{Id: 1, etag: '*'}, {Id: 2, etag: '*'}];

toInsert.forEach((item) => {
    batchExec.insert(siteUrl, listGuid, item, listItemType);
});

toUpdate.forEach((item) => {
    batchExec.update(siteUrl, listGuid, item, listItemType, item.etag);
});

toDelete.forEach((item) => {
    batchExec.delete(siteUrl, listGuid, item.Id, item.etag);
});

batchExec.executeAsync().done((result) => {
    console.info(result);
});

You may also chain operations. For example:

    //...cont'd

    batchExec
        .insert(siteUrl, listGuid, {Title: "Don't Panic"}, listItemType)
        .delete(siteUrl, listGuid, 42, '*')
        .executeAsync().done((result) => {
            console.info(result);
        });