SpRestLib

SpRestLib

  • Download
  • Get Started
  • API Documentation
  • SharePoint Guides
  • GitHub

›API Reference

Get Started

  • Installation
  • Promise You Will Love It
  • Promise-based Operations

Features

  • Library Integration
  • Library Options
  • SharePoint via Node.js
  • Utility Methods

API Reference

  • List/Library Methods (SP.List)
  • File Methods (SP.File)
  • Folder Methods (SP.Folder)
  • Site Methods (SP.Web)
  • User Methods (SP.User)
  • REST API Methods

SpRestLib UI

  • Form Binding

Help / Support

  • SharePoint Authentication

List/Library Methods (SP.List)

Syntax

Lists can be accessed by either their name or their GUID:

sprLib.list( 'name' )
sprLib.list( 'GUID' )
sprLib.list({ name:'name' })
sprLib.list({ guid:'GUID' })
sprLib.list({ name:'name', baseUrl:'/path/to/list', requestDigest:'formDigestValue' })

Options

PropTypeRequired?DescriptionPossible Values
namestringYlist name or list GUIDEx:{'name': 'Employees'}
guidstringlist GUID (alias for name)Ex:{'guid': '8675309-ab3d-ef87b08e09e1'}
baseUrlstringlocation of listRelative or full path. Ex:{'baseUrl': '/sites/dev'}
requestDigeststringthe request form digest security tokenEx:{'requestDigest': 'ABC123'}

baseUrl

By default, the base URL is set to where the host webpart is located (_spPageContextInfo.webServerRelativeUrl). However, there are occasions when reading from other locations - like a subsite - is desired. Use the baseUrl parameter to specify the desired location.

requestDigest

By default, the request digest is set to the <input id="__REQUESTDIGEST"> form element value if one exists (e.g.: WebPart in an .aspx page). Security tokens are required for certain SharePoint operations like creating or updating List items. If your application is not inside a SharePoint .aspx page, you will need to obtain the digest value and pass it when it's needed.

Example: How to obtain a FormDigestValue

sprLib.rest({ url:'_api/contextinfo', type:'POST' })
.then(arr => console.log(arr[0].GetContextWebInformation.FormDigestValue) );

Get/Manipulate Items

Get Item

Syntax:
sprLib.list(listName|listGUID).items(options)

Returns:

  • Array of objects containing column name/value pairs

Notes:

  • Omitting the listCols option will result in all List columns being returned (mimics SharePoint default behavior)
  • Only the first 100 items are returned by default (mimics SharePoint default behavior) - use queryLimit to get more (5,000 is typically the max, use queryNext [paging] for unlimited items)

Options

OptionTypeDefaultDescriptionPossible Values / Returns
listColsarrayarray of column names (OData style)listCols: ['Name', 'Badge_x0020_Number']
listColsobjectobject with column propertieslistCols: { badge: { dataName:'Badge_x0020_Number' } }
cachebooleanfalsecache settingsEx:cache: true
queryFilterstringquery filterutilizes OData style Query Operators
queryLimitstringmax items to return1-N
queryNextobjectobject with Next/Skip options (paging)prevId (1-N), maxItems (1-5000) Ex:{ prevId:5000, maxItems:1000 }
queryOrderbystringcolumn(s) to order byEx:queryOrderby:Name
metadatabooleanfalsewhether to return __metadataThe __metadata property can be included in the results array (to enable further operations, use of Etag, etc.) by using metadata:true

listCols Object

OptionTypeDefaultDescriptionPossible Values / Return Values
dataNamestringthe column namethe fixed, back-end REST column name (use Get List Column Properties)
dispNamestringthe name to use when displaying results in table headers, etc.
dateFormatstringformat to use when returning/displaying dateINTL, INTLTIME, YYYYMMDD, ISO, US
dataFuncfunctionfunction to use for returning a resultuse a custom function to transform the query result (see below)
getVersionsbooleanfalsereturn append text versions in arraytrue or false

listCols dataFunc Option

There are many times where you'll need more than a simple column value. For example, providing a link to the InfoPath form so users can edit the item directly.

The dataFunc option allows you access to the entire result set, then return any type of value. See the sample below where an "editLink" is created.

Get Item Sample Code

// EX: Simple array of column names
sprLib.list('Employees').items( ['Id','Name','Badge_x0020_Number'] )
.then(arrData => console.table(arrData))
.catch(errMsg => console.error(errMsg));

// Result:
/*
.---------------------------------------.
| Id  |    Name    | Badge_x0020_Number |
|-----|------------|--------------------|
| 253 | Hal Jordan |              12345 |
'---------------------------------------'
*/
// EX: Using 'listCols' option with array of column names
sprLib.list('Employees').items({
    listCols: ['Name', 'Badge_x0020_Number', 'Hire_x0020_Date']
})
.then(arrData => console.table(arrData))
.catch(errMsg => console.error(errMsg));
// EX: Using 'listCols' option to name our columns
// EX: Using 'getVersions' to gather all "Append Text"/Versioned Text into an array
// EX: Using 'dataFunc' option to return a dynamic, generated value (an html link)
// EX: Using query options: filter, order, limit
sprLib.list('Employees').items({
    listCols: {
        empId:      { dataName:'ID' },
        badgeNum:   { dataName:'Badge_x0020_Number' },
        appendText: { dataName:'Versioned_x0020_Comments', getVersions:true },
        viewLink:   { dataFunc:function(objItem){ return '<a href="/sites/dev/Lists/Employees/DispForm.aspx?ID='+objItem.ID+'">View Emp</a>' } }
    },
    queryFilter:  'Salary gt 100000',
    queryOrderby: 'Hire_x0020_Date',
    queryLimit:   3
})
.then(function(arrData){ console.table(arrData) })
.catch(function(errMsg){ console.error(errMsg) });

// RESULT:
/*
.--------------------------------------------------------------------------------------------------------------------------------.
| empId | badgeNum |            appendText              |                                viewLink                                |
|-------|----------|------------------------------------|------------------------------------------------------------------------|
|   334 |  1497127 | ["20170624:Update","20170601:New"] | <a href="/sites/dev/Lists/Employees/DispForm.aspx?ID=334">View Emp</a> |
|   339 |  1497924 | ["Not here yet", "Emp created"]    | <a href="/sites/dev/Lists/Employees/DispForm.aspx?ID=339">View Emp</a> |
|   350 |  1497927 | ["Vice President promotion"]       | <a href="/sites/dev/Lists/Employees/DispForm.aspx?ID=350">View Emp</a> |
'--------------------------------------------------------------------------------------------------------------------------------'
*/
// EX: Using paging/next/skip

// Anytime there are more results than what was returned, an `__next` object will be included. Keep passing these in subsequent queries to get all results.
sprLib.list('Departments').items({ listCols:['Id','Created'], queryLimit:5 });
// RESULT:
/*
.-----------------------------------------------------------.
|            __next             | Id |       Created        |
|-------------------------------|----|----------------------|
| {"prevId":"5","maxItems":"5"} |  1 | 2016-12-04T21:58:47Z |
| {"prevId":"5","maxItems":"5"} |  2 | 2016-12-04T21:59:07Z |
| {"prevId":"5","maxItems":"5"} |  3 | 2016-12-04T21:59:20Z |
| {"prevId":"5","maxItems":"5"} |  4 | 2016-12-04T21:59:36Z |
| {"prevId":"5","maxItems":"5"} |  5 | 2016-12-04T21:59:49Z |
'-----------------------------------------------------------'
*/

sprLib.list('Departments').items({
    listCols:  ['Id','Created'],
    queryNext: {'prevId':5, 'maxItems':5}
});
// RESULT:
/*
.------------------------------------------------------------.
|             __next             | Id |       Created        |
|--------------------------------|----|----------------------|
| {"prevId":"10","maxItems":"5"} |  6 | 2017-06-01T03:19:01Z |
| {"prevId":"10","maxItems":"5"} |  7 | 2017-12-14T05:00:10Z |
| {"prevId":"10","maxItems":"5"} |  8 | 2017-12-14T05:00:34Z |
| {"prevId":"10","maxItems":"5"} |  9 | 2017-12-14T05:00:59Z |
| {"prevId":"10","maxItems":"5"} | 10 | 2017-12-14T05:01:15Z |
'------------------------------------------------------------'
*/

Create Item

Syntax: sprLib.list(listName|listGUID).create(itemObject)

Options: An object with internal name/value pairs to be inserted

Returns: Object with column name/value pairs

Create Item Sample Code

sprLib.list('Employees')
.create({
    Name: 'Marty McFly',
    Badge_x0020_Number: 12345,
    Hire_x0020_Date: new Date(),
    Active: true
})
.then(function(objItem){
    console.log('New Item:');
    console.table(objItem);
})
.catch(function(strErr){ console.error(strErr); });

Update Item

Syntax: sprLib.list(listName|listGUID).update(itemObject)

Options:

  • An object with internal name/value pairs to be inserted
  • if __metadata.etag is not provided, this is equivalent to force:true (etag:'"*"' is used)

Returns: The object provided

Update Item Sample Code

sprLib.list('Employees')
.update({
    ID: 99,
    Name: 'Marty McFly',
    Active: false
})
.then(function(objItem){
    console.log('Updated Item:');
    console.table(objItem);
})
.catch(function(strErr){ console.error(strErr); });

Delete Item

Syntax: sprLib.list(listName|listGUID).delete(itemObject)

Returns: ID of the item just deleted

Notes: Permanently deletes the item (bypasses Recycle Bin - not recoverable)

Delete Item Sample Code

sprLib.list('Employees').delete({ "ID":123 })
.then(function(intId){ console.log('Deleted Item #'+intId); })
.catch(function(strErr){ console.error(strErr); });

Recycle Item

Syntax: sprLib.list(listName|listGUID).recycle(itemObject)

Returns: ID of the item just recycled

Notes: Moves the item into the Site Recycle Bin

Recycle Item Sample Code

sprLib.list('Employees').recycle({ "ID":123 })
.then(function(intId){ console.log('Recycled Item #'+intId); })
.catch(function(strErr){ console.error(strErr); });

List Information

Column Types and Options

Syntax: sprLib.list(listName|listGUID).cols()

Returns: Array of columns with name value pairs of property values

Column Properties

PropertyTypeDescription
dispNamestringdisplay name
dataNamestringinternal name - used in REST queries and in listCols arguments
dataTypestringcolumn type (FieldTypeKind) value (ex: Boolean, DateTime, Text)
defaultValuebooleanthe default value (if any)
isAppendbooleanis this an append text column?
isNumPctbooleanis this a percentage number column?
isReadOnlybooleanis this an read only column?
isRequiredbooleanis a value required in this column?
isUniquebooleanare unique values enforced on this column?
maxLengthbooleanthe maximum length of the column value
choiceValuesarrayallowed choice values
allowFillInChoicesbooleanare fill-in (custom) choices allowed?

Sample Code

sprLib.list('Announcements').cols()
.then(function(arrayResults){ console.table(arrayResults) });

// Result:
/*
.---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.
|      dispName       |     dataName      |  dataType   | isAppend | isNumPct | isReadOnly | isRequired | isUnique | defaultValue | maxLength | choiceValues | allowFillInChoices |
|---------------------|-------------------|-------------|----------|----------|------------|------------|----------|--------------|-----------|--------------|--------------------|
| ID                  | ID                | Counter     | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Content Type        | ContentType       | Computed    | false    | false    | false      | false      | false    | null         | null      | null         | null               |
| Title               | Title             | Text        | false    | false    | false      | true       | false    | null         |       255 | null         | null               |
| Modified            | Modified          | DateTime    | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Created             | Created           | DateTime    | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Created By          | Author            | User        | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Modified By         | Editor            | User        | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Attachments         | Attachments       | Attachments | false    | false    | false      | false      | false    | null         | null      | null         | null               |
| Title               | LinkTitleNoMenu   | Computed    | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Title               | LinkTitle         | Computed    | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Item Child Count    | ItemChildCount    | Lookup      | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Folder Child Count  | FolderChildCount  | Lookup      | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| App Created By      | AppAuthor         | Lookup      | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| App Modified By     | AppEditor         | Lookup      | false    | false    | true       | false      | false    | null         | null      | null         | null               |
| Compliance Asset Id | ComplianceAssetId | Text        | false    | false    | true       | false      | false    | null         |       255 | null         | null               |
| Body                | Body              | Note        | false    | false    | false      | false      | false    | null         | null      | null         | null               |
| Expires             | Expires           | DateTime    | false    | false    | false      | false      | false    | null         | null      | null         | null               |
| Status              | Status            | Choice      | false    | false    | false      | false      | false    | null         | null      | [1, 2, 3, 4] | false              |
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
*/

List Properties

Syntax: sprLib.list(listName|listGUID).info()

Returns: Object containing list properties - e.g. Title, Item Count, Created Date, etc.

Properties

Property NameTypeDescription
AllowContentTypesbooleanWhether Allow management of content types? is enabled
BaseTemplateintegerSPListTemplateType SP Base Template ID number - ex: 100
BaseTypeintegerSP Base Type ID number - ex: 0
CreatedstringDate the List/Library was created (ISO format)
DescriptionstringList/Library Description
DraftVersionVisibilitynumberwhether draft versions can be seen
EnableAttachmentsbooleanwhether users can attach files to items in this list
EnableFolderCreationbooleanwhether users can create folders in this list/library
EnableVersioningbooleanwhether versioning is enabled for the items in this list
ForceCheckoutbooleanWhether Force checkout is enabled
HasUniqueRoleAssignmentsbooleanWhether list has unique (non-inherited) permissions
HiddenbooleanWhether List is hidden
IdGUIDThe SP GUID of the List
ItemCountnumberThe number of Items in the List
LastItemDeletedDatestringDate (ISO format) an item was last deleted
LastItemModifiedDatestringDate (ISO format) an item was last modified
LastItemUserModifiedDatestringDate (ISO format) an item was last modified by a User
ListItemEntityTypeFullNamestringSP.List.listItemEntityTypeFullName property
TitlestringThe Title of the List/Library

Sample Code

sprLib.list('Employees').info()
.then(function(object){ console.table([object]) });

// RESULT:
/*
.---------------------------------------------------------------------.
|         Prop Name          |               Prop Value               |
|----------------------------|----------------------------------------|
| AllowContentTypes          | true                                   |
| BaseTemplate               | 100                                    |
| BaseType                   | 0                                      |
| Created                    | "2017-08-21T20:48:43Z"                 |
| Description                | ""                                     |
| DraftVersionVisibility     | 0                                      |
| EnableAttachments          | true                                   |
| EnableFolderCreation       | false                                  |
| EnableVersioning           | true                                   |
| ForceCheckout              | false                                  |
| HasUniqueRoleAssignments   | false                                  |
| Hidden                     | false                                  |
| Id                         | "8fda2799-dbbc-a420-9869-df87b08b09c1" |
| ItemCount                  | 238                                    |
| LastItemDeletedDate        | "2017-10-27T04:42:39Z"                 |
| LastItemModifiedDate       | "2017-10-27T04:42:55Z"                 |
| LastItemUserModifiedDate   | "2017-10-27T04:42:55Z"                 |
| ListItemEntityTypeFullName | "SP.Data.EmployeesListItem"            |
| Title                      | "Employees"                            |
'---------------------------------------------------------------------'
*/

List Permissions

Syntax: sprLib.list(listName|listGUID).perms()

Returns: Array of list permissions

Permission Properties

Property NameTypeDescription
Memberobjectobject with Member properties (Title,PrincipalId,PrincipalType)
Rolesobjectarray of Role objects with properties: (Name,Hidden)

Sample Code

sprLib.list('Employees').perms()
.then(function(arrayResults){ console.table(arrayResults) });

/*
.-----------------------------------------------------------------------------------------------------------------------------------------------------------.
|                                        Member                                         |                               Roles                               |
|---------------------------------------------------------------------------------------|-------------------------------------------------------------------|
| {"Title":"Dev Site Members","PrincipalType":"SharePoint Group","PrincipalId":8}       | [{"Hidden":false,"Name":"Design"},{"Hidden":false,"Name":"Edit"}] |
| {"Title":"Dev Site Owners","PrincipalType":"SharePoint Group","PrincipalId":6}        | [{"Hidden":false,"Name":"Full Control"}]                          |
| {"Title":"Dev Site Visitors","PrincipalType":"SharePoint Group","PrincipalId":7}      | [{"Hidden":false,"Name":"Read"}]                                  |
| {"Title":"Excel Services Viewers","PrincipalType":"SharePoint Group","PrincipalId":5} | [{"Hidden":false,"Name":"View Only"}]                             |
'-----------------------------------------------------------------------------------------------------------------------------------------------------------'
*/

HowTo: List Permissions Report

Easily reproduce the "List Permissions" page ('/_layouts/15/user.aspx') with a few lines of code.

Sample Code

sprLib.list('Employees').perms()
.then(function(arrPerms){
    arrPerms.forEach(function(perm){
        var $row = $('<tr/>');
        $row.append('<td><a href="../_layouts/15/people.aspx?MembershipGroupId='+ perm.Member.PrincipalId +'">'+ perm.Member.Title +'</a></td>');
        $row.append('<td>'+ perm.Member.PrincipalType +'</td>');
        $row.append('<td/>');
        perm.Roles.forEach(function(role){ if ( !role.Hidden ) $row.find('td:last-child').append(role.Name); });
        $('#tabListPerms tbody').append( $row );
    });
});

/*
.----------------------------------------------------------.
|          Name          |      Type        |    Roles     |
|------------------------|------------------|--------------|
| Dev Site Members       | SharePoint Group | Design, Edit |
| Dev Site Owners        | SharePoint Group | Full Control |
| Dev Site Visitors      | SharePoint Group | Read         |
| Excel Services Viewers | SharePoint Group | View Only    |
'----------------------------------------------------------'
*/
← Utility MethodsFile Methods (SP.File) →
  • Syntax
  • Options
    • baseUrl
    • requestDigest
  • Get/Manipulate Items
    • Get Item
    • Create Item
    • Update Item
    • Delete Item
    • Recycle Item
  • List Information
    • Column Types and Options
    • List Properties
    • List Permissions
    • HowTo: List Permissions Report
SpRestLib
Docs
Getting Started with SpRestLibSharePoint API ReferenceSharePoint Development GuidesAbout JavaScript Promises
Community
FacebookTwitterPinterestYouTube Channel
More
GitHub IssuesGitHub ProjectSpRestLib on Stack OverflowSite Icons
Copyright © 2019 Brent Ely