fix error in site retrieval that was caused by a wrong algo to retrieve page canvas context

This commit is contained in:
Tobias Maestrini 2024-10-06 02:08:37 +02:00
parent 2e7c220dee
commit f24c7d7e16
1 changed files with 20 additions and 13 deletions

View File

@ -25,7 +25,7 @@ export class SPService {
let counter = 1;
this.allUrls.forEach(url => {
if (url === anchorUrl) {
if (counter != 1) {
if (counter !== 1) {
anchorUrl = anchorUrl.slice(0, -((counter - 1).toString().length + 1)) + '-' + counter;
} else {
@ -44,18 +44,25 @@ export class SPService {
* @param context Web part context
* @returns anchorLinks
*/
public static async GetAnchorLinks(context: WebPartContext) {
let anchorLinks: INavLink[] = [];
public static async GetAnchorLinks(context: WebPartContext): Promise<INavLink[]> {
const anchorLinks: INavLink[] = [];
try {
/* Page ID on which the web part is added */
const pageId = context.pageContext.listItem.id;
const currentPageRelativeUrl = context.pageContext.site.serverRequestPath;
const currentPageSiteRelativeURl = context.pageContext.site.serverRelativeUrl;
const currentPageUrl = currentPageRelativeUrl.replace(`${currentPageSiteRelativeURl}/`, '')
/* Get the canvasContent1 data for the page which consists of all the HTML */
const data = await context.spHttpClient.get(`${context.pageContext.web.absoluteUrl}/_api/sitepages/pages(${pageId})`, SPHttpClient.configurations.v1);
const data = await context.spHttpClient.get(`${context.pageContext.web.absoluteUrl}/_api/sitepages/pages?$select=CanvasContent1&$filter=Url eq '${currentPageUrl}'`, SPHttpClient.configurations.v1);
const jsonData = await data.json();
const canvasContent1 = jsonData.CanvasContent1;
const canvasContent1JSON: any[] = JSON.parse(canvasContent1);
let canvasContent1JSON: any[];
try {
const canvasContent1 = jsonData.value?.[0].CanvasContent1;
canvasContent1JSON = JSON.parse(canvasContent1);
} catch (err) {
throw Error(`Could not retrieve content: ${err.message}`);
}
this.allUrls = [];
@ -63,7 +70,7 @@ export class SPService {
canvasContent1JSON.map((webPart) => {
if (webPart.zoneGroupMetadata && webPart.zoneGroupMetadata.type === 1) {
const headingIsEmpty: boolean = !webPart.zoneGroupMetadata.displayName;
const headingValue: string = headingIsEmpty ? 'Empty Heading' : webPart.zoneGroupMetadata.displayName ;
const headingValue: string = headingIsEmpty ? 'Empty Heading' : webPart.zoneGroupMetadata.displayName;
const anchorUrl: string = this.GetAnchorUrl(headingValue);
this.allUrls.push(anchorUrl);
@ -76,10 +83,10 @@ export class SPService {
if (webPart.innerHTML) {
const HTMLString: string = webPart.innerHTML;
const hasCollapsableHeader: boolean = webPart.zoneGroupMetadata &&
webPart.zoneGroupMetadata.type === 1 &&
( anchorLinks.filter(x => x.name === webPart.zoneGroupMetadata.displayName).length === 1 ||
!webPart.zoneGroupMetadata.displayName );
const hasCollapsableHeader: boolean = webPart.zoneGroupMetadata &&
webPart.zoneGroupMetadata.type === 1 &&
(anchorLinks.filter(x => x.name === webPart.zoneGroupMetadata.displayName).length === 1 ||
!webPart.zoneGroupMetadata.displayName);
const htmlObject: HTMLDivElement = document.createElement('div');
htmlObject.innerHTML = HTMLString;