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

File Methods (SP.File)

Syntax

Files can be accessed by either their full or relative path:

sprLib.file('sample.pptx')
sprLib.file('/sites/dev/Shared Documents/sample.pptx')

File CheckIn/Checkout

File CheckIn

sprLib.file("filename").checkin()
sprLib.file("filename").checkin({ comment:"All done!" })
sprLib.file("filename").checkin({ comment:"All done!", type:"Major" })

Returns: True on success

File CheckIn Options

OptionTypeDefaultDescription
commentstringCheckIn comment. Ex: { 'comment':"updated due date" }
typestringMajorCheckIn type. Possible values: Major, Minor, Overwrite

File CheckOut

sprLib.file("filename").checkout()

Returns: True on success

File Manipulation

Download File

sprLib.file("filename").get()

Returns: Blob containing the file (either text or binary).

Download File Sample Code

// Example: Client-browser code to download file from SharePoint using JavaScript and REST
sprLib.file('SiteAssets/img/sprestlib.png').get()
.then(function(blob){
    var url = (window.URL || window.webkitURL).createObjectURL(blob);
    var link = document.createElement("a");
    link.setAttribute("href", url);
    link.setAttribute("download", _fileName);
    link.style = "visibility:hidden";
    document.body.appendChild(link);
    link.click();
    setTimeout(function(){ document.body.removeChild(link); }, 500);
});

Example

Delete File

sprLib.file("filename").delete()

Bypasses Recycle Bin (permanently deletes the file)

Returns: True on success

Delete File Sample Code

sprLib.file('SiteAssets/img/junk.png').delete()
.then(boolResult => console.log('file deleted!') );

Recycle File

sprLib.file("filename").recycle()

Sends file to the site Recycle Bin (recoverable)

Returns: True on success

Recycle File Sample Code

sprLib.file('SiteAssets/img/junk.png').recycle()
.then(boolResult => console.log('file recycled!') );

File Information

File Properties

sprLib.file("filename").info()

Returns: Object containing file properties

File Properties Enumeration

Property NameTypeDescription
Authorobjectobject containing the id of the author - ex: {"Id":9}
CheckInCommentstringthe latest CheckIn comment
CheckOutTypenumberthe item's check-out type (2=none, 1=offline, 0=online)
CheckedOutByUserobjectobject containing the id of the user who checked out the file (empty object if file is not checked out)
Createdstringthe Date (ISO format) a file was created
ETagstringthe ETag value - ex: "{8921212D-7E83-5AF1-CB51-5431BAD43233},12"
Existsbooleanwhether the file exists
Lengthintegerthe size of the file in bytes - ex: 55100
LockedByUserobjectobject containing the id of the locking user - ex: {"Id":9}
MajorVersionintegermajor version number - ex: 30
MinorVersionintegerminor version number - ex: 1
Modifiedstringthe Date (ISO format) an item was last modified
ModifiedByobjectobject containing the id of the author - ex: {"Id":9}
Namestringthe name of the file - ex: "Sample.pptx"
ServerRelativeUrlstringthe server relative URL - ex: "/sites/dev/Documents/Demo.xlsx"
UIVersionLabelstringmajor.minor version - ex: "30.0"
UniqueIdGUIDGUID for the file - ex: 8921212D-7E83-5AF1-CB51-5431BAD43233

File Properties Sample Code

sprLib.file('/sites/dev/Shared Documents/sample.xlsx').info()
.then(function(objInfo){ console.table([objInfo]) });

// Result:
/*
.----------------------------------------------------------------.
|     Prop Name     |                 Prop Value                 |
|-------------------|--------------------------------------------|
| Author            | {"Id":9}                                   |
| CheckedOutByUser  | {}                                         |
| LockedByUser      | {}                                         |
| ModifiedBy        | {"Id":9}                                   |
| CheckInComment    |                                            |
| CheckOutType      | 2                                          |
| ETag              | "{C8093E01-1044-51E2-9DC7-8524012E9111},1" |
| Exists            | true                                       |
| Length            | 14234                                      |
| MajorVersion      | 1                                          |
| MinorVersion      | 0                                          |
| Name              | "sample.xlsx"                              |
| ServerRelativeUrl | "/sites/dev/Shared Documents/sample.xlsx"  |
| TimeCreated       | "2017-08-06T01:33:37Z"                     |
| TimeLastModified  | "2017-08-06T01:33:37Z"                     |
| UniqueId          | "c8093f01-1044-51e2-9dc7-8524012e9111"     |
| UIVersionLabel    | "1.0"                                      |
'----------------------------------------------------------------'
*/

File Permissions

sprLib.file("filename").perms()

Returns: Array of file permissions

Permissions Properties

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

File Permissions Sample Code

sprLib.file('/sites/dev/Shared Documents/sample.xlsx').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"}]                             |
'-----------------------------------------------------------------------------------------------------------------------------------------------------------'
*/
← List/Library Methods (SP.List)Folder Methods (SP.Folder) →
  • Syntax
  • File CheckIn/Checkout
    • File CheckIn
    • File CheckOut
  • File Manipulation
    • Download File
    • Delete File
    • Recycle File
  • File Information
    • File Properties
    • File Permissions
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