I am using this code:
Use document.referrer
from the plugin as a URL source.
Locate the project name and extract the part of the URL before the project name.
const url = document.referrer;
// how to detect base Url: get projectName and find 'ProjectName/_'
// http://tfs2017-test:8080/tfs/Org/MyProject/_apps/hub/...
const projectService = await SDK.getService<IProjectPageService>(CommonServiceIds.ProjectPageService);
const project = await projectService.getProject();
if (!project) {
throw new Error("Cannot get project.")
}
const findStr = `${project.name}/_`;
const index = url.indexOf(findStr);
if (index < 0) {
throw new Error(`URL '${url}' does not contain '${findStr}' substring`);
}
// extract from url without '_'
this._baseUrl = url.substring(0, index + findStr.length - 1);
Edit 04.05.2021:
Because document.referrer is not working good for some browsers, I am using now more "DevOps way":
// https://github.com/microsoft/azure-devops-extension-sdk/issues/28
this._locationService = await SDK.getService<ILocationService>(CommonServiceIds.LocationService);
const hostBaseUrl = await this._locationService.getResourceAreaLocation(
CoreRestClient.RESOURCE_AREA_ID
);
console.log(`hostBaseUrl: ${hostBaseUrl}`);
const projectService = await SDK.getService<IProjectPageService>(CommonServiceIds.ProjectPageService);
const project = await projectService.getProject();
if (!project) {
throw new Error("Cannot get project.")
}
this._baseUrl = `${hostBaseUrl}${project.name}/`;
console.log(`baseUrl: ${this._baseUrl}`);