@pnp/sp
Version:
pnp - provides a fluent api for working with SharePoint REST
283 lines • 10.3 kB
JavaScript
import { __decorate } from "tslib";
import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPQueryable, SPInstance, SPCollection, spPost, } from "../spqueryable.js";
import { hOP, objectDefinedNotNull } 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";
/**
* 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;
}
[Symbol.asyncIterator]() {
const nextInit = SPCollection(this).using(parseBinderWithErrorCheck(async (r) => {
const json = await r.json();
const nextLink = hOP(json, "d") && hOP(json.d, "__next") ? json.d.__next : json["odata.nextLink"];
return {
hasNext: typeof nextLink === "string" && nextLink.length > 0,
nextLink,
value: parseODataJSON(json),
};
}));
const queryParams = ["$top", "$select", "$expand", "$filter", "$orderby", "$skiptoken"];
for (let i = 0; i < queryParams.length; i++) {
const param = this.query.get(queryParams[i]);
if (objectDefinedNotNull(param)) {
nextInit.query.set(queryParams[i], param);
}
}
return {
_next: nextInit,
async next() {
if (this._next === null) {
return { done: true, value: undefined };
}
const result = await this._next();
if (result.hasNext) {
this._next = SPCollection([this._next, result.nextLink]);
return { done: false, value: result.value };
}
else {
this._next = null;
return { done: false, value: result.value };
}
},
};
}
/**
* 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));
}
};
_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",
}));
return spPost(Item(this).using(ItemUpdatedParser()), postBody);
}
/**
* 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);
function ItemUpdatedParser() {
return parseBinderWithErrorCheck(async (r) => ({
etag: r.headers.get("etag"),
}));
}
//# sourceMappingURL=types.js.map