UNPKG

@pnp/sp

Version:

pnp - provides a fluent api for working with SharePoint REST

124 lines 4.95 kB
import { __decorate } from "tslib"; import { _SPCollection, spInvokableFactory, _SPInstance, SPCollection, spPost, } from "../spqueryable.js"; import { extractWebUrl } from "../utils/extract-web-url.js"; import { combine } from "@pnp/core"; import { defaultPath } from "../decorators.js"; function getAppCatalogPath(base, path) { const paths = ["_api/web/tenantappcatalog/", "_api/web/sitecollectionappcatalog/"]; for (let i = 0; i < paths.length; i++) { const index = base.indexOf(paths[i]); if (index > -1) { return combine(base.substring(index, index + paths[i].length), path); } } return combine(base, path); } let _AppCatalog = class _AppCatalog extends _SPCollection { constructor(base, path) { super(base, null); this._url = combine(extractWebUrl(this._url), path); } /** * Get details of specific app from the app catalog * @param id - Specify the guid of the app */ getAppById(id) { return App(this, `getById('${id}')`); } /** * Synchronize a solution to the Microsoft Teams App Catalog * @param id - Specify the guid of the app * @param useSharePointItemId (optional) - By default this REST call requires the SP Item id of the app, not the app id. * PnPjs will try to fetch the item id, you can still use this parameter to pass your own item id in the first parameter */ async syncSolutionToTeams(id, useSharePointItemId = false) { // This REST call requires that you refer the list item id of the solution in the app catalog site. let appId = null; const webUrl = combine(extractWebUrl(this.toUrl()), "_api/web"); if (useSharePointItemId) { appId = id; } else { const listId = (await SPCollection([this, webUrl], "lists").select("Id").filter("EntityTypeName eq 'AppCatalog'")())[0].Id; const listItems = await SPCollection([this, webUrl], `lists/getById('${listId}')/items`).select("Id").filter(`AppProductID eq '${id}'`).top(1)(); if (listItems && listItems.length > 0) { appId = listItems[0].Id; } else { throw Error(`Did not find the app with id ${id} in the appcatalog.`); } } return spPost(AppCatalog(this, getAppCatalogPath(this.toUrl(), `SyncSolutionToTeams(id=${appId})`))); } /** * Uploads an app package. Not supported for batching * * @param filename Filename to create. * @param content app package data (eg: the .app or .sppkg file). * @param shouldOverWrite Should an app with the same name in the same location be overwritten? (default: true) * @returns Promise<IAppAddResult> */ async add(filename, content, shouldOverWrite = true) { // you don't add to the availableapps collection const adder = AppCatalog(this, getAppCatalogPath(this.toUrl(), `add(overwrite=${shouldOverWrite},url='${filename}')`)); return spPost(adder, { body: content, headers: { "binaryStringRequestBody": "true", }, }); } }; _AppCatalog = __decorate([ defaultPath("_api/web/tenantappcatalog/AvailableApps") ], _AppCatalog); export { _AppCatalog }; export const AppCatalog = spInvokableFactory(_AppCatalog); export class _App extends _SPInstance { /** * This method deploys an app on the app catalog. It must be called in the context * of the tenant app catalog web or it will fail. * * @param skipFeatureDeployment Deploy the app to the entire tenant */ deploy(skipFeatureDeployment = false) { return this.do(`Deploy(${skipFeatureDeployment})`); } /** * This method retracts a deployed app on the app catalog. It must be called in the context * of the tenant app catalog web or it will fail. */ retract() { return this.do("Retract"); } /** * This method allows an app which is already deployed to be installed on a web */ install() { return this.do("Install"); } /** * This method allows an app which is already installed to be uninstalled on a web * Note: when you use the REST API to uninstall a solution package from the site, it is not relocated to the recycle bin */ uninstall() { return this.do("Uninstall"); } /** * This method allows an app which is already installed to be upgraded on a web */ upgrade() { return this.do("Upgrade"); } /** * This method removes an app from the app catalog. It must be called in the context * of the tenant app catalog web or it will fail. */ remove() { return this.do("Remove"); } do(path) { return spPost(App(this, path)); } } export const App = spInvokableFactory(_App); //# sourceMappingURL=types.js.map