@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
86 lines • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeleteNavigationLinks = exports.AddNavigationLink = exports.GetNavigationLinks = void 0;
const typecheckers_1 = require("../../helpers/typecheckers");
const consolelogger_1 = require("../consolelogger");
const rest_1 = require("../rest");
const common_1 = require("./common");
const logger = consolelogger_1.ConsoleLogger.get("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);
const sideNavResponse = await (0, rest_1.GetJson)(sideNavUrl);
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 [];
}
exports.GetNavigationLinks = GetNavigationLinks;
/**
* 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');
}
}
exports.AddNavigationLink = AddNavigationLink;
/**
* 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');
}
}
exports.DeleteNavigationLinks = DeleteNavigationLinks;
//# sourceMappingURL=navigation-links.js.map