Skip to content

web info cleanup #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/utils/sharepoint.rest/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FieldTypeAsString, IFieldInfoEX, IFieldTaxonomyInfo } from "../../types
import { ISPRestError } from "../../types/sharepoint.utils.types";
import { ConsoleLogger } from "../consolelogger";
import { getCacheItem, setCacheItem } from "../localstoragecache";
import { mediumLocalCache } from "../rest";
import { GetJsonSync, longLocalCache, mediumLocalCache } from "../rest";
import { GetWebIdSync, GetWebInfoSync } from "./web";

const logger = ConsoleLogger.get("sharepoint.rest/common");
Expand All @@ -26,15 +26,18 @@ export function hasGlobalContext() {
export function GetFileSiteUrl(fileUrl: string): string {
let siteUrl: string;
let urlParts = fileUrl.split('/');
if (urlParts[urlParts.length - 1].indexOf('.') > 0)//file name
urlParts.pop();//file name

let key = "GetSiteUrl|" + urlParts.join("/").toLowerCase();
let key = "GetSiteUrl|" + fileUrl.toLowerCase();
siteUrl = getCacheItem<string>(key);
if (isNullOrUndefined(siteUrl)) {
while (!isValidGuid(GetWebIdSync(urlParts.join('/'))))
while (urlParts.length > 0) {
const candidateUrl = makeServerRelativeUrl(normalizeUrl(urlParts.join("/"), true))
const syncResult = GetJsonSync<{ d: { Id: string; }; }>(`${candidateUrl}_api/web/Id`, null, { ...longLocalCache });
if (syncResult.success && isValidGuid(syncResult.result.d.Id)) {
break
}
urlParts.pop();

}
siteUrl = normalizeUrl(urlParts.join('/'));
setCacheItem(key, siteUrl, mediumLocalCache.localStorageExpiration);//keep for 15 minutes
}
Expand All @@ -46,8 +49,16 @@ export function GetFileSiteUrl(fileUrl: string): string {
* If you send a guid - it will look for a site with that ID in the current context site collection
*/
export function GetSiteUrl(siteUrlOrId?: string): string {
let siteUrl: string;
if (isNullOrUndefined(siteUrlOrId)) {
if (!isNullOrUndefined(siteUrlOrId) && isValidGuid(siteUrlOrId)) {
const webInfo = GetWebInfoSync(null, siteUrlOrId);
return makeServerRelativeUrl(normalizeUrl(webInfo.ServerRelativeUrl, true));
}
return GetSiteUrlLocally(siteUrlOrId);
}

/** gets a siteUrl locally (without making requests) (todo although currently GetFileSiteUrl does make requests...) */
export function GetSiteUrlLocally(siteUrl?: string): string {
if (isNullOrUndefined(siteUrl)) {
if (hasGlobalContext()) {
siteUrl = _spPageContextInfo.webServerRelativeUrl;
if (_spPageContextInfo.isAppWeb)//#1300 if in a classic app sub-site
Expand All @@ -57,20 +68,13 @@ export function GetSiteUrl(siteUrlOrId?: string): string {
siteUrl = GetFileSiteUrl(window.location.pathname);
}
}
else if (isValidGuid(siteUrlOrId)) {
//GetWebInfoSync calls GetSiteUrl recursively, but with null should not get in here
let webInfo = GetWebInfoSync(null, siteUrlOrId);
siteUrl = webInfo.ServerRelativeUrl;
}
else siteUrl = siteUrlOrId;

//must end with / otherwise root sites will return "" and we will think there is no site url.
return makeServerRelativeUrl(normalizeUrl(siteUrl, true));
}

/** gets a site url, returns its REST _api url */
export function GetRestBaseUrl(siteUrl: string): string {
siteUrl = GetSiteUrl(siteUrl);
siteUrl = GetSiteUrlLocally(siteUrl);
return siteUrl + '_api';
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/sharepoint.rest/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { AutoDiscoverTenantInfo } from "../auth/discovery";
import { ConsoleLogger } from "../consolelogger";
import { toIsoDateFormat } from "../date";
import { GetJson, GetJsonSync, extraLongLocalCache, longLocalCache, mediumLocalCache, noLocalCache, shortLocalCache, weeekLongLocalCache } from "../rest";
import { CONTENT_TYPES_SELECT, CONTENT_TYPES_SELECT_WITH_FIELDS, GetRestBaseUrl, GetSiteUrl, LIST_EXPAND, LIST_SELECT, WEB_SELECT, hasGlobalContext } from "./common";
import { CONTENT_TYPES_SELECT, CONTENT_TYPES_SELECT_WITH_FIELDS, GetRestBaseUrl, GetSiteUrl, GetSiteUrlLocally, LIST_EXPAND, LIST_SELECT, WEB_SELECT, hasGlobalContext } from "./common";
import { GetListFields, GetListFieldsSync, GetListRestUrl } from "./list";
import { SPTimeZoneIdToIANATimeZoneName } from "./timzone-map";

Expand Down Expand Up @@ -611,7 +611,7 @@ export async function GetWebInfo(siteUrl: string, webId?: string, refreshCache?:
if (currentWebId !== webId) {
let url = _getWebInfoByIdRequestUrl(siteUrl, webId);
webInfoResponse = await GetJson<IGetWebInfoResponse>(url, null, {
method: "POST", spWebUrl: GetSiteUrl(siteUrl), ...shortLocalCache,
method: "POST", spWebUrl: GetSiteUrlLocally(siteUrl), ...shortLocalCache,
jsonMetadata: jsonTypes.nometadata,
allowCache: refreshCache !== true
});
Expand Down Expand Up @@ -641,7 +641,7 @@ export function GetWebInfoSync(siteUrl: string, webId?: string): IWebBasicInfo {
if (currentWebId !== webId) {
let url = _getWebInfoByIdRequestUrl(siteUrl, webId);
let syncResult = GetJsonSync<IGetWebInfoResponse>(url, null, {
method: "POST", spWebUrl: GetSiteUrl(siteUrl), ...shortLocalCache,
method: "POST", spWebUrl: GetSiteUrlLocally(siteUrl), ...shortLocalCache,
jsonMetadata: jsonTypes.nometadata
});
if (syncResult.success) {
Expand Down