@pnp/sp
Version:
pnp - provides a fluent api for working with SharePoint REST
201 lines • 8.4 kB
JavaScript
import { __decorate } from "tslib";
import { isUrlAbsolute, combine } from "@pnp/core";
import { body } 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 { escapeQueryStrValue } from "../utils/escape-query-str.js";
import { extractWebUrl } from "../utils/extract-web-url.js";
import { toResourcePath } from "../utils/to-resource-path.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(`('${escapeQueryStrValue(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='${escapeQueryStrValue(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");
}
/**
* 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);
}
/**
* Moves a folder by path to destination path
* Also works with different site collections.
*
* @param destUrl Absolute or relative URL of the destination path
* @param keepBoth Keep both if folder with the same name in the same location already exists?
*/
async moveByPath(destUrl, KeepBoth = false) {
const urlInfo = await this.getParentInfos();
const uri = new URL(urlInfo.ParentWeb.Url);
await spPost(Folder([this, uri.origin], "/_api/SP.MoveCopyUtil.MoveFolderByPath()"), body({
destPath: toResourcePath(isUrlAbsolute(destUrl) ? destUrl : combine(uri.origin, destUrl)),
options: {
KeepBoth,
ResetAuthorAndCreatedOnCopy: true,
ShouldBypassSharedLocks: true,
},
srcPath: toResourcePath(combine(uri.origin, urlInfo.Folder.ServerRelativeUrl)),
}));
}
/**
* Copies a folder by path to destination path
* Also works with different site collections.
*
* @param destUrl Absolute or relative URL of the destination path
* @param keepBoth Keep both if folder with the same name in the same location already exists?
*/
async copyByPath(destUrl, KeepBoth = false) {
const urlInfo = await this.getParentInfos();
const uri = new URL(urlInfo.ParentWeb.Url);
await spPost(Folder([this, uri.origin], "/_api/SP.MoveCopyUtil.CopyFolderByPath()"), body({
destPath: toResourcePath(isUrlAbsolute(destUrl) ? destUrl : combine(uri.origin, destUrl)),
options: {
KeepBoth: KeepBoth,
ResetAuthorAndCreatedOnCopy: true,
ShouldBypassSharedLocks: true,
},
srcPath: toResourcePath(combine(uri.origin, urlInfo.Folder.ServerRelativeUrl)),
}));
}
/**
* 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,
},
};
}
/**
* Gets the shareable item associated with this folder
*/
async getShareable() {
// sharing only works on the item end point, not the file one
const d = await SPInstance(this, "listItemAllFields").select("odata.id")();
return Item([this, odataUrlFrom(d)]);
}
}
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='${escapeQueryStrValue(serverRelativePath)}')`);
}
//# sourceMappingURL=types.js.map