use ensureSitePages routine to surely get the library

This commit is contained in:
Ruslan Schuh 2023-03-28 09:56:03 +02:00
parent d00f00b857
commit 9b10ab8717
3 changed files with 37669 additions and 1214 deletions

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

@ -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,