@@ -6,7 +6,11 @@ import { SPHttpClient, ISPHttpClientOptions } from "@microsoft/sp-http";
66
77export default class SPService implements ISPService {
88
9- constructor ( private _context : WebPartContext | ExtensionContext ) { }
9+ private _webAbsoluteUrl : string ;
10+
11+ constructor ( private _context : WebPartContext | ExtensionContext , webAbsoluteUrl ?: string ) {
12+ this . _webAbsoluteUrl = webAbsoluteUrl ? webAbsoluteUrl : this . _context . pageContext . web . absoluteUrl ;
13+ }
1014
1115 /**
1216 * Get lists or libraries
@@ -15,7 +19,7 @@ export default class SPService implements ISPService {
1519 */
1620 public async getLibs ( options ?: ILibsOptions ) : Promise < ISPLists > {
1721 let filtered : boolean ;
18- let queryUrl : string = `${ this . _context . pageContext . web . absoluteUrl } /_api/web/lists?$select=Title,id,BaseTemplate` ;
22+ let queryUrl : string = `${ this . _webAbsoluteUrl } /_api/web/lists?$select=Title,id,BaseTemplate` ;
1923
2024 if ( options . orderBy ) {
2125 queryUrl += `&$orderby=${ options . orderBy === LibsOrderBy . Id ? 'Id' : 'Title' } ` ;
@@ -52,7 +56,7 @@ export default class SPService implements ISPService {
5256 `substringof('${ encodeURIComponent ( filterText . replace ( "'" , "''" ) ) } ',${ internalColumnName } )${ filter ? ' and ' + filter : '' } `
5357 : `startswith(${ internalColumnName } ,'${ encodeURIComponent ( filterText . replace ( "'" , "''" ) ) } ')${ filter ? ' and ' + filter : '' } ` ; //string = filterList ? `and ${filterList}` : '';
5458 try {
55- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
59+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
5660 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items?$select=${ keyInternalColumnName || 'Id' } ,${ internalColumnName } &$filter=${ filterStr } ` ;
5761 const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
5862 if ( data . ok ) {
@@ -109,7 +113,7 @@ export default class SPService implements ISPService {
109113
110114 try {
111115 const webAbsoluteUrl = ! webUrl
112- ? this . _context . pageContext . web . absoluteUrl
116+ ? this . _webAbsoluteUrl
113117 : webUrl ;
114118 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items?$orderby=${ internalColumnName } &$select=${ keyInternalColumnName ||
115119 "Id" } ,${ internalColumnName } &${ _filter } ${ costumfilter } ${ _top } `;
@@ -144,7 +148,7 @@ export default class SPService implements ISPService {
144148 */
145149 public async getListItemAttachments ( listId : string , itemId : number , webUrl ?: string ) : Promise < any [ ] > {
146150 try {
147- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
151+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
148152 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/items(@itemId)/AttachmentFiles?@listId=guid'${ encodeURIComponent ( listId ) } '&@itemId=${ encodeURIComponent ( String ( itemId ) ) } ` ;
149153 const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
150154 if ( data . ok ) {
@@ -174,7 +178,7 @@ export default class SPService implements ISPService {
174178 const spOpts : ISPHttpClientOptions = {
175179 headers : { "X-HTTP-Method" : 'DELETE' , }
176180 } ;
177- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
181+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
178182 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/items(@itemId)/AttachmentFiles/getByFileName(@fileName)/RecycleObject?@listId=guid'${ encodeURIComponent ( listId ) } '&@itemId=${ encodeURIComponent ( String ( itemId ) ) } &@fileName='${ encodeURIComponent ( fileName . replace ( / ' / g, "''" ) ) } '` ;
179183 const data = await this . _context . spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , spOpts ) ;
180184 } catch ( error ) {
@@ -206,7 +210,7 @@ export default class SPService implements ISPService {
206210 const spOpts : ISPHttpClientOptions = {
207211 body : file
208212 } ;
209- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
213+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
210214 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/items(@itemId)/AttachmentFiles/add(FileName=@fileName)?@listId=guid'${ encodeURIComponent ( listId ) } '&@itemId=${ encodeURIComponent ( String ( itemId ) ) } &@fileName='${ encodeURIComponent ( fileName . replace ( / ' / g, "''" ) ) } '` ;
211215 const data = await this . _context . spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , spOpts ) ;
212216 return ;
@@ -224,7 +228,7 @@ export default class SPService implements ISPService {
224228 * @param webUrl
225229 */
226230 public async getAttachment ( listId : string , itemId : number , fileName : string , webUrl ?: string ) : Promise < any > {
227- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
231+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
228232 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/items(@itemId)/AttachmentFiles/GetByFileBame(@fileName))?@listId=guid'${ encodeURIComponent ( listId ) } '&@itemId=${ encodeURIComponent ( String ( itemId ) ) } &@fileName='${ encodeURIComponent ( fileName . replace ( / ' / g, "''" ) ) } '` ;
229233 const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
230234 if ( data . ok ) {
@@ -248,7 +252,7 @@ export default class SPService implements ISPService {
248252 public async checkAttachmentExists ( listId : string , itemId : number , fileName : string , webUrl ?: string ) : Promise < any > {
249253 try {
250254 const listServerRelativeUrl = await this . getListServerRelativeUrl ( listId , webUrl ) ;
251- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
255+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
252256 const fileServerRelativeUrl = `${ listServerRelativeUrl } /Attachments/${ itemId } /${ fileName } ` ;
253257 const apiUrl = `${ webAbsoluteUrl } /_api/web/getfilebyserverrelativeurl(@url)/Exists?@url='${ encodeURIComponent ( fileServerRelativeUrl . replace ( / ' / g, "''" ) ) } '` ;
254258 const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
@@ -272,7 +276,7 @@ export default class SPService implements ISPService {
272276 * @param webUrl
273277 */
274278 public async getListName ( listId : string , webUrl ?: string ) : Promise < string > {
275- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
279+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
276280 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/RootFolder/Name?@listId=guid'${ encodeURIComponent ( listId ) } '` ;
277281 const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
278282 if ( data . ok ) {
@@ -292,7 +296,7 @@ export default class SPService implements ISPService {
292296 * @param webUrl
293297 */
294298 public async getListServerRelativeUrl ( listId : string , webUrl ?: string ) : Promise < string > {
295- const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
299+ const webAbsoluteUrl = ! webUrl ? this . _webAbsoluteUrl : webUrl ;
296300 const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/RootFolder/ServerRelativeUrl?@listId=guid'${ encodeURIComponent ( listId ) } '` ;
297301 const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
298302 if ( data . ok ) {
0 commit comments