1
- import { MSGraphClientV3 } from "@microsoft/sp-http" ;
2
- import { AdaptiveCardExtensionContext } from '@microsoft/sp-adaptive-card-extension-base' ;
3
- import { GraphFiles } from './types' ;
4
-
5
-
6
- export interface IFileService {
7
- _getFiles ( siteAddress : string , listTitle : string ) : Promise < GraphFiles > ;
8
- }
9
-
10
- export class FileService implements IFileService {
11
- public context : AdaptiveCardExtensionContext ;
12
- private MSGraphClient : MSGraphClientV3 ;
13
-
14
- constructor ( context : AdaptiveCardExtensionContext ) {
15
- this . context = context ;
16
- }
17
-
18
- public async _getFiles ( siteAddress : string , listTitle : string ) : Promise < GraphFiles > {
19
- let files : GraphFiles ;
20
- try {
21
- const client = await this . _getClient ( ) ;
22
- const siteId = await this . _getSiteId ( client , siteAddress ) ;
23
- const listId = await this . _getListId ( client , siteId , listTitle ) ;
24
- files = await client . api ( "sites/" + siteId + "/lists/" + listId + "/items" ) . select ( "contentType" ) . version ( 'beta' ) . get ( ) ;
25
- } catch {
26
- files = { value :[ ] } ;
27
- }
28
- return files ;
29
- }
30
-
31
- private async _getSiteId ( client :MSGraphClientV3 , siteAddress : string ) : Promise < string > {
32
- const hostname = siteAddress . split ( '/' ) [ 2 ] ;
33
- const serverRelativeUrl = siteAddress . split ( hostname ) [ 1 ] ;
34
- const siteId = await client . api ( "sites/" + hostname + ":" + serverRelativeUrl ) . version ( 'beta' ) . get ( ) ;
35
- return siteId . id ;
36
- }
37
-
38
- private async _getListId ( client :MSGraphClientV3 , siteId : string , listTitle : string ) : Promise < string > {
39
- const list = await client . api ( "sites/" + siteId + "/lists" ) . version ( 'beta' ) . filter ( "displayName eq '" + listTitle + "'" ) . get ( ) ;
40
- return list . value [ 0 ] . id ;
41
- }
42
-
43
- private async _getClient ( ) : Promise < MSGraphClientV3 > {
44
- if ( this . MSGraphClient === undefined )
45
- this . MSGraphClient = await this . context . msGraphClientFactory . getClient ( "3" ) ;
46
- return this . MSGraphClient ;
47
- }
1
+ import { MSGraphClientV3 } from "@microsoft/sp-http" ;
2
+ import { AdaptiveCardExtensionContext } from '@microsoft/sp-adaptive-card-extension-base' ;
3
+ import { GraphFiles } from './types' ;
4
+
5
+
6
+ export interface IFileService {
7
+ _getFiles ( siteAddress : string , listTitle : string ) : Promise < GraphFiles > ;
8
+ }
9
+
10
+ export class FileService implements IFileService {
11
+ public context : AdaptiveCardExtensionContext ;
12
+ private MSGraphClient : MSGraphClientV3 ;
13
+
14
+ constructor ( context : AdaptiveCardExtensionContext ) {
15
+ this . context = context ;
16
+ }
17
+
18
+ public async _getFiles ( siteAddress : string , listTitle : string ) : Promise < GraphFiles > {
19
+ let files : GraphFiles ;
20
+ try {
21
+ const client = await this . _getClient ( ) ;
22
+ const siteId = await this . _getSiteId ( client , siteAddress ) ;
23
+ const listId = await this . _getListId ( client , siteId , listTitle ) ;
24
+ files = await client . api ( "sites/" + siteId + "/lists/" + listId + "/items" ) . select ( "contentType" ) . version ( 'beta' ) . get ( ) ;
25
+ } catch {
26
+ files = { value :[ ] } ;
27
+ }
28
+ return files ;
29
+ }
30
+
31
+ private async _getSiteId ( client :MSGraphClientV3 , siteAddress : string ) : Promise < string > {
32
+ const hostname = siteAddress . split ( '/' ) [ 2 ] ;
33
+ const serverRelativeUrl = siteAddress . split ( hostname ) [ 1 ] ;
34
+ const siteId = await client . api ( "sites/" + hostname + ":" + serverRelativeUrl ) . version ( 'beta' ) . get ( ) ;
35
+ return siteId . id ;
36
+ }
37
+
38
+ private async _getListId ( client :MSGraphClientV3 , siteId : string , listTitle : string ) : Promise < string > {
39
+ const list = await client . api ( "sites/" + siteId + "/lists" ) . version ( 'beta' ) . filter ( "displayName eq '" + listTitle + "'" ) . get ( ) ;
40
+ return list . value [ 0 ] . id ;
41
+ }
42
+
43
+ private async _getClient ( ) : Promise < MSGraphClientV3 > {
44
+ if ( this . MSGraphClient === undefined )
45
+ this . MSGraphClient = await this . context . msGraphClientFactory . getClient ( "3" ) ;
46
+ return this . MSGraphClient ;
47
+ }
48
48
}
0 commit comments