0.5.2 • Published 1 month ago

@b-jones-rfd/sp-rest-connect v0.5.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 month ago

GitHub Actions CI npm version npm bundle size code style: prettier

Sharepoint REST Connect

Use SharePoint Rest Services to interact with lists and document libraries. I got tired of rewriting these for work projects. Still building and not ready for use.

Prerequisites

This project requires NodeJS and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine, try running the following command.

$ npm -v && node -v
10.2.4
v20.11.1

PNPM is a awesome alternative to NPM and is recommended.

Table of contents

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Installation

BEFORE YOU INSTALL: please read the prerequisites

To install and set up the library, run:

$ npm i @b-jones-rfd/sp-rest-connect

Or if you prefer using Yarn:

$ yarn add @b-jones-rfd/sp-rest-connect

Or for PNPM:

$ pnpm add @b-jones-rfd/sp-rest-connect

Usage

Instance Methods

Actions can be performed against site collection lists or document libraries using instance methods on a SiteCollection instance.

import { createSiteConnection } from '@b-jones-rfd/sp-rest-connect'

const siteConnectionOptions = {
  username: 'tim',
  password: 'myexceptionalsecurepassword',
  site: 'my.sharepoint.com',
  serverRelativeUrl: '/path/to/my/site',
  protocol: 'https' as const,
}

const connection: SiteConnection = createSiteConnection(siteConnectionOptions)

const params = new URLSearchParams({
  $select: 'Id,Title',
  $top: '200',
})

async function getMyListUsingConnection(listName: string) {
  const contents = await connection.getListContents({ listName, params })
  if (contents.success) return contents.data
  else throw new Error(contents.error)
}

Factory Action Methods

Additionally, for single use or reduced import size, action factory methods can be imported directly. Call the factory method with a SiteConnectionOptions object to return an asynchronous action function that can be called directly.

import { getListContents } from '@b-jones-rfd/sp-rest-connect'

async function getMyListUsingAction(listName: string) {
  const action = getListContents(connectionOpts)
  const contents = await action({ listName, params })
  if (contents.success) return contents.data
  else throw new Error(contents.error)
}

API

createSiteConnection

SiteConnectionOptions

username

TypeDescriptionExample
stringSharePoint username'user'

password

TypeDescription
stringSharePoint password

site

TypeDescriptionExample
stringSharePoint domainsharepoint.domain.com

serverRelativeUrl

TypeDescriptionExample
stringSharePoint site relative Url/path/to/my/site

protocol

TypeDefault valueOptionsDescription
string'https''http' or 'http'Site protocol

domain

TypeDefault valueDescription
string''NTLM domain

hostname

TypeDefault valueDescription
stringos.hostname()OS Hostname

Actions

SiteConnection instance action methods.

export type Action<TConfig, TResponse> = (
  options: TConfig
) => Promise<Result<TResponse>>

If using the actions directly call the factory method with a SiteConnectionOptions object to return an action that can be used to execute a SharePoint action.

addAttachmentToListItem(options)

options

PropertyTypeDescriptionRequired
accessTokenstringSharePoint access tokenY
listNamestringSharePoint list nameY
spIdnumberSharePoint list item IDY
fileNamestringFile nameY
payloadBufferFile contentsY

addDocumentToLibrary(options)

options

PropertyTypeDescriptionRequired
accessTokenstringSharePoint access tokenY
folderstringSharePoint folder nameY
fileNamestringFile nameY
payloadBufferFile contentsY

addListItem(options)

options

PropertyTypeDescriptionRequired
accessTokenstringSharePoint access tokenY
listNamestringSharePoint list nameY
payloadstringList item JSON as stringY

checkFolderExistsInLibrary(options)

options

PropertyTypeDescriptionRequired
folderstringSharePoint folder/subfolder nameY

createFolderInLibrary(options)

options

PropertyTypeDescriptionRequired
accessTokenstringSharePoint access tokenY
folderstringSharePoint folder/subfolder nameY

deleteDocumentFromLibrary(options)

options

PropertyTypeDescriptionRequired
accessTokenstringSharePoint access tokenY
folderstringSharePoint folder/subfolder nameY
fileNamestringFile nameY

deleteListItem(options)

options

PropertyTypeDescriptionRequired
accessTokenstringSharePoint access tokenY
listNamestringSharePoint list nameY
spIdnumberSharePoint list item IDY

getAuthToken()

getFormDigestValue()

getDocumentFromLibrary(options)

options

PropertyTypeDescriptionRequired
folderstringSharePoint folder nameY
fileNamestringFile nameY

getListContents(options)

options

PropertyTypeDescriptionRequired
listNamestringSharePoint list nameY
paramsUrlSearchParamsSharePoint list nameN

getListItem(options)

options

PropertyTypeDescriptionRequired
listNamestringSharePoint list nameY
spIdnumberSharePoint list item IDY

getListItemType(options)

options

PropertyTypeDescriptionRequired
listNamestringSharePoint list nameY

updateListItem(options)

options

PropertyTypeDescriptionRequired
accessTokenstringSharePoint access tokenY
listNamestringSharePoint list nameY
spIdnumberSharePoint list item IDY
patchstringUpdated list item JSON as stringY

Responses

Responses are provided based on the Result type. Success can be determined by checking the success property.

export type Result<TResponse> = Success<TResponse> | Failure

Success

Response is returned in the data property.

type Success<TResponse> = { success: true; data: TResponse }

Failure

Errors are returned in the error property.

type Failure = { success: false; error: string }

Contributing

This is a pet project to save me time at work. It is still under development and not ready for use.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

MIT License

0.5.2

1 month ago

0.5.1

3 months ago

0.5.0

3 months ago

0.4.9

4 months ago

0.4.8

4 months ago

0.4.7

4 months ago

0.4.6

1 year ago

0.4.5

1 year ago

0.4.4

1 year ago

0.4.3

1 year ago

0.4.2

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.0.3

1 year ago