gd-sprest
Advanced tools
Comparing version 0.9.2 to 0.9.3
{ | ||
"name": "gd-sprest", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"description": "An easy way to develop against the SharePoint REST API.", | ||
@@ -5,0 +5,0 @@ "author": "Gunjan Datta <me@dattabase.com> (https://github.com/gunjandatta/sprest)", |
# SharePoint Online/On-Premise REST Framework | ||
An easy way to develop against the SharePoint REST api. | ||
An easy way to create, read, update and delete data using the SharePoint 2013/Online REST API. | ||
@@ -7,7 +7,7 @@ *Please report issues. I am constantly updating/fixing/testing to make this library better.* | ||
## Benefits: | ||
* Generates the REST api url and formats it for app webs automatically. | ||
* Chain property and methods for fewer requests to the server. | ||
* Global flag for defaulting the execution against the host web, for easier development in an app web. | ||
* PowerShell-Like experience in the browser console. (Synchronous Requests) | ||
* Written in TypeScript with definition file for intellisense. | ||
* Easily create, read, update and delete SharePoint data | ||
* Easier to execute requests against the host web from a SharePoint Hosted App | ||
* Ability to control the order of the requests executed against the server | ||
* PowerShell-Like experience in the browser console | ||
* TypeScript definition for intellisense | ||
@@ -17,8 +17,2 @@ ## Demo: | ||
## Get Started: | ||
### Node Package Manager (NPM) | ||
``` | ||
npm install gd-sprest --save-dev | ||
``` | ||
## Documentation: | ||
@@ -31,1 +25,7 @@ [Click here](https://github.com/gunjandatta/sprest/wiki) to view the documentation for the framework. | ||
[Office Fabric UI Dashboard](https://github.com/gunjandatta/sprest-list) | ||
## Get Started: | ||
### Node Package Manager (NPM) | ||
``` | ||
npm install gd-sprest --save-dev | ||
``` |
@@ -133,7 +133,14 @@ module $REST { | ||
this.executeRequest(true, () => { | ||
// Set the wait flag | ||
this.base.waitFlags[this.responseIndex] = true; | ||
// Execute the callback | ||
callback ? callback(this) : null; | ||
// Execute the callback and see if it returns a promise | ||
let returnVal = callback ? callback(this) : null; | ||
if(returnVal && typeof(returnVal.done) === "function") { | ||
// Wait for the promise to complete | ||
returnVal.done(() => { | ||
// Set the wait flag | ||
this.base.waitFlags[this.responseIndex] = true; | ||
}); | ||
} else { | ||
// Set the wait flag | ||
this.base.waitFlags[this.responseIndex] = true; | ||
} | ||
}); | ||
@@ -590,13 +597,12 @@ } | ||
if(response.request == null || !response.request.completedFl) { return; } | ||
// Ensure the wait flag is set for the previous request | ||
if(counter > 0 && this.base.waitFlags[counter - 1] != true) { return; } | ||
} | ||
// Ensure this isn't the first request | ||
// If so, then ensure the previous request has completed | ||
if(this.responseIndex == 0 || this.base.waitFlags[this.responseIndex - 1]) { | ||
// Clear the interval | ||
window.clearInterval(intervalId); | ||
// Clear the interval | ||
window.clearInterval(intervalId); | ||
// Execute the callback | ||
callback(); | ||
} | ||
// Execute the callback | ||
callback(); | ||
}, 10); | ||
@@ -603,0 +609,0 @@ } |
@@ -13,2 +13,5 @@ declare module $REST.Types { | ||
/** Gets a value that specifies whether the file exists. */ | ||
Exists: string; | ||
/** | ||
@@ -37,4 +40,7 @@ * Gets the files contained in the folder. | ||
/** Gets a value that specifies the count of items in the list folder. */ | ||
ItemCount: string; | ||
ItemCount: number; | ||
/** Indicates whether the folder is enabled for WOPI default action. */ | ||
IsWOPIEnabled: boolean; | ||
/** | ||
@@ -41,0 +47,0 @@ * Specifies the list item field (2) values for the list item corresponding to the file. |
@@ -43,3 +43,24 @@ declare module $REST.Types { | ||
getFolder(web:$REST.Types.IWeb, folderUrl:string, createFl?:boolean): Types.IPromise; | ||
/** | ||
* Method to remove empty folders | ||
* @param web - The web containing the files. | ||
* @param folderUrls - An array of folder urls, relative to the web. | ||
*/ | ||
removeEmptyFolders(web:$REST.Types.IWeb, folderUrls:Array<string>): Types.IPromise; | ||
/** | ||
* Method to remove files from a web. | ||
* @param web - The web containing the files. | ||
* @param fileUrl - The file url, relative to the web. | ||
*/ | ||
removeFile(web:$REST.Types.IWeb, fileUrl:string): Types.IPromise; | ||
/** | ||
* Method to remove files from a web. | ||
* @param web - The web containing the files. | ||
* @param fileUrls - An array of file urls, relative to the web. | ||
*/ | ||
removeFiles(web:$REST.Types.IWeb, fileUrls:Array<string>): Types.IPromise; | ||
} | ||
} |
@@ -275,3 +275,3 @@ declare module $REST.Settings { | ||
/**A Boolean value that specifies whether to return best bet results for the query. */ | ||
/** A Boolean value that specifies whether to return best bet results for the query. */ | ||
ProcessBestBets?: boolean; | ||
@@ -278,0 +278,0 @@ |
@@ -83,4 +83,2 @@ module $REST { | ||
.execute((file:$REST.Types.IFile) => { | ||
let promise = new $REST.Utils.Promise(); | ||
// Save a reference to this file | ||
@@ -100,5 +98,2 @@ srcFile = file; | ||
}); | ||
// Return the promise | ||
return promise; | ||
}); | ||
@@ -119,16 +114,17 @@ | ||
// Method to copy a file in this app web to the host web | ||
static copyFilesToHostWeb(fileUrls:Array<string>, folderUrls:Array<string>, overwriteFl?:boolean, promise?:Utils.Promise, files?:Array<$REST.Types.IFile>, folders?:Array<$REST.Types.IFolder>) { | ||
// Ensure the files, folders and promise exist | ||
static copyFilesToHostWeb(fileUrls:Array<string>, folderUrls:Array<string>, overwriteFl?:boolean, idx?:number, promise?:Utils.Promise, files?:Array<$REST.Types.IFile>, folders?:Array<$REST.Types.IFolder>) { | ||
files = files ? files : []; | ||
folders = folders ? folders : []; | ||
idx = idx ? idx : 0; | ||
promise = promise ? promise : new Utils.Promise(); | ||
// Ensure the array is not empty | ||
if(fileUrls.length == 0 || folderUrls.length == 0) { | ||
// Resolve the promise | ||
if(fileUrls.length == idx || folderUrls.length == idx) { | ||
// Resolve the promise and return it | ||
promise.resolve(files, folders); | ||
return promise; | ||
} | ||
// Copy the file | ||
this.copyFileToHostWeb(fileUrls.pop(), folderUrls.pop(), overwriteFl) | ||
this.copyFileToHostWeb(fileUrls[idx], folderUrls[idx], overwriteFl) | ||
// Wait for it to complete | ||
@@ -141,3 +137,3 @@ .done((file, folder) => { | ||
// Copy the files | ||
this.copyFilesToHostWeb(fileUrls, folderUrls, overwriteFl, promise, files, folders); | ||
this.copyFilesToHostWeb(fileUrls, folderUrls, overwriteFl, ++idx, promise, files, folders); | ||
}) | ||
@@ -160,33 +156,24 @@ | ||
// Ensure the folder exists | ||
if(!folder.existsFl) { | ||
// Get the folder | ||
folder.execute(); | ||
} | ||
// Wait for the request to complete | ||
folder.done(() => { | ||
// Get the sub-folder | ||
let subFolder = folder.Folders(subFolderName).execute((subFolder) => { | ||
// Method to add additional sub folders | ||
let addSubFolders = (subFolder) => { | ||
// See if we are done | ||
if(subFolderUrl.length == 0) { | ||
// Resolve the promise | ||
promise.resolve(subFolder); | ||
} else { | ||
// Create the sub folder | ||
this.createSubFolders(subFolder, subFolderUrl, promise); | ||
} | ||
}; | ||
// Ensure the sub-folder exists | ||
if(subFolder.existsFl) { | ||
// Add the rest of the sub folders | ||
addSubFolders(subFolder); | ||
// Get the sub-folder | ||
let subFolder = folder.Folders(subFolderName).execute((subFolder) => { | ||
// Method to add additional sub folders | ||
let addSubFolders = (subFolder) => { | ||
// See if we are done | ||
if(subFolderUrl.length == 0) { | ||
// Resolve the promise | ||
promise.resolve(subFolder); | ||
} else { | ||
// Create the sub folder | ||
folder.Folders().add(subFolderName).execute(addSubFolders); | ||
this.createSubFolders(subFolder, subFolderUrl, promise); | ||
} | ||
}); | ||
}; | ||
// Ensure the sub-folder exists | ||
if(subFolder.Exists) { | ||
// Add the rest of the sub folders | ||
addSubFolders(subFolder); | ||
} else { | ||
// Create the sub folder | ||
folder.Folders().add(subFolderName).execute(addSubFolders); | ||
} | ||
}); | ||
@@ -221,3 +208,3 @@ | ||
// Ensure the folder exists | ||
if(folder.existsFl) { | ||
if(folder.Exists) { | ||
// Save a reference to the folder | ||
@@ -253,3 +240,107 @@ dstFolder = folder; | ||
} | ||
// Method to remove empty folders | ||
static removeEmptyFolders(web:$REST.Types.IWeb, folderUrls:Array<string>) { | ||
let promise = new Utils.Promise(); | ||
// Ensure folder urls exist | ||
if(folderUrls.length == 0) { | ||
// Resolve the promise and return it | ||
promise.resolve(); | ||
} else { | ||
let prevFolderUrl = null; | ||
// Sort the urls alphabetically, then from longest to shortest | ||
folderUrls.sort().sort(function(a, b) { return a.length > b.length ? -1 : 1; }); | ||
// Parse the folders | ||
for(let folderUrl of folderUrls) { | ||
let folder = null; | ||
// See if we already removed this folder | ||
if(folderUrl == prevFolderUrl) { continue; } | ||
else { prevFolderUrl = folderUrl; } | ||
// Parse the folder names | ||
let folderNames = folderUrl.split('/'); | ||
for(let folderName of folderNames) { | ||
// Get the sub-folder | ||
folder = folder ? folder.Folders(folderName) : web.Folders(folderName); | ||
} | ||
// Execute the request | ||
folder.execute((folder:$REST.Types.IFolder) => { | ||
let promise = new Utils.Promise(); | ||
// See if the folder is empty | ||
if(folder.ItemCount == 0) { | ||
// Delete the folder, and resolve the promise | ||
folder.delete().execute(() => { promise.resolve(); }); | ||
} else { | ||
// Resolve the proise | ||
promise.resolve(); | ||
} | ||
// Return the promise | ||
return promise; | ||
}, true); | ||
} | ||
// Wait for the requests to complete, and resolve the promise | ||
web.done(() => { promise.resolve(); }); | ||
} | ||
// Return the promise | ||
return promise; | ||
} | ||
// Method to remove a file | ||
static removeFile(web:$REST.Types.IWeb, fileUrl:string) { | ||
let promise = new Utils.Promise(); | ||
let folder = null; | ||
let folders = fileUrl.split('/'); | ||
// Parse the folders | ||
for(let i=0; i<folders.length-1; i++) { | ||
// Get the folder | ||
folder = folder ? folder.Folders(folders[i]) : web.Folders(folders[i]); | ||
} | ||
// Get the file | ||
folder.Files(folders[folders.length-1]).execute((file:$REST.Types.IFile) => { | ||
// See if it exists | ||
if(file.Exists) { | ||
// Delete it and resolve the promise | ||
file.delete().execute(() => { promise.resolve(); }); | ||
} else { | ||
// Resolve the promises | ||
promise.resolve(); | ||
} | ||
}, true); | ||
// Return the promise | ||
return promise; | ||
} | ||
// Method to remove files | ||
static removeFiles(web:$REST.Types.IWeb, fileUrls:Array<string>, idx?:number, promise?:Utils.Promise) { | ||
idx = idx ? idx : 0; | ||
promise = promise ? promise : new Utils.Promise(); | ||
// See if we have removed all files | ||
if(fileUrls.length == idx) { | ||
// Resolve the promise and return it | ||
promise.resolve(); | ||
} else { | ||
// Remove the file | ||
this.removeFile(web, fileUrls[idx]).done(() => { | ||
// Remove the files | ||
this.removeFiles(web, fileUrls, ++idx, promise); | ||
}) | ||
} | ||
// Return the promise | ||
return promise; | ||
} | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
4601788
21845