@@ -6,13 +6,15 @@ import { GeneralHelper } from "../common/utilities/GeneralHelper";
6
6
export class FileBrowserService {
7
7
protected itemsToDownloadCount : number ;
8
8
protected context : BaseComponentContext ;
9
+ protected siteAbsoluteUrl : string ;
9
10
10
11
protected driveAccessToken : string ;
11
12
protected mediaBaseUrl : string ;
12
13
protected callerStack : string ;
13
14
14
- constructor ( context : BaseComponentContext , itemsToDownloadCount : number = 100 ) {
15
+ constructor ( context : BaseComponentContext , itemsToDownloadCount : number = 100 , siteAbsoluteUrl ?: string ) {
15
16
this . context = context ;
17
+ this . siteAbsoluteUrl = siteAbsoluteUrl || context . pageContext . web . absoluteUrl ;
16
18
17
19
this . itemsToDownloadCount = itemsToDownloadCount ;
18
20
this . driveAccessToken = null ;
@@ -27,7 +29,7 @@ export class FileBrowserService {
27
29
public getListItems = async ( listUrl : string , folderPath : string , acceptedFilesExtensions ?: string [ ] , nextPageQueryStringParams ?: string , sortBy ?: string , isDesc ?: boolean ) : Promise < FilesQueryResult > => {
28
30
let filesQueryResult : FilesQueryResult = { items : [ ] , nextHref : null } ;
29
31
try {
30
- let restApi = `${ this . context . pageContext . web . absoluteUrl } /_api/web/GetList('${ listUrl } ')/RenderListDataAsStream` ;
32
+ let restApi = `${ this . siteAbsoluteUrl } /_api/web/GetList('${ listUrl } ')/RenderListDataAsStream` ;
31
33
32
34
// Do not pass FolderServerRelativeUrl as query parameter
33
35
// Attach passed nextPageQueryStringParams values to REST URL
@@ -59,7 +61,7 @@ export class FileBrowserService {
59
61
*/
60
62
public getSiteMediaLibraries = async ( includePageLibraries : boolean = false ) => {
61
63
try {
62
- const absoluteUrl = this . context . pageContext . web . absoluteUrl ;
64
+ const absoluteUrl = this . siteAbsoluteUrl ;
63
65
const restApi = `${ absoluteUrl } /_api/SP.Web.GetDocumentAndMediaLibraries?webFullUrl='${ encodeURIComponent ( absoluteUrl ) } '&includePageLibraries='${ includePageLibraries } '` ;
64
66
const mediaLibrariesResult = await this . context . spHttpClient . get ( restApi , SPHttpClient . configurations . v1 ) ;
65
67
@@ -84,7 +86,7 @@ export class FileBrowserService {
84
86
*/
85
87
public getLibraryNameByInternalName = async ( internalName : string ) : Promise < string > => {
86
88
try {
87
- const absoluteUrl = this . context . pageContext . web . absoluteUrl ;
89
+ const absoluteUrl = this . siteAbsoluteUrl ;
88
90
const restApi = `${ absoluteUrl } /_api/web/GetFolderByServerRelativeUrl('${ internalName } ')/Properties?$select=vti_x005f_listtitle` ;
89
91
const libraryResult = await this . context . spHttpClient . get ( restApi , SPHttpClient . configurations . v1 ) ;
90
92
@@ -155,6 +157,25 @@ export class FileBrowserService {
155
157
return fieldName ;
156
158
}
157
159
160
+ /**
161
+ * Gets the Title of the current Web
162
+ * @returns SharePoint Site Title
163
+ */
164
+ public getSiteTitle = async ( ) : Promise < string > => {
165
+ const restApi = `${ this . siteAbsoluteUrl } /_api/web?$select=Title` ;
166
+ const webResult = await this . context . spHttpClient . get ( restApi , SPHttpClient . configurations . v1 ) ;
167
+
168
+ if ( ! webResult || ! webResult . ok ) {
169
+ throw new Error ( `Something went wrong when executing request. Status='${ webResult . status } '` ) ;
170
+ }
171
+ if ( ! webResult || ! webResult ) {
172
+ throw new Error ( `Cannot read data from the results.` ) ;
173
+ }
174
+ let webJson = await webResult . json ( ) ;
175
+ return webJson . Title ;
176
+
177
+ }
178
+
158
179
/**
159
180
* Executes query to load files with possible extension filtering
160
181
* @param restApi
@@ -325,7 +346,7 @@ export class FileBrowserService {
325
346
* Creates an absolute URL
326
347
*/
327
348
protected buildAbsoluteUrl = ( relativeUrl : string ) => {
328
- const siteUrl : string = GeneralHelper . getAbsoluteDomainUrl ( this . context . pageContext . web . absoluteUrl ) ;
349
+ const siteUrl : string = GeneralHelper . getAbsoluteDomainUrl ( this . siteAbsoluteUrl ) ;
329
350
return `${ siteUrl } ${ relativeUrl . indexOf ( '/' ) === 0 ? '' : '/' } ${ relativeUrl } ` ;
330
351
}
331
352
0 commit comments