diff --git a/src/utils/auth/common.ts b/src/utils/auth/common.ts index f96ca90..256f832 100644 --- a/src/utils/auth/common.ts +++ b/src/utils/auth/common.ts @@ -2,7 +2,7 @@ import { getCacheItem, IRestOptions, setCacheItem } from "../../exports-index"; import { isNullOrEmptyString, isNullOrUndefined, isNumber } from "../../helpers/typecheckers"; import { SPFxAuthToken, SPFxAuthTokenType } from "../../types/auth"; import { GetJson, GetJsonSync } from "../rest"; -import { GetRestBaseUrl } from "../sharepoint.rest/common"; +import { GetRestBaseUrl, hasGlobalContext } from "../sharepoint.rest/common"; export function GetTokenAudiencePrefix(appId: string) { return `api://${appId}`; @@ -66,7 +66,7 @@ function _getGetSPFxClientAuthTokenParams(siteUrl: string, spfxTokenType: SPFxAu } function _parseAndCacheGetSPFxClientAuthTokenResult(result: SPFxAuthToken, spfxTokenType: SPFxAuthTokenType = SPFxAuthTokenType.Graph) { - if (!isNullOrUndefined(result) && !isNullOrEmptyString(result.access_token)) { + if (hasGlobalContext() && !isNullOrUndefined(result) && !isNullOrEmptyString(result.access_token)) { let expiration = isNumber(result.expires_on) ? new Date(result.expires_on * 1000) : { @@ -81,9 +81,11 @@ function _parseAndCacheGetSPFxClientAuthTokenResult(result: SPFxAuthToken, spfxT } function _getSPFxClientAuthTokenFromCache(spfxTokenType: SPFxAuthTokenType = SPFxAuthTokenType.Graph) { - let cachedToken = getCacheItem(`access_token_${spfxTokenType}_${_spPageContextInfo.webId}`); - if (!isNullOrEmptyString(cachedToken)) { - return cachedToken; + if (hasGlobalContext()) { + let cachedToken = getCacheItem(`access_token_${spfxTokenType}_${_spPageContextInfo.webId}`); + if (!isNullOrEmptyString(cachedToken)) { + return cachedToken; + } } return null; } diff --git a/src/utils/sharepoint.rest/date.ts b/src/utils/sharepoint.rest/date.ts index 1dcb2b7..5032106 100644 --- a/src/utils/sharepoint.rest/date.ts +++ b/src/utils/sharepoint.rest/date.ts @@ -4,13 +4,16 @@ import { DateOrNull } from "../../types/common.types"; import { toIsoDateFormat } from "../date"; import { LocaleKnownScript } from "../knownscript"; import { SPServerLocalTimeToUTCSync } from "./web"; +import { hasGlobalContext } from "./common"; function _SPServerLocalTimeToUTC(dateValueStr: string) { - //yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC - let utcTimeValue = SPServerLocalTimeToUTCSync(_spPageContextInfo.webServerRelativeUrl, dateValueStr); + if (hasGlobalContext()) { + //yyyy-MM-ddTHH:mm or SPServerLocalTime (5/27/2021 11:34) to UTC + let utcTimeValue = SPServerLocalTimeToUTCSync(_spPageContextInfo.webServerRelativeUrl, dateValueStr); - if (!isNullOrEmptyString(utcTimeValue)) { - return new Date(utcTimeValue); + if (!isNullOrEmptyString(utcTimeValue)) { + return new Date(utcTimeValue); + } } return null; } diff --git a/src/utils/sharepoint.rest/item.ts b/src/utils/sharepoint.rest/item.ts index 2e46b2b..1d299db 100644 --- a/src/utils/sharepoint.rest/item.ts +++ b/src/utils/sharepoint.rest/item.ts @@ -10,7 +10,7 @@ import { IRestItem } from "../../types/sharepoint.utils.types"; import { LocaleKnownScript } from "../../utils/knownscript"; import { ConsoleLogger } from "../consolelogger"; import { GetJson, GetJsonSync } from "../rest"; -import { GetFieldNameFromRawValues, GetSiteUrl, __getSPRestErrorData, getFieldNameForUpdate } from "./common"; +import { GetFieldNameFromRawValues, GetSiteUrl, __getSPRestErrorData, getFieldNameForUpdate, hasGlobalContext } from "./common"; import { GetList, GetListFields, GetListFieldsAsHash, GetListRestUrl } from "./list"; import { GetUser, GetUserSync } from "./user"; @@ -423,7 +423,7 @@ export function GetSPFieldValueAsText(value: any, field: IFieldInfoEX): string[] rawValues.forEach(raw => { if (isNullOrEmptyString(raw)) {/** skip */ } else if (isNumber(raw)) - if (isUser) { + if (isUser && hasGlobalContext()) { //todo - try not sync... try { let userInfo = GetUserSync(_spPageContextInfo.siteServerRelativeUrl, raw); diff --git a/src/utils/sharepoint.rest/web.ts b/src/utils/sharepoint.rest/web.ts index c7553c1..e353561 100644 --- a/src/utils/sharepoint.rest/web.ts +++ b/src/utils/sharepoint.rest/web.ts @@ -86,7 +86,10 @@ export function GetTenantId() { /** Get tenant id lower case no {} */ export function GetPortalUrl() { - return _spPageContextInfo.portalUrl; + if (hasGlobalContext()) { + return _spPageContextInfo.portalUrl; + } + return null; } /** Get site id lower case no {} */ @@ -478,7 +481,7 @@ export async function GetAllSubWebs(siteUrl: string, options?: { allSiteCollecti if (queryFailed) { // Igor: Issue #7702 - if (_spPageContextInfo && _spPageContextInfo.siteServerRelativeUrl.toLowerCase() !== siteUrl.toLowerCase()) { + if (hasGlobalContext() && _spPageContextInfo && _spPageContextInfo.siteServerRelativeUrl.toLowerCase() !== siteUrl.toLowerCase()) { //siteUrl = _spPageContextInfo.siteServerRelativeUrl; //currentSite = await GetWebInfo(siteUrl); //Kevin: Issue 1028 @@ -937,7 +940,7 @@ export async function SPServerLocalTime(siteUrl: string) { export function GetContextWebInformationSync(siteUrl: string): IContextWebInformation { var siteId: string = null; - if (_spPageContextInfo && _spPageContextInfo.isAppWeb) { + if (hasGlobalContext() && _spPageContextInfo && _spPageContextInfo.isAppWeb) { //inside an app web you can't get the contextinfo for any other site siteUrl = _spPageContextInfo.webServerRelativeUrl; siteId = _spPageContextInfo.siteId;