UNPKG

@pnp/sp

Version:

pnp - provides a fluent api for working with SharePoint REST

48 lines 2.7 kB
import { odataUrlFrom } from "../utils/odata-url-from.js"; import { extractWebUrl } from "../utils/extract-web-url.js"; import { Web } from "../webs/types.js"; import "../lists/web.js"; import { _Folder, Folder } from "../folders/types.js"; _Folder.prototype.getDefaultColumnValues = async function () { const folderProps = await Folder(this, "Properties").select("vti_x005f_listname")(); const { ServerRelativePath: serRelPath } = await this.select("ServerRelativePath")(); const web = Web([this, extractWebUrl(odataUrlFrom(folderProps))]); const docLib = web.lists.getById(folderProps.vti_x005f_listname); // and we return the defaults associated with this folder's server relative path only // if you want all the defaults use list.getDefaultColumnValues() return (await docLib.getDefaultColumnValues()).filter(v => v.path.toLowerCase() === serRelPath.DecodedUrl.toLowerCase()); }; _Folder.prototype.setDefaultColumnValues = async function (fieldDefaults, merge = true) { // we start by figuring out where we are const folderProps = await Folder(this, "Properties").select("vti_x005f_listname")(); // now we create a web, list and batch to get some info we need const web = Web([this, extractWebUrl(odataUrlFrom(folderProps))]); const docLib = web.lists.getById(folderProps.vti_x005f_listname); // we need the proper folder path const folderPath = (await this.select("ServerRelativePath")()).ServerRelativePath.DecodedUrl; // at this point we should have all the defaults to update // and we need to get all the defaults to update the entire doc const existingDefaults = await docLib.getDefaultColumnValues(); // we filter all defaults to remove any associated with this folder if merge is false const filteredExistingDefaults = merge ? existingDefaults : existingDefaults.filter(f => f.path !== folderPath); // we update / add any new defaults from those passed to this method fieldDefaults.forEach(d => { const existing = filteredExistingDefaults.find(ed => ed.name === d.name && ed.path === folderPath); if (existing) { existing.value = d.value; } else { filteredExistingDefaults.push({ name: d.name, path: folderPath, value: d.value, }); } }); // after this operation filteredExistingDefaults should contain all the value we want to write to the file await docLib.setDefaultColumnValues(filteredExistingDefaults); }; _Folder.prototype.clearDefaultColumnValues = async function () { await this.setDefaultColumnValues([], false); }; //# sourceMappingURL=folder.js.map