JavaScript SharePoint REST Web Services Library
SpRestLib is a lightweight wrapper around the SharePoint REST API that can be used in client browsers or server-side.
This library is for developers who build web parts embedded into Content Editor/Script Editor, SPFx web parts, Angular/React apps, Node.js/npm-based solutions, etc. Using SpRestLib greatly simplifies SharePoint integration by reducing common operations to concise Promise-based methods.
sprLib.rest(options)
- Returns the results of a given REST call to any SharePoint REST API
sprLib.list(listName).items()
- Returns an array of SP.ListItem
objects using a variety of query optionssprLib.list(listName).create()
- Create a new list item using JSON datasprLib.list(listName).update()
- Update an existing item using JSON datasprLib.list(listName).delete()
- Delete an existing item using JSON data (permanently delete)sprLib.list(listName).recycle()
- Recycle an existing item using JSON data (move to Recycle Bin)sprLib.list(listName).cols()
- Returns an array of column properties (datatype, default values, etc.)sprLib.list(listName).info()
- Returns SP.List
properties (last modified, number of items, etc.)sprLib.list(listName).perms()
- Returns an array of the list's Member Role assignmentssprLib.file(fileName).get()
- Returns a file (binary/text) as a blob which can be savedsprLib.file(fileName).info()
- Returns SP.File
properties (Created, GUID, HasUniquePerms, etc.)sprLib.file(fileName).perms()
- Returns an array of the file's Member Role assignmentssprLib.file(fileName).checkin()
- Check in a file (supports optional comments/checkin types)sprLib.file(fileName).checkout()
- Check out a filesprLib.file(fileName).delete()
- Permanently deletes a file (bypasses recycle bin)sprLib.file(fileName).recycle()
- Moves file to the site Recycle BinsprLib.folder(folderName).files()
- Returns an array of file objects contained in the foldersprLib.folder(folderName).folders()
- Returns an array of folder objects contained in the foldersprLib.folder(folderName).info()
- Returns SP.Folder
properties (Created, GUID, HasUniquePerms, etc.)sprLib.folder(folderName).perms()
- Returns an array of the folder's Member Role assignmentssprLib.folder(folderName).add()
- Creates a new folder under the parent foldersprLib.folder(folderName).delete()
- Permanently deletes a folder (bypasses recycle bin)sprLib.folder(folderName).recycle()
- Moves folder to the site Recycle BinsprLib.site(siteUrl).groups()
- Returns an array of the site's Groups and MemberssprLib.site(siteUrl).info()
- Returns SP.Web
site properties (ID, Owner, Language, Logo, etc.)sprLib.site(siteUrl).lists()
- Returns an array of the site's Lists/LibrariessprLib.site(siteUrl).perms()
- Returns an array of the site's Member/Roles objectssprLib.site(siteUrl).roles()
- Returns an array of the site's RolessprLib.site(siteUrl).subsites()
- Returns an array of the site's SubsitessprLib.site(siteUrl).users()
- Returns an array of the site's Users and their base permissionssprLib.user(options).groups()
- Returns SP.Group
group properties (Id, Owner, Title, etc.)sprLib.user(options).info()
- Returns SP.User
user properties (Id, Email, Login, Title, etc.)sprLib.user(options).profile()
- Returns SP.UserProfile.PersonProperties
(DirectReports, PictureUrl, etc.)sprLib.renewSecurityToken()
- Refreshes the SharePoint page security digest token (__REQUESTDIGEST
)data-sprlib{options}
- Populates the parent tag using the options providedIt's really easy to test drive SpRestLib!
Just open your browser's Developer Tools window anywhere on your SharePoint site, then run the following code snippet which will load the SpRestLib bundle script dynamically:
// Load/Demo SpRestLib via CDN
var script = document.createElement('script');
script.src = "https://cdn.jsdelivr.net/gh/gitbrent/[email protected]/dist/sprestlib.bundle.js";
script.onload = function(){
// Demo library method - show current user info
console.log('Current SharePoint User: ');
sprLib.user().info().then( objUser => console.log(objUser) );
}
document.getElementsByTagName('head')[0].appendChild(script);
Upload the example/sprestlib-demo.html
file to SiteAssets on your SharePoint site and add it into a web part for a live
demo of all available methods.
<script src="https://cdn.jsdelivr.net/gh/gitbrent/[email protected]/dist/sprestlib.min.js"></script>
// Use bundle for IE11 support
<script src="https://cdn.jsdelivr.net/gh/gitbrent/[email protected]/dist/sprestlib.bundle.js"></script>
<script src="/subsite/SiteAssets/js/sprestlib.min.js"></script>
// Use bundle for IE11 support
<script src="/subsite/SiteAssets/js/sprestlib.bundle.js"></script>
npm install sprestlib
var sprLib = require("sprestlib");
yarn install sprestlib
SP.List
)SP.File
)SP.Folder
)SP.Web
)SP.User
)SharePoint Authentication Notes
Using SpRestLib with React, Angular, SharePoint Electron, etc. Integration with Other Libraries
example
directory for a complete, working demo of connecting to SharePoint Online.See SharePoint Authentication Notes for issues with authentication.
Please file issues or suggestions on the issues page on GitHub, or even better, submit a pull request. Feedback is always welcome!
When reporting issues, please include a code snippet or a link demonstrating the problem.
Does SpRestLib support SharePoint 2010 or 2007?
Unfortunately, older versions cannot be supported. The SharePoint 2007/2010 API utilized SOAP web services with XML (_vti_bin/lists.asmx
endpoints) while the current API uses a completely new (_api/web/lists()
endpoint) backed by REST services.
SPServices
libraryCopyright © 2016-2019 Brent Ely