Merge pull request #3643 from ruslan-s/improvements/ensureSitePages

This commit is contained in:
Hugo Bernier 2023-04-02 21:12:11 -04:00 committed by GitHub
commit 58574d5096
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 3025 additions and 1343 deletions

View File

@ -1,18 +1,3 @@
---
page_type: sample
products:
- office-sp
languages:
- javascript
- typescript
extensions:
contentType: samples
technologies:
- SharePoint Framework
- React
createdDate: 04/30/2020 12:00:00 AM
---
# Pages Hierarchy
## Summary
@ -53,6 +38,7 @@ This web part allows users to create a faux page hierarchy in their pages librar
* [Bo George](https://github.com/bogeorge) ([@bo_george](https://twitter.com/bo_george))
* [Nick Brown](https://github.com/techienickb) ([@techienickb](https://twitter.com/techienickb))
* [SlowRobot](https://github.com/SlowRobot)
* [ruslan-s](https://github.com/ruslan-s)
## Version history
@ -62,7 +48,7 @@ Version|Date|Comments
1.2|March 24, 2022|Updated to SPFX v1.14 and PnP packages to v3
1.3|March 31, 2022|Added a Tree View
1.4|July 29, 2022|Updated Tree View Functionality
1.5|March 29, 2023|Added support for non-English SitePages library paths
## Minimal path to awesome

View File

@ -9,7 +9,7 @@
"This web part allows users to create a faux page hierarchy in their pages library and use it for page-to-page navigation."
],
"creationDateTime": "2020-04-30",
"updateDateTime": "2022-07-29",
"updateDateTime": "2023-03-29",
"products": [
"SharePoint"
],
@ -48,6 +48,11 @@
"pictureUrl": "https://github.com/techienickb.png",
"name": "Nick Brown",
"twitter": "techienickb"
},
{
"gitHubAccount": "ruslan-s",
"name": "ruslan-s",
"pictureUrl": "https://github.com/ruslan-s.png"
}
],
"references": [

View File

@ -4,7 +4,7 @@
"name": "react-pages-hierarchy",
"id": "89758fb6-85e2-4e2b-ac88-4f4e7e5f60cb",
"title": "Pages Hierarchy",
"version": "1.0.3.0",
"version": "1.0.3.1",
"includeClientSideAssets": true,
"isDomainIsolated": false,
"developer": {
@ -51,4 +51,4 @@
"paths": {
"zippedPackage": "solution/react-pages-hierarchy.sppkg"
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
import { useReducer, useEffect, useState } from 'react';
import { PermissionKind, spfi, SPFx } from '@pnp/sp/presets/all';
import { ErrorHelper, LogHelper, ListTitles, PageFields } from '@src/utilities';
import { ErrorHelper, LogHelper, PageFields } from '@src/utilities';
import { Action } from "./action";
import { GetRequest } from './getRequest';
import { IPage } from '@src/models/IPage';
@ -97,6 +97,7 @@ export function usePageApi(currentPageId: number, pageEditFinished: boolean, con
getRequest: { isLoading: false, hasError: false, errorMessage: "" },
tree: null
});
const [spLibGuid, setSpLibGuid] = useState<string>();
const sp = spfi().using(SPFx(context));
@ -104,12 +105,20 @@ export function usePageApi(currentPageId: number, pageEditFinished: boolean, con
useEffect(() => {
LogHelper.verbose('usePageApi', 'useEffect', `[currentPageId, ${currentPageId}, pageEditFinished: ${pageEditFinished} ]`);
if (currentPageId) {
if (currentPageId && !!spLibGuid) {
checkIfParentPageExists();
getPagesAsync();
}
}, [currentPageId, pageEditFinished]);
}, [currentPageId, pageEditFinished, spLibGuid]);
async function getSitePagesLibraryGuid() {
LogHelper.verbose('usePageApi', 'getSitePagesLibrary', ``);
const lib = await sp.web.lists.ensureSitePagesLibrary();
const libData = await lib();
await setSpLibGuid(libData.Id);
}
async function getPagesAsync() {
LogHelper.verbose('usePageApi', 'getPagesAsync', ``);
@ -121,7 +130,7 @@ export function usePageApi(currentPageId: number, pageEditFinished: boolean, con
// add select and order by later. Order by ID?
let pages: IPage[] = [];
let items = await sp.web.lists.getByTitle(ListTitles.SITEPAGES).items
let items = await sp.web.lists.getById(spLibGuid).items
.select(
PageFields.ID,
PageFields.TITLE,
@ -160,7 +169,7 @@ export function usePageApi(currentPageId: number, pageEditFinished: boolean, con
async function checkIfParentPageExists() {
LogHelper.verbose('usePageApi', 'parentPageExists', ``);
let parentPage = await sp.web.lists.getByTitle(ListTitles.SITEPAGES).fields
let parentPage = await sp.web.lists.getById(spLibGuid).fields
.getByInternalNameOrTitle(PageFields.PARENTPAGELOOKUP)()
.catch(e => {
// swallow the exception we'll handle below
@ -178,7 +187,7 @@ export function usePageApi(currentPageId: number, pageEditFinished: boolean, con
}
async function canCurrentUserManageSitePages(): Promise<void> {
let canManagePages = await sp.web.lists.getByTitle(ListTitles.SITEPAGES)
let canManagePages = await sp.web.lists.getById(spLibGuid)
.currentUserHasPermissions(PermissionKind.ManageLists)
.catch(e => {
ErrorHelper.handleHttpError('canUserUpdateSitePages', e);
@ -192,16 +201,16 @@ export function usePageApi(currentPageId: number, pageEditFinished: boolean, con
async function addParentPageFieldToSitePages(): Promise<void> {
LogHelper.verbose('usePageApi', 'addParentPageFieldToSitePages', ``);
let list = await sp.web.lists.getByTitle(ListTitles.SITEPAGES)();
let list = await sp.web.lists.getById(spLibGuid)();
let lookup = await sp.web.lists.getByTitle(ListTitles.SITEPAGES).fields
let lookup = sp.web.lists.getById(spLibGuid).fields
.addLookup(PageFields.PARENTPAGELOOKUP, { LookupListId: list.Id, LookupFieldName: PageFields.TITLE })
.catch(e => {
return null;
ErrorHelper.handleHttpError('canUserUpdateSitePages', e);
});
await sp.web.lists.getByTitle(ListTitles.SITEPAGES).fields
await sp.web.lists.getById(spLibGuid).fields
.getByInternalNameOrTitle(PageFields.PARENTPAGELOOKUP)
.update({ Title: PageFields.PARENTPAGELOOKUP_DISPLAYNAME })
.catch(e => {
@ -272,6 +281,10 @@ export function usePageApi(currentPageId: number, pageEditFinished: boolean, con
addParentPageFieldToSitePages();
};
useEffect(() => {
getSitePagesLibraryGuid();
}, []);
return {
state: {
parentPageColumnExists: pagesState.parentPageColumnExists,

View File

@ -1,4 +1,5 @@
import { IItemUpdateResult, IContextInfo } from '@pnp/sp/presets/all';
import { IItemUpdateResult } from '@pnp/sp/presets/all';
import { IContextInfo } from '@pnp/sp/context-info';
import { IFetchOptions } from '@pnp/common';
import { FilterParser } from './filterParser';
import { MockListFactory } from './mocklistfactory';
@ -658,7 +659,7 @@ export class MockResponse {
}
private getContextInfo(): Response {
let contexInfo: IContextInfo = {
let contexInfo: Partial<IContextInfo> = {
FormDigestTimeoutSeconds: 100,
FormDigestValue: 100
};