@@ -2,7 +2,7 @@ import { ISPService, ILibsOptions, LibsOrderBy } from "./ISPService";
2
2
import { ISPLists } from "../common/SPEntities" ;
3
3
import { WebPartContext } from "@microsoft/sp-webpart-base" ;
4
4
import { ExtensionContext } from "@microsoft/sp-extension-base" ;
5
- import { SPHttpClient , SPHttpClientResponse , ISPHttpClientOptions } from "@microsoft/sp-http" ;
5
+ import { SPHttpClient , SPHttpClientResponse , ISPHttpClientOptions } from "@microsoft/sp-http" ;
6
6
import { sp , Web } from '@pnp/sp' ;
7
7
8
8
export default class SPService implements ISPService {
@@ -63,24 +63,25 @@ export default class SPService implements ISPService {
63
63
}
64
64
}
65
65
66
- /**
67
- * Get list item attachments
68
- *
69
- * @param listId
70
- * @param itemId
71
- * @param webUrl
72
- */
73
- public async getListItemAttachments ( listId : string , itemId : number , webUrl ?: string ) : Promise < any [ ] > {
66
+ /**
67
+ * Get list item attachments
68
+ *
69
+ * @param listId
70
+ * @param itemId
71
+ * @param webUrl
72
+ */
73
+ public async getListItemAttachments ( listId : string , itemId : number , webUrl ?: string ) : Promise < any [ ] > {
74
74
try {
75
75
const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
76
- const apiUrl = `${ webAbsoluteUrl } /_api/web/lists('${ listId } ')/items( ${ itemId } )/AttachmentFiles ` ;
76
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/items(@itemId)/AttachmentFiles?@listId=guid '${ encodeURIComponent ( listId ) } '&@itemId= ${ encodeURIComponent ( String ( itemId ) ) } ` ;
77
77
const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
78
78
if ( data . ok ) {
79
79
const results = await data . json ( ) ;
80
- if ( results && results . value && results . value . length > 0 ) {
80
+ if ( results && results . value ) {
81
81
return results . value ;
82
82
}
83
83
}
84
+
84
85
return null ;
85
86
} catch ( error ) {
86
87
console . dir ( error ) ;
@@ -102,7 +103,7 @@ export default class SPService implements ISPService {
102
103
headers : { "X-HTTP-Method" : 'DELETE' , }
103
104
} ;
104
105
const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
105
- const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(' ${ listId } ' )/items(${ itemId } )/AttachmentFiles/getByFileName('${ encodeURIComponent ( fileName ) } ') ` ;
106
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@ listId)/items(@ itemId)/AttachmentFiles/getByFileName(@fileName)?@listId=guid '${ encodeURIComponent ( listId ) } '&@itemId= ${ encodeURIComponent ( String ( itemId ) ) } &@fileName=' ${ encodeURIComponent ( fileName . replace ( / ' / g , "''" ) ) } ' ` ;
106
107
const data = await this . _context . spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , spOpts ) ;
107
108
} catch ( error ) {
108
109
console . dir ( error ) ;
@@ -121,20 +122,20 @@ export default class SPService implements ISPService {
121
122
*/
122
123
public async addAttachment ( listId : string , itemId : number , fileName : string , file : File , webUrl ?: string ) : Promise < void > {
123
124
try {
124
- // remove special characteres in FileName
125
+ // Remove special characters in FileName
125
126
fileName = fileName . replace ( / [ ^ \. \w \s ] / gi, '' ) ;
126
- // Check if Attachment Exists
127
+ // Check if attachment exists
127
128
const fileExists = await this . checkAttachmentExists ( listId , itemId , fileName , webUrl ) ;
128
- // Delete Attachment if exists
129
+ // Delete attachment if it exists
129
130
if ( fileExists ) {
130
131
await this . deleteAttachment ( fileName , listId , itemId , webUrl ) ;
131
132
}
132
- // Add Attachment
133
+ // Add attachment
133
134
const spOpts : ISPHttpClientOptions = {
134
135
body : file
135
136
} ;
136
137
const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
137
- const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(' ${ listId } ' )/items(${ itemId } )/AttachmentFiles/add(FileName='${ encodeURIComponent ( fileName ) } ') ` ;
138
+ 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 , "''" ) ) } ' ` ;
138
139
const data = await this . _context . spHttpClient . post ( apiUrl , SPHttpClient . configurations . v1 , spOpts ) ;
139
140
return ;
140
141
} catch ( error ) {
@@ -152,14 +153,15 @@ export default class SPService implements ISPService {
152
153
*/
153
154
public async getAttachment ( listId : string , itemId : number , fileName : string , webUrl ?: string ) : Promise < any > {
154
155
const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
155
- const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(' ${ listId } ' )/items(${ itemId } )/AttachmentFiles/GetByFileBame('${ fileName } ')) ` ;
156
+ 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 , "''" ) ) } ' ` ;
156
157
const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
157
158
if ( data . ok ) {
158
159
const file = await data . json ( ) ;
159
160
if ( file ) {
160
161
return file ;
161
162
}
162
163
}
164
+
163
165
return null ;
164
166
}
165
167
@@ -173,14 +175,15 @@ export default class SPService implements ISPService {
173
175
*/
174
176
public async checkAttachmentExists ( listId : string , itemId : number , fileName : string , webUrl ?: string ) : Promise < any > {
175
177
try {
176
- const listName = await this . getListName ( listId , webUrl ) ;
178
+ const listServerRelativeUrl = await this . getListServerRelativeUrl ( listId , webUrl ) ;
177
179
const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
178
- const apiUrl = `${ webAbsoluteUrl } /_api/web/getfilebyserverrelativeurl('/lists/${ listName } /Attachments/${ itemId } /${ fileName } ')` ;
180
+ const fileServerRelativeUrl = `${ listServerRelativeUrl } /Attachments/${ itemId } /${ fileName } ` ;
181
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/getfilebyserverrelativeurl(@url)/Exists?@url='${ encodeURIComponent ( fileServerRelativeUrl . replace ( / ' / g, "''" ) ) } '` ;
179
182
const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
180
183
if ( data . ok ) {
181
184
const results = await data . json ( ) ;
182
- if ( results && results . Exists ) {
183
- return results . Exists ;
185
+ if ( results ) {
186
+ return results . value ;
184
187
}
185
188
}
186
189
@@ -198,14 +201,35 @@ export default class SPService implements ISPService {
198
201
*/
199
202
public async getListName ( listId : string , webUrl ?: string ) : Promise < string > {
200
203
const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
201
- const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(' ${ listId } ')?$select= RootFolder/Name&$expand=RootFolder) ` ;
204
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@ listId)/ RootFolder/Name?@listId=guid' ${ encodeURIComponent ( listId ) } ' ` ;
202
205
const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
203
206
if ( data . ok ) {
204
207
const results = await data . json ( ) ;
205
208
if ( results ) {
206
- return results . RootFolder . Name ;
209
+ return results . value ;
207
210
}
208
211
}
212
+
213
+ return ;
214
+ }
215
+
216
+ /**
217
+ * Get the list server relative url
218
+ *
219
+ * @param listId
220
+ * @param webUrl
221
+ */
222
+ public async getListServerRelativeUrl ( listId : string , webUrl ?: string ) : Promise < string > {
223
+ const webAbsoluteUrl = ! webUrl ? this . _context . pageContext . web . absoluteUrl : webUrl ;
224
+ const apiUrl = `${ webAbsoluteUrl } /_api/web/lists(@listId)/RootFolder/ServerRelativeUrl?@listId=guid'${ encodeURIComponent ( listId ) } '` ;
225
+ const data = await this . _context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
226
+ if ( data . ok ) {
227
+ const results = await data . json ( ) ;
228
+ if ( results ) {
229
+ return results . value ;
230
+ }
231
+ }
232
+
209
233
return ;
210
234
}
211
235
}
0 commit comments