@@ -64,43 +64,37 @@ export default class SPService implements ISPService {
64
64
65
65
// Get ListAttachments
66
66
public async getListItemAttachments ( listId : string , itemId : number , webUrl ?: string ) : Promise < any [ ] > {
67
-
68
- let spWeb : Web ;
69
- if ( typeof webUrl !== "undefined" ) {
70
- spWeb = new Web ( webUrl ) ;
71
- } else {
72
- spWeb = new Web ( this . _context . pageContext . web . absoluteUrl ) ;
73
- }
74
-
75
67
try {
76
- const files = await spWeb . lists
77
- . getById ( listId )
78
- . items . getById ( itemId )
79
- . attachmentFiles . get ( ) ;
80
-
81
- return Promise . resolve ( files ) ;
68
+ const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
69
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items(${ itemId } )/AttachmentFiles` ;
70
+ const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
71
+ if ( data . ok ) {
72
+ const results = await data . json ( ) ;
73
+ if ( results && results . value && results . value . length > 0 ) {
74
+ return results . value ;
75
+ }
76
+ }
77
+ return null ;
82
78
} catch ( error ) {
83
79
console . dir ( error ) ;
84
80
return Promise . reject ( error ) ;
85
81
}
82
+
86
83
}
87
84
88
85
// delete attachment
89
86
public async deleteAttachment ( fileName : string , listId : string , itemId : number , webUrl ?: string ) : Promise < void > {
90
- let spWeb : Web ;
91
- if ( typeof webUrl !== "undefined" ) {
92
- spWeb = new Web ( webUrl ) ;
93
- } else {
94
- spWeb = new Web ( this . _context . pageContext . web . absoluteUrl ) ;
95
- }
96
87
try {
97
- await spWeb . lists
98
- . getById ( listId )
99
- . items . getById ( itemId )
100
- . attachmentFiles . getByName ( encodeURIComponent ( fileName ) )
101
- . delete ( ) ;
102
- return ;
88
+ const spOpts : ISPHttpClientOptions = {
89
+ headers : { "X-HTTP-Method" : 'DELETE' , }
90
+ } ;
91
+ const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
92
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items(${ itemId } )/AttachmentFiles/getByFileName('${ encodeURIComponent ( fileName ) } ')` ;
93
+ const data = await this . _context . spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , spOpts ) ;
94
+
95
+
103
96
} catch ( error ) {
97
+ console . dir ( error ) ;
104
98
return Promise . reject ( error ) ;
105
99
}
106
100
}
@@ -124,12 +118,15 @@ export default class SPService implements ISPService {
124
118
await this . deleteAttachment ( fileName , listId , itemId , webUrl ) ;
125
119
}
126
120
// Add Attachment
127
- const files = await spWeb . lists
128
- . getById ( listId )
129
- . items . getById ( itemId )
130
- . attachmentFiles . add ( fileName , file ) ;
121
+ const spOpts : ISPHttpClientOptions = {
122
+ body : file
123
+ } ;
124
+ const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
125
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items(${ itemId } )/AttachmentFiles/add(FileName='${ encodeURIComponent ( fileName ) } ')` ;
126
+ const data = await this . _context . spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , spOpts ) ;
131
127
132
128
return ;
129
+
133
130
} catch ( error ) {
134
131
return Promise . reject ( error ) ;
135
132
}
@@ -138,21 +135,17 @@ export default class SPService implements ISPService {
138
135
// get Attachment
139
136
//
140
137
public async getAttachment ( listId : string , itemId : number , fileName : string , webUrl ?: string ) : Promise < any > {
141
- let spWeb : Web ;
142
- if ( typeof webUrl !== "undefined" ) {
143
- spWeb = new Web ( webUrl ) ;
144
- } else {
145
- spWeb = new Web ( this . _context . pageContext . web . absoluteUrl ) ;
146
- }
147
- try {
148
- const file = await spWeb . lists
149
- . getById ( listId )
150
- . items . getById ( itemId )
151
- . attachmentFiles . getByName ( encodeURIComponent ( fileName ) ) . get ( ) ;
152
- return Promise . resolve ( file ) ;
153
- } catch ( error ) {
154
- return Promise . reject ( error ) ;
138
+ const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
139
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items(${ itemId } )/AttachmentFiles/GetByFileBame('${ fileName } '))` ;
140
+ const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
141
+ if ( data . ok ) {
142
+ const file = await data . json ( ) ;
143
+ if ( file ) {
144
+ return file ;
145
+ }
155
146
}
147
+
148
+ return null ;
156
149
}
157
150
// Check if Attachment Exists
158
151
public async checkAttachmentExists ( listId : string , itemId : number , fileName : string , webUrl ?: string ) : Promise < any > {
@@ -177,22 +170,15 @@ export default class SPService implements ISPService {
177
170
178
171
// Get ListName
179
172
public async getListName ( listId : string , webUrl ?: string ) : Promise < string > {
180
- let spWeb : Web ;
181
- if ( typeof webUrl !== "undefined" ) {
182
- spWeb = new Web ( webUrl ) ;
183
- } else {
184
- spWeb = new Web ( this . _context . pageContext . web . absoluteUrl ) ;
185
- }
186
- try {
187
- const list = await spWeb . lists
188
- . getById ( listId )
189
- . select ( 'RootFolder/Name' )
190
- . expand ( 'RootFolder' )
191
- . get ( ) ;
192
-
193
- return Promise . resolve ( list . RootFolder . Name ) ;
194
- } catch ( error ) {
195
- return Promise . reject ( error ) ;
173
+ const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
174
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')?$select=RootFolder/Name&$expand=RootFolder)` ;
175
+ const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
176
+ if ( data . ok ) {
177
+ const results = await data . json ( ) ;
178
+ if ( results ) {
179
+ return results . RootFolder . Name ;
180
+ }
196
181
}
182
+ return ;
197
183
}
198
184
}
0 commit comments