TypeScript functions to get current domain, site collection and Site urls with window.location fallback

Introduction

Recently, I was working in a TypeScript project building a SPA application on top of Office 365 and I needed to get the current domain, current site collection and current site urls for using them in a different React components.

The case is we always rely on _spPageContextInfo to get this information, but in fact, we can use window.location object to build the urls as a fallback in case we need to use this Utils library before SP.js loaded or in other context.

Code

Here is my solution for that problem using TypeScript:

export class Utils {
/*
* Function to get Site Collection URL
* Samples:
* "https://domain.sharepoint.com/sites/intranet"
*/
public static getSiteCollectionUrl(): string {
if (window
&& "location" in window
&& "protocol" in window.location
&& "pathname" in window.location
&& "host" in window.location) {
let baseUrl = window.location.protocol + "//" + window.location.host;
const pathname = window.location.pathname;
const siteCollectionDetector = "/sites/";
if (pathname.indexOf(siteCollectionDetector) >= 0) {
baseUrl += pathname.substring(0, pathname.indexOf("/", siteCollectionDetector.length));
}
return baseUrl;
}
return null;
}
/*
* Function to get Current Site Url
* Samples:
* "https://domain.sharepoint.com/sites/intranet/subsite/Pages/Home.aspx"
*/
public static getCurrentAbsoluteSiteUrl(): string {
if (window
&& "location" in window
&& "protocol" in window.location
&& "pathname" in window.location
&& "host" in window.location) {
return window.location.protocol + "//" + window.location.host + window.location.pathname;
}
return null;
}
/*
* Function to get Current Site Url
* Samples:
* "/sites/intranet"
*/
public static getWebServerRelativeUrl(): string {
if (window
&& "location" in window
&& "pathname" in window.location) {
return window.location.pathname.replace(/\/$/, "");
}
return null;
}
/*
* Function to get Layout Page Url
* Replacement in SPFx for SP.Utilities.Utility.getLayoutsPageUrl('sp.js')
* Samples:
* getLayoutsPageUrl('sp.js')
* "/sites/intranet/_layouts/15/sp.js"
*/
public static getLayoutsPageUrl(libraryName: string): string {
if (window
&& "location" in window
&& "pathname" in window.location
&& libraryName !== "") {
return window.location.pathname.replace(/\/$/, "") + "/_layouts/15/" + libraryName;
}
return null;
}
/*
* Function to get Current Domain Url
* Samples:
* "https://domain.sharepoint.com"
*/
public static getAbsoluteDomainUrl(): string {
if (window
&& "location" in window
&& "protocol" in window.location
&& "host" in window.location) {
return window.location.protocol + "//" + window.location.host;
}
return null;
}
}

 

Author: José Quinto
Link: https://blog.josequinto.com/2017/03/09/typescript-functions-to-get-current-domain-site-collection-and-site-urls-with-window-location-fallback/
Copyright Notice: All articles in this blog are licensed under CC BY-SA 4.0 unless stating additionally.