@pnp/sp
Version:
pnp - provides a fluent api for working with SharePoint REST
354 lines • 15.3 kB
JavaScript
import { __decorate } from "tslib";
import { hOP, isArray, objectDefinedNotNull } from "@pnp/core";
import { body, headers, TextParse } from "@pnp/queryable";
import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPQueryable, SPCollection, } from "../spqueryable.js";
import { odataUrlFrom } from "../utils/odata-url-from.js";
import { defaultPath } from "../decorators.js";
import { spPost, spPostMerge } from "../operations.js";
import { toResourcePath } from "../utils/to-resource-path.js";
import { encodePath } from "../utils/encode-path-str.js";
let _Lists = class _Lists extends _SPCollection {
/**
* Gets a list from the collection by guid id
*
* @param id The Id of the list (GUID)
*/
getById(id) {
return List(this).concat(`('${id}')`);
}
/**
* Gets a list from the collection by title
*
* @param title The title of the list
*/
getByTitle(title) {
return List(this, `getByTitle('${encodePath(title)}')`);
}
/**
* Adds a new list to the collection
*
* @param title The new list's title
* @param description The new list's description
* @param template The list template value
* @param enableContentTypes If true content types will be allowed and enabled, otherwise they will be disallowed and not enabled
* @param additionalSettings Will be passed as part of the list creation body
*/
async add(title, desc = "", template = 100, enableContentTypes = false, additionalSettings = {}) {
const addSettings = {
"AllowContentTypes": enableContentTypes,
"BaseTemplate": template,
"ContentTypesEnabled": enableContentTypes,
"Description": desc,
"Title": title,
...additionalSettings,
};
const data = await spPost(this, body(addSettings));
return { data, list: this.getByTitle(addSettings.Title) };
}
/**
* Ensures that the specified list exists in the collection (note: this method not supported for batching)
*
* @param title The new list's title
* @param desc The new list's description
* @param template The list template value
* @param enableContentTypes If true content types will be allowed and enabled, otherwise they will be disallowed and not enabled
* @param additionalSettings Will be passed as part of the list creation body or used to update an existing list
*/
async ensure(title, desc = "", template = 100, enableContentTypes = false, additionalSettings = {}) {
const addOrUpdateSettings = { Title: title, Description: desc, ContentTypesEnabled: enableContentTypes, ...additionalSettings };
const list = this.getByTitle(addOrUpdateSettings.Title);
try {
await list.select("Title")();
const data = await list.update(addOrUpdateSettings).then(r => r.data);
return { created: false, data, list: this.getByTitle(addOrUpdateSettings.Title) };
}
catch (e) {
const data = await this.add(title, desc, template, enableContentTypes, addOrUpdateSettings).then(r => r.data);
return { created: true, data, list: this.getByTitle(addOrUpdateSettings.Title) };
}
}
/**
* Gets a list that is the default asset location for images or other files, which the users upload to their wiki pages.
*/
async ensureSiteAssetsLibrary() {
const json = await spPost(Lists(this, "ensuresiteassetslibrary"));
return List([this, odataUrlFrom(json)]);
}
/**
* Gets a list that is the default location for wiki pages.
*/
async ensureSitePagesLibrary() {
const json = await spPost(Lists(this, "ensuresitepageslibrary"));
return List([this, odataUrlFrom(json)]);
}
};
_Lists = __decorate([
defaultPath("lists")
], _Lists);
export { _Lists };
export const Lists = spInvokableFactory(_Lists);
export class _List extends _SPInstance {
constructor() {
super(...arguments);
this.delete = deleteableWithETag();
}
/**
* Gets the effective base permissions of this list
*
*/
get effectiveBasePermissions() {
return SPQueryable(this, "EffectiveBasePermissions");
}
/**
* Gets the event receivers attached to this list
*
*/
get eventReceivers() {
return SPCollection(this, "EventReceivers");
}
/**
* Gets the related fields of this list
*
*/
get relatedFields() {
return SPQueryable(this, "getRelatedFields");
}
/**
* Gets the IRM settings for this list
*
*/
get informationRightsManagementSettings() {
return SPQueryable(this, "InformationRightsManagementSettings");
}
/**
* Updates this list intance 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 data = await spPostMerge(this, body(properties, headers({ "IF-Match": eTag })));
const list = hOP(properties, "Title") ? this.getParent(List, `getByTitle('${properties.Title}')`) : List(this);
return {
data,
list,
};
}
/**
* Returns the collection of changes from the change log that have occurred within the list, based on the specified query.
* @param query A query that is performed against the change log.
*/
getChanges(query) {
return spPost(List(this, "getchanges"), body({ query }));
}
/**
* Returns the collection of items in the list based on the provided CamlQuery
* @param query A query that is performed against the list
* @param expands An expanded array of n items that contains fields to expand in the CamlQuery
*/
getItemsByCAMLQuery(query, ...expands) {
return spPost(List(this, "getitems").expand(...expands), body({ query }));
}
/**
* See: https://msdn.microsoft.com/en-us/library/office/dn292554.aspx
* @param query An object that defines the change log item query
*/
getListItemChangesSinceToken(query) {
return spPost(List(this, "getlistitemchangessincetoken").using(TextParse()), body({ query }));
}
/**
* Moves the list to the Recycle Bin and returns the identifier of the new Recycle Bin item.
*/
async recycle() {
return spPost(List(this, "recycle"));
}
/**
* Renders list data based on the view xml provided
* @param viewXml A string object representing a view xml
*/
async renderListData(viewXml) {
const q = List(this, "renderlistdata(@viewXml)");
q.query.set("@viewXml", `'${viewXml}'`);
const data = await spPost(q);
return JSON.parse(data);
}
/**
* Returns the data for the specified query view
*
* @param parameters The parameters to be used to render list data as JSON string.
* @param overrideParams The parameters that are used to override and extend the regular SPRenderListDataParameters.
* @param query Allows setting of query parameters
*/
// eslint-disable-next-line max-len
renderListDataAsStream(parameters, overrideParameters = null, query = new Map()) {
if (hOP(parameters, "RenderOptions") && isArray(parameters.RenderOptions)) {
parameters.RenderOptions = parameters.RenderOptions.reduce((v, c) => v + c);
}
const clone = List(this, "RenderListDataAsStream");
if (query && query.size > 0) {
query.forEach((v, k) => clone.query.set(k, v));
}
const params = objectDefinedNotNull(overrideParameters) ? { parameters, overrideParameters } : { parameters };
return spPost(clone, body(params));
}
/**
* Gets the field values and field schema attributes for a list item.
* @param itemId Item id of the item to render form data for
* @param formId The id of the form
* @param mode Enum representing the control mode of the form (Display, Edit, New)
*/
async renderListFormData(itemId, formId, mode) {
const data = await spPost(List(this, `renderlistformdata(itemid=${itemId}, formid='${formId}', mode='${mode}')`));
// data will be a string, so we parse it again
return JSON.parse(data);
}
/**
* Reserves a list item ID for idempotent list item creation.
*/
async reserveListItemId() {
return spPost(List(this, "reservelistitemid"));
}
/**
* Creates an item using path (in a folder), validates and sets its field values.
*
* @param formValues The fields to change and their new values.
* @param decodedUrl Path decoded url; folder's server relative path.
* @param bNewDocumentUpdate true if the list item is a document being updated after upload; otherwise false.
* @param checkInComment Optional check in comment.
* @param additionalProps Optional set of additional properties LeafName new document file name,
*/
async addValidateUpdateItemUsingPath(formValues, decodedUrl, bNewDocumentUpdate = false, checkInComment, additionalProps) {
const addProps = {
FolderPath: toResourcePath(decodedUrl),
};
if (objectDefinedNotNull(additionalProps)) {
if (additionalProps.leafName) {
addProps.LeafName = toResourcePath(additionalProps.leafName);
}
if (additionalProps.objectType) {
addProps.UnderlyingObjectType = additionalProps.objectType;
}
}
return spPost(List(this, "AddValidateUpdateItemUsingPath()"), body({
bNewDocumentUpdate,
checkInComment,
formValues,
listItemCreateInfo: addProps,
}));
}
/**
* Gets the parent information for this item's list and web
*/
async getParentInfos() {
const urlInfo = await this.select("Id", "RootFolder/UniqueId", "RootFolder/ServerRelativeUrl", "RootFolder/ServerRelativePath", "ParentWeb/Id", "ParentWeb/Url", "ParentWeb/ServerRelativeUrl", "ParentWeb/ServerRelativePath").expand("RootFolder", "ParentWeb")();
return {
List: {
Id: urlInfo.Id,
RootFolderServerRelativePath: urlInfo.RootFolder.ServerRelativePath,
RootFolderServerRelativeUrl: urlInfo.RootFolder.ServerRelativeUrl,
RootFolderUniqueId: urlInfo.RootFolder.UniqueId,
},
ParentWeb: {
Id: urlInfo.ParentWeb.Id,
ServerRelativePath: urlInfo.ParentWeb.ServerRelativePath,
ServerRelativeUrl: urlInfo.ParentWeb.ServerRelativeUrl,
Url: urlInfo.ParentWeb.Url,
},
};
}
}
export const List = spInvokableFactory(_List);
/**
* Enum representing the options of the RenderOptions property on IRenderListDataParameters interface
*/
export var RenderListDataOptions;
(function (RenderListDataOptions) {
RenderListDataOptions[RenderListDataOptions["None"] = 0] = "None";
RenderListDataOptions[RenderListDataOptions["ContextInfo"] = 1] = "ContextInfo";
RenderListDataOptions[RenderListDataOptions["ListData"] = 2] = "ListData";
RenderListDataOptions[RenderListDataOptions["ListSchema"] = 4] = "ListSchema";
RenderListDataOptions[RenderListDataOptions["MenuView"] = 8] = "MenuView";
RenderListDataOptions[RenderListDataOptions["ListContentType"] = 16] = "ListContentType";
/**
* The returned list will have a FileSystemItemId field on each item if possible.
*/
RenderListDataOptions[RenderListDataOptions["FileSystemItemId"] = 32] = "FileSystemItemId";
/**
* Returns the client form schema to add and edit items.
*/
RenderListDataOptions[RenderListDataOptions["ClientFormSchema"] = 64] = "ClientFormSchema";
/**
* Returns QuickLaunch navigation nodes.
*/
RenderListDataOptions[RenderListDataOptions["QuickLaunch"] = 128] = "QuickLaunch";
/**
* Returns Spotlight rendering information.
*/
RenderListDataOptions[RenderListDataOptions["Spotlight"] = 256] = "Spotlight";
/**
* Returns Visualization rendering information.
*/
RenderListDataOptions[RenderListDataOptions["Visualization"] = 512] = "Visualization";
/**
* Returns view XML and other information about the current view.
*/
RenderListDataOptions[RenderListDataOptions["ViewMetadata"] = 1024] = "ViewMetadata";
/**
* Prevents AutoHyperlink from being run on text fields in this query.
*/
RenderListDataOptions[RenderListDataOptions["DisableAutoHyperlink"] = 2048] = "DisableAutoHyperlink";
/**
* Enables urls pointing to Media TA service, such as .thumbnailUrl, .videoManifestUrl, .pdfConversionUrls.
*/
RenderListDataOptions[RenderListDataOptions["EnableMediaTAUrls"] = 4096] = "EnableMediaTAUrls";
/**
* Return Parant folder information.
*/
RenderListDataOptions[RenderListDataOptions["ParentInfo"] = 8192] = "ParentInfo";
/**
* Return Page context info for the current list being rendered.
*/
RenderListDataOptions[RenderListDataOptions["PageContextInfo"] = 16384] = "PageContextInfo";
/**
* Return client-side component manifest information associated with the list.
*/
RenderListDataOptions[RenderListDataOptions["ClientSideComponentManifest"] = 32768] = "ClientSideComponentManifest";
/**
* Return all content-types available on the list.
*/
RenderListDataOptions[RenderListDataOptions["ListAvailableContentTypes"] = 65536] = "ListAvailableContentTypes";
/**
* Return the order of items in the new-item menu.
*/
RenderListDataOptions[RenderListDataOptions["FolderContentTypeOrder"] = 131072] = "FolderContentTypeOrder";
/**
* Return information to initialize Grid for quick edit.
*/
RenderListDataOptions[RenderListDataOptions["GridInitInfo"] = 262144] = "GridInitInfo";
/**
* Indicator if the vroom API of the SPItemUrl returned in MediaTAUrlGenerator should use site url as host.
*/
RenderListDataOptions[RenderListDataOptions["SiteUrlAsMediaTASPItemHost"] = 524288] = "SiteUrlAsMediaTASPItemHost";
/**
* Return the files representing mount points in the list.
*/
RenderListDataOptions[RenderListDataOptions["AddToOneDrive"] = 1048576] = "AddToOneDrive";
/**
* Return SPFX CustomAction.
*/
RenderListDataOptions[RenderListDataOptions["SPFXCustomActions"] = 2097152] = "SPFXCustomActions";
/**
* Do not return non-SPFX CustomAction.
*/
RenderListDataOptions[RenderListDataOptions["CustomActions"] = 4194304] = "CustomActions";
})(RenderListDataOptions || (RenderListDataOptions = {}));
/**
* Determines the display mode of the given control or view
*/
export var ControlMode;
(function (ControlMode) {
ControlMode[ControlMode["Display"] = 1] = "Display";
ControlMode[ControlMode["Edit"] = 2] = "Edit";
ControlMode[ControlMode["New"] = 3] = "New";
})(ControlMode || (ControlMode = {}));
//# sourceMappingURL=types.js.map