Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Added new 'guid' option for list() (Issue #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
GitBrent authored and GitBrent committed Jan 6, 2018
1 parent cc53878 commit a6f87da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,18 @@ Lists can be accessed by either their name or their GUID:

Syntax: `sprLib.list(listName)`
Syntax: `sprLib.list(listGUID)`
Syntax: `sprLib.list({ name:name })`
Syntax: `sprLib.list({ guid:GUID })`
Syntax: `sprLib.list({ name:name, baseUrl:path })`
Syntax: `sprLib.list({ name:name, baseUrl:path, requestDigest:formDigestValue })`

### Options
| Prop | Type | Required? | Description | Possible Values |
| :-------------- | :----- | :-------- | :--------------------------------------- | :----------------------------- |
| `name` | string | Y | list name or GUID | Ex:`{'name': 'Employees'}` |
| `baseUrl` | string | | data to be sent | Ex:`{'baseUrl': '/sites/dev'}` |
| `requestDigest` | string | | the request form digest security token | Ex:`'requestDigest':'ABC123'` |
| Prop | Type | Required? | Description | Possible Values |
| :-------------- | :----- | :-------- | :--------------------------------------- | :------------------------------- |
| `name` | string | Y | list name or list GUID | Ex:`{'name': 'Employees'}` |
| `guid` | string | | list GUID (convenience alias for name) | Ex:`{'guid': '8675309-ab3d-ef87b08e09e1'}` |
| `baseUrl` | string | | data to be sent | Ex:`{'baseUrl': '/sites/dev'}` |
| `requestDigest` | string | | the request form digest security token | Ex:`{'requestDigest': 'ABC123'}` |

#### Options: baseUrl
By default, the base URL is set to where the host webpart is located (`_spPageContextInfo.webServerRelativeUrl`).
Expand Down
34 changes: 19 additions & 15 deletions dist/sprestlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var NODEJS = ( typeof module !== 'undefined' && module.exports );
(function(){
// APP VERSION/BUILD
var APP_VER = "1.4.0-beta";
var APP_BLD = "20180104";
var APP_BLD = "20180105";
var DEBUG = false; // (verbose mode/lots of logging)
// ENUMERATIONS
var ENUM_PRINCIPALTYPES = {
Expand Down Expand Up @@ -610,34 +610,38 @@ var NODEJS = ( typeof module !== 'undefined' && module.exports );

// API: LIST (CRUD + getItems)
/**
* @param `inOpts` (string) - required - List Name or List GUID
* @param `inOpt` (string) - required - ListName or ListGUID
* @example - string - sprLib.list('Documents');
*
* @param `inOpts` (object) - required - { `name`, [`baseUrl`] }
* @param `inOpt` (object) - required - { `name`, [`baseUrl`] }
* @example - string - sprLib.list({ name:'23846527-228a-41a2-b5c1-7b55b6fea1a3' });
* @example - string - sprLib.list({ guid:'23846527-228a-41a2-b5c1-7b55b6fea1a3' });
* @example - string - sprLib.list({ name:'Documents' });
* @example - string - sprLib.list({ name:'Documents', baseUrl:'/sites/dev/sandbox' });
* @example - string - sprLib.list({ name:'Documents', baseUrl:'/sites/dev/sandbox', requestDigest:'8675309,05 Dec 2017 01:23:45 -0000' });
*/
sprLib.list = function list(inOpts) {
sprLib.list = function list(inOpt) {
var _newList = {};
var _urlBase = "_api/lists";
var _requestDigest = $("#__REQUESTDIGEST").val();
inOpt = inOpt || {};
// Allow `guid` as a synonym for `name` per user request
if ( inOpt.guid ) inOpt.name = inOpt.guid;

// A: Param check
if ( inOpts && typeof inOpts === 'string' ) {
if ( inOpt && typeof inOpt === 'string' ) {
// DESIGN: Accept either [ListName] or [ListGUID]
_urlBase += ( gRegexGUID.test(inOpts) ? "(guid'"+ inOpts +"')" : "/getbytitle('"+ inOpts.replace(/\s/gi,'%20') +"')" );
_urlBase += ( gRegexGUID.test(inOpt) ? "(guid'"+ inOpt +"')" : "/getbytitle('"+ inOpt.replace(/\s/gi,'%20') +"')" );
}
else if ( inOpts && typeof inOpts === 'object' && Object.keys(inOpts).length > 0 && inOpts.name ) {
_urlBase = (inOpts.baseUrl ? inOpts.baseUrl.replace(/\/+$/,'')+'/_api/lists' : _urlBase);
_urlBase += ( gRegexGUID.test(inOpts.name) ? "(guid'"+ inOpts.name +"')" : "/getbytitle('"+ inOpts.name.replace(/\s/gi,'%20') +"')" );
if ( inOpts.requestDigest ) _requestDigest = inOpts.requestDigest;
else if ( inOpt && typeof inOpt === 'object' && Object.keys(inOpt).length > 0 && inOpt.name ) {
_urlBase = (inOpt.baseUrl ? inOpt.baseUrl.replace(/\/+$/,'')+'/_api/lists' : _urlBase);
_urlBase += ( gRegexGUID.test(inOpt.name) ? "(guid'"+ inOpt.name +"')" : "/getbytitle('"+ inOpt.name.replace(/\s/gi,'%20') +"')" );
if ( inOpt.requestDigest ) _requestDigest = inOpt.requestDigest;
}
else {
console.error("ERROR: A 'listName' or 'listGUID' is required! EX: `sprLib.list('Employees')` or `sprLib.list({ name:'Employees' })`");
console.error('ARGS:');
console.error(inOpts);
console.error(inOpt);
return null;
}

Expand Down Expand Up @@ -2327,10 +2331,10 @@ var NODEJS = ( typeof module !== 'undefined' && module.exports );
}

// API: NODEJS: Setup
sprLib.nodeConfig = function nodeConfig(inOpts){
inOpts = (inOpts && typeof inOpts === 'object' ? inOpts : {});
APP_OPTS.nodeCookie = inOpts.cookie || '';
APP_OPTS.nodeServer = inOpts.server || '';
sprLib.nodeConfig = function nodeConfig(inOpt){
inOpt = (inOpt && typeof inOpt === 'object' ? inOpt : {});
APP_OPTS.nodeCookie = inOpt.cookie || '';
APP_OPTS.nodeServer = inOpt.server || '';
}

/* ===============================================================================================
Expand Down

0 comments on commit a6f87da

Please sign in to comment.