@pnp/sp
Version:
pnp - provides a fluent api for working with SharePoint REST
247 lines • 10.3 kB
JavaScript
import { __decorate } from "tslib";
import { isUrlAbsolute, combine, noInherit } from "@pnp/core";
import { body, cancelableScope } from "@pnp/queryable";
import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPInstance, } from "../spqueryable.js";
import { odataUrlFrom } from "../utils/odata-url-from.js";
import { Item } from "../items/types.js";
import { defaultPath } from "../decorators.js";
import { spPost, spPostMerge } from "../operations.js";
import { extractWebUrl } from "../utils/extract-web-url.js";
import { toResourcePath } from "../utils/to-resource-path.js";
import { encodePath } from "../utils/encode-path-str.js";
import "../context-info/index.js";
import { BatchNever } from "../batching.js";
let _Folders = class _Folders extends _SPCollection {
/**
* Gets a folder by it's name
*
* @param name Folder's name
*/
getByUrl(name) {
return Folder(this).concat(`('${encodePath(name)}')`);
}
/**
* Adds a new folder by path and should be prefered over add
*
* @param serverRelativeUrl The server relative url of the new folder to create
* @param overwrite True to overwrite an existing folder, default false
*/
async addUsingPath(serverRelativeUrl, overwrite = false) {
const data = await spPost(Folders(this, `addUsingPath(DecodedUrl='${encodePath(serverRelativeUrl)}',overwrite=${overwrite})`));
return {
data,
folder: folderFromServerRelativePath(this, data.ServerRelativeUrl),
};
}
};
_Folders = __decorate([
defaultPath("folders")
], _Folders);
export { _Folders };
export const Folders = spInvokableFactory(_Folders);
export class _Folder extends _SPInstance {
constructor() {
super(...arguments);
this.delete = deleteableWithETag();
}
/**
* Gets this folder's sub folders
*
*/
get folders() {
return Folders(this);
}
/**
* Gets this folder's list item field values
*
*/
get listItemAllFields() {
return SPInstance(this, "listItemAllFields");
}
/**
* Gets the parent folder, if available
*
*/
get parentFolder() {
return Folder(this, "parentFolder");
}
/**
* Gets this folder's properties
*
*/
get properties() {
return SPInstance(this, "properties");
}
/**
* Gets this folder's storage metrics information
*
*/
get storageMetrics() {
return SPInstance(this, "storagemetrics");
}
/**
* Updates folder's properties
* @param props Folder's properties to update
*/
async update(props) {
const data = await spPostMerge(this, body(props));
return {
data,
folder: this,
};
}
/**
* Moves the folder to the Recycle Bin and returns the identifier of the new Recycle Bin item.
*/
recycle() {
return spPost(Folder(this, "recycle"));
}
/**
* Gets the associated list item for this folder, loading the default properties
*/
async getItem(...selects) {
const q = this.listItemAllFields;
const d = await q.select(...selects)();
if (d["odata.null"]) {
throw Error("No associated item was found for this folder. It may be the root folder, which does not have an item.");
}
return Object.assign(Item([this, odataUrlFrom(d)]), d);
}
async moveByPath(destUrl, ...rest) {
let options = {
KeepBoth: false,
ShouldBypassSharedLocks: true,
RetainEditorAndModifiedOnMove: false,
};
if (rest.length === 1) {
if (typeof rest[0] === "boolean") {
options.KeepBoth = rest[0];
}
else if (typeof rest[0] === "object") {
options = { ...options, ...rest[0] };
}
}
return this.moveCopyImpl(destUrl, options, "MoveFolderByPath");
}
async copyByPath(destUrl, ...rest) {
let options = {
ShouldBypassSharedLocks: true,
ResetAuthorAndCreatedOnCopy: true,
KeepBoth: false,
};
if (rest.length === 1) {
if (typeof rest[0] === "boolean") {
options.KeepBoth = rest[0];
}
else if (typeof rest[0] === "object") {
options = { ...options, ...rest[0] };
}
}
return this.moveCopyImpl(destUrl, options, "CopyFolderByPath");
}
/**
* Deletes the folder object with options.
*
* @param parameters Specifies the options to use when deleting a folder.
*/
async deleteWithParams(parameters) {
return spPost(Folder(this, "DeleteWithParameters"), body({ parameters }));
}
/**
* Create the subfolder inside the current folder, as specified by the leafPath
*
* @param leafPath leafName of the new folder
*/
async addSubFolderUsingPath(leafPath) {
await spPost(Folder(this, "AddSubFolderUsingPath"), body({ leafPath: toResourcePath(leafPath) }));
return this.folders.getByUrl(leafPath);
}
/**
* Gets the parent information for this folder's list and web
*/
async getParentInfos() {
const urlInfo = await this.select("ServerRelativeUrl", "ListItemAllFields/ParentList/Id", "ListItemAllFields/ParentList/RootFolder/UniqueId", "ListItemAllFields/ParentList/RootFolder/ServerRelativeUrl", "ListItemAllFields/ParentList/RootFolder/ServerRelativePath", "ListItemAllFields/ParentList/ParentWeb/Id", "ListItemAllFields/ParentList/ParentWeb/Url", "ListItemAllFields/ParentList/ParentWeb/ServerRelativeUrl", "ListItemAllFields/ParentList/ParentWeb/ServerRelativePath").expand("ListItemAllFields/ParentList", "ListItemAllFields/ParentList/RootFolder", "ListItemAllFields/ParentList/ParentWeb")();
return {
Folder: {
ServerRelativeUrl: urlInfo.ServerRelativeUrl,
},
ParentList: {
Id: urlInfo.ListItemAllFields.ParentList.Id,
RootFolderServerRelativePath: urlInfo.ListItemAllFields.ParentList.RootFolder.ServerRelativePath,
RootFolderServerRelativeUrl: urlInfo.ListItemAllFields.ParentList.RootFolder.ServerRelativeUrl,
RootFolderUniqueId: urlInfo.ListItemAllFields.ParentList.RootFolder.UniqueId,
},
ParentWeb: {
Id: urlInfo.ListItemAllFields.ParentList.ParentWeb.Id,
ServerRelativePath: urlInfo.ListItemAllFields.ParentList.ParentWeb.ServerRelativePath,
ServerRelativeUrl: urlInfo.ListItemAllFields.ParentList.ParentWeb.ServerRelativeUrl,
Url: urlInfo.ListItemAllFields.ParentList.ParentWeb.Url,
},
};
}
/**
* Implementation of folder move/copy
*
* @param destUrl The server relative path to which the folder will be copied/moved
* @param options Any options
* @param methodName The method to call
* @returns An IFolder representing the moved or copied folder
*/
moveCopyImpl(destUrl, options, methodName) {
// create a timeline we will manipulate for this request
const poster = Folder(this);
// add our pre-request actions, this fixes issues with batching hanging #2668
poster.on.pre(noInherit(async (url, init, result) => {
const urlInfo = await Folder(this).using(BatchNever()).getParentInfos();
const uri = new URL(urlInfo.ParentWeb.Url);
url = combine(urlInfo.ParentWeb.Url, `/_api/SP.MoveCopyUtil.${methodName}()`);
init = body({
destPath: toResourcePath(isUrlAbsolute(destUrl) ? destUrl : combine(uri.origin, destUrl)),
options,
srcPath: toResourcePath(combine(uri.origin, urlInfo.Folder.ServerRelativeUrl)),
}, init);
return [url, init, result];
}));
return spPost(poster).then(() => folderFromPath(this, destUrl));
}
}
__decorate([
cancelableScope
], _Folder.prototype, "moveByPath", null);
__decorate([
cancelableScope
], _Folder.prototype, "copyByPath", null);
export const Folder = spInvokableFactory(_Folder);
/**
* Creates an IFolder instance given a base object and a server relative path
*
* @param base Valid SPQueryable from which the observers will be used and the web url extracted
* @param serverRelativePath The server relative url to the folder (ex: '/sites/dev/documents/folder3')
* @returns IFolder instance referencing the folder described by the supplied parameters
*/
export function folderFromServerRelativePath(base, serverRelativePath) {
return Folder([base, extractWebUrl(base.toUrl())], `_api/web/getFolderByServerRelativePath(decodedUrl='${encodePath(serverRelativePath)}')`);
}
/**
* Creates an IFolder instance given a base object and an absolute path
*
* @param base Valid SPQueryable from which the observers will be used
* @param serverRelativePath The absolute url to the folder (ex: 'https://tenant.sharepoint.com/sites/dev/documents/folder/')
* @returns IFolder instance referencing the folder described by the supplied parameters
*/
export async function folderFromAbsolutePath(base, absoluteFolderPath) {
const { WebFullUrl } = await Folder(this).using(BatchNever()).getContextInfo(absoluteFolderPath);
const { pathname } = new URL(absoluteFolderPath);
return folderFromServerRelativePath(Folder([base, combine(WebFullUrl, "_api/web")]), decodeURIComponent(pathname));
}
/**
* Creates an IFolder intance given a base object and either an absolute or server relative path to a folder
*
* @param base Valid SPQueryable from which the observers will be used
* @param serverRelativePath server relative or absolute url to the file (ex: 'https://tenant.sharepoint.com/sites/dev/documents/folder' or '/sites/dev/documents/folder')
* @returns IFile instance referencing the file described by the supplied parameters
*/
export async function folderFromPath(base, path) {
return (isUrlAbsolute(path) ? folderFromAbsolutePath : folderFromServerRelativePath)(base, path);
}
//# sourceMappingURL=types.js.map