UNPKG

@pnp/sp

Version:

pnp - provides a fluent api for working with SharePoint REST

296 lines • 10.3 kB
import { __decorate } from "tslib"; import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPQueryable, SPInstance, } from "../spqueryable.js"; import { hOP } from "@pnp/core"; import { extractWebUrl } from "@pnp/sp"; import { List } from "../lists/types.js"; import { body, headers, parseBinderWithErrorCheck, parseODataJSON } from "@pnp/queryable"; import { defaultPath } from "../decorators.js"; import { spPost } from "../operations.js"; /** * Describes a collection of Item objects * */ let _Items = class _Items extends _SPCollection { /** * Gets an Item by id * * @param id The integer id of the item to retrieve */ getById(id) { return Item(this).concat(`(${id})`); } /** * Gets BCS Item by string id * * @param stringId The string id of the BCS item to retrieve */ getItemByStringId(stringId) { // creates an item with the parent list path and append out method call return Item([this, this.parentUrl], `getItemByStringId('${stringId}')`); } /** * Skips the specified number of items (https://msdn.microsoft.com/en-us/library/office/fp142385.aspx#sectionSection6) * * @param skip The starting id where the page should start, use with top to specify pages * @param reverse It true the PagedPrev=true parameter is added allowing backwards navigation in the collection */ skip(skip, reverse = false) { if (reverse) { this.query.set("$skiptoken", `Paged=TRUE&PagedPrev=TRUE&p_ID=${skip}`); } else { this.query.set("$skiptoken", `Paged=TRUE&p_ID=${skip}`); } return this; } /** * Gets a collection designed to aid in paging through data * */ getPaged() { return this.using(PagedItemParser(this))(); } /** * Adds a new item to the collection * * @param properties The new items's properties * @param listItemEntityTypeFullName The type name of the list's entities */ async add(properties = {}) { return spPost(this, body(properties)).then((data) => ({ data: data, item: this.getById(data.Id), })); } }; _Items = __decorate([ defaultPath("items") ], _Items); export { _Items }; export const Items = spInvokableFactory(_Items); /** * Descrines a single Item instance * */ export class _Item extends _SPInstance { constructor() { super(...arguments); this.delete = deleteableWithETag(); } /** * Gets the effective base permissions for the item * */ get effectiveBasePermissions() { return SPQueryable(this, "EffectiveBasePermissions"); } /** * Gets the effective base permissions for the item in a UI context * */ get effectiveBasePermissionsForUI() { return SPQueryable(this, "EffectiveBasePermissionsForUI"); } /** * Gets the field values for this list item in their HTML representation * */ get fieldValuesAsHTML() { return SPInstance(this, "FieldValuesAsHTML"); } /** * Gets the field values for this list item in their text representation * */ get fieldValuesAsText() { return SPInstance(this, "FieldValuesAsText"); } /** * Gets the field values for this list item for use in editing controls * */ get fieldValuesForEdit() { return SPInstance(this, "FieldValuesForEdit"); } /** * Gets the collection of versions associated with this item */ get versions() { return ItemVersions(this); } /** * this item's list */ get list() { return this.getParent(List, "", this.parentUrl.substring(0, this.parentUrl.lastIndexOf("/"))); } /** * Updates this list instance with the supplied properties * * @param properties A plain object hash of values to update for the list * @param eTag Value used in the IF-Match header, by default "*" */ async update(properties, eTag = "*") { const postBody = body(properties, headers({ "IF-Match": eTag, "X-HTTP-Method": "MERGE", })); const data = await spPost(Item(this).using(ItemUpdatedParser()), postBody); return { data, item: this, }; } /** * Moves the list item to the Recycle Bin and returns the identifier of the new Recycle Bin item. */ recycle() { return spPost(Item(this, "recycle")); } /** * Deletes the item object with options. * * @param parameters Specifies the options to use when deleting a item. */ async deleteWithParams(parameters) { return spPost(Item(this, "DeleteWithParameters"), body({ parameters })); } /** * Gets a string representation of the full URL to the WOPI frame. * If there is no associated WOPI application, or no associated action, an empty string is returned. * * @param action Display mode: 0: view, 1: edit, 2: mobileView, 3: interactivePreview */ async getWopiFrameUrl(action = 0) { const i = Item(this, "getWOPIFrameUrl(@action)"); i.query.set("@action", action); return spPost(i); } /** * Validates and sets the values of the specified collection of fields for the list item. * * @param formValues The fields to change and their new values. * @param bNewDocumentUpdate true if the list item is a document being updated after upload; otherwise false. */ validateUpdateListItem(formValues, bNewDocumentUpdate = false) { return spPost(Item(this, "validateupdatelistitem"), body({ formValues, bNewDocumentUpdate })); } /** * Gets the parent information for this item's list and web */ async getParentInfos() { const urlInfo = await this.select("Id", "ParentList/Id", "ParentList/Title", "ParentList/RootFolder/UniqueId", "ParentList/RootFolder/ServerRelativeUrl", "ParentList/RootFolder/ServerRelativePath", "ParentList/ParentWeb/Id", "ParentList/ParentWeb/Url", "ParentList/ParentWeb/ServerRelativeUrl", "ParentList/ParentWeb/ServerRelativePath").expand("ParentList", "ParentList/RootFolder", "ParentList/ParentWeb")(); return { Item: { Id: urlInfo.Id, }, ParentList: { Id: urlInfo.ParentList.Id, Title: urlInfo.ParentList.Title, RootFolderServerRelativePath: urlInfo.ParentList.RootFolder.ServerRelativePath, RootFolderServerRelativeUrl: urlInfo.ParentList.RootFolder.ServerRelativeUrl, RootFolderUniqueId: urlInfo.ParentList.RootFolder.UniqueId, }, ParentWeb: { Id: urlInfo.ParentList.ParentWeb.Id, ServerRelativePath: urlInfo.ParentList.ParentWeb.ServerRelativePath, ServerRelativeUrl: urlInfo.ParentList.ParentWeb.ServerRelativeUrl, Url: urlInfo.ParentList.ParentWeb.Url, }, }; } async setImageField(fieldName, imageName, imageContent) { const contextInfo = await this.getParentInfos(); const webUrl = extractWebUrl(this.toUrl()); const q = SPQueryable([this, webUrl], "/_api/web/UploadImage"); q.concat("(listTitle=@a1,imageName=@a2,listId=@a3,itemId=@a4)"); q.query.set("@a1", `'${contextInfo.ParentList.Title}'`); q.query.set("@a2", `'${imageName}'`); q.query.set("@a3", `'${contextInfo.ParentList.Id}'`); q.query.set("@a4", contextInfo.Item.Id); const result = await spPost(q, { body: imageContent }); const itemInfo = { "type": "thumbnail", "fileName": result.Name, "nativeFile": {}, "fieldName": fieldName, "serverUrl": contextInfo.ParentWeb.Url.replace(contextInfo.ParentWeb.ServerRelativeUrl, ""), "serverRelativeUrl": result.ServerRelativeUrl, "id": result.UniqueId, }; return this.validateUpdateListItem([{ FieldName: fieldName, FieldValue: JSON.stringify(itemInfo), }]); } } export const Item = spInvokableFactory(_Item); /** * Describes a collection of Version objects * */ let _ItemVersions = class _ItemVersions extends _SPCollection { /** * Gets a version by id * * @param versionId The id of the version to retrieve */ getById(versionId) { return ItemVersion(this).concat(`(${versionId})`); } }; _ItemVersions = __decorate([ defaultPath("versions") ], _ItemVersions); export { _ItemVersions }; export const ItemVersions = spInvokableFactory(_ItemVersions); /** * Describes a single Version instance * */ export class _ItemVersion extends _SPInstance { constructor() { super(...arguments); this.delete = deleteableWithETag(); } } export const ItemVersion = spInvokableFactory(_ItemVersion); /** * Provides paging functionality for list items */ export class PagedItemCollection { constructor(parent, nextUrl, results) { this.parent = parent; this.nextUrl = nextUrl; this.results = results; } /** * If true there are more results available in the set, otherwise there are not */ get hasNext() { return typeof this.nextUrl === "string" && this.nextUrl.length > 0; } /** * Gets the next set of results, or resolves to null if no results are available */ async getNext() { if (this.hasNext) { const items = Items([this.parent, this.nextUrl], ""); return items.getPaged(); } return null; } } function PagedItemParser(parent) { return parseBinderWithErrorCheck(async (r) => { const json = await r.json(); const nextUrl = hOP(json, "d") && hOP(json.d, "__next") ? json.d.__next : json["odata.nextLink"]; return new PagedItemCollection(parent, nextUrl, parseODataJSON(json)); }); } function ItemUpdatedParser() { return parseBinderWithErrorCheck(async (r) => ({ etag: r.headers.get("etag"), })); } //# sourceMappingURL=types.js.map