UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

89 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetNavigationLinks = GetNavigationLinks; exports.AddNavigationLink = AddNavigationLink; exports.DeleteNavigationLinks = DeleteNavigationLinks; const common_logger_1 = require("../../common-logger"); const typecheckers_1 = require("../../helpers/typecheckers"); const rest_1 = require("../rest"); const common_1 = require("./common"); const logger = new common_logger_1.CommonLogger("utils/sharepoint.rest/navigation-links"); /** * Get all navigation links in the top and side navigation of a SharePoint site * @param siteUrl The URL of the SharePoint site * @returns An array containing all navigation links */ async function GetNavigationLinks(siteUrl) { siteUrl = (0, common_1.GetSiteUrl)(siteUrl); const topNavUrl = `${(0, common_1.GetRestBaseUrl)(siteUrl)}/web/navigation/topnavigationbar`; const sideNavUrl = `${(0, common_1.GetRestBaseUrl)(siteUrl)}/web/navigation/quicklaunch`; try { const topNavResponse = await (0, rest_1.GetJson)(topNavUrl, null, { spWebUrl: siteUrl //allow getDigest to work when not in SharePoint }); const sideNavResponse = await (0, rest_1.GetJson)(sideNavUrl, null, { spWebUrl: siteUrl //allow getDigest to work when not in SharePoint }); const topNavLinks = topNavResponse.d.results.map((link) => ({ ...link, Location: "topnavigationbar" })); const sideNavLinks = sideNavResponse.d.results.map((link) => ({ ...link, Location: "quicklaunch" })); return [...topNavLinks, ...sideNavLinks]; } catch (error) { logger.error(`Error fetching navigation links: ${error.message}`); } return []; } /** * Add a navigation link to the specified location (top navigation or side navigation) * @param title The title of the navigation link * @param url The url of the navigation link * @param location The location where the link will be added ('topnavigationbar' or 'quicklaunch'). Default is 'quicklaunch'. * @Logs If the location is invalid or if adding the link fails */ async function AddNavigationLink(title, url, location = 'quicklaunch') { try { let siteUrl = (0, common_1.GetSiteUrl)(); let navigationUrl = ""; navigationUrl = `${(0, common_1.GetRestBaseUrl)(siteUrl)}/web/navigation/${location}`; const response = await (0, rest_1.GetJson)(navigationUrl, JSON.stringify({ '__metadata': { 'type': 'SP.NavigationNode' }, 'Title': title, 'Url': url }), { spWebUrl: siteUrl, }); if (!(0, typecheckers_1.isNullOrUndefined)(response) && !(0, typecheckers_1.isNullOrUndefined)(response.d)) { return response.d; } } catch (error) { logger.error('Error adding link'); } } /** * Delete navigation links by title and URL * @param navLinks An array of navigation links to be deleted * @Logs If the location is invalid or if deleting the links fails */ async function DeleteNavigationLinks(navLinks) { try { const siteUrl = (0, common_1.GetSiteUrl)(); for (const navLink of navLinks) { const navigationUrl = `${(0, common_1.GetRestBaseUrl)(siteUrl)}/web/Navigation/GetNodeById(${navLink.Id})`; // Use the same convention to make the DELETE request const response = await (0, rest_1.GetJson)(navigationUrl, null, { method: 'POST', spWebUrl: siteUrl, xHttpMethod: 'DELETE' }); if (!(0, typecheckers_1.isNullOrEmptyString)(response) && !response.ok) { logger.error('Failed to delete link'); } } logger.info('Navigation links deleted successfully'); } catch (error) { logger.error('Error deleting links'); } } //# sourceMappingURL=navigation-links.js.map