|
1 | 1 | ---
|
2 | 2 | title: Validate web part property values
|
3 | 3 | description: Validate property values in SharePoint Framework client-side web parts by validating the value directly inside a web part's code, or by calling an external API.
|
4 |
| -ms.date: 05/09/2020 |
| 4 | +ms.date: 05/11/2020 |
5 | 5 | ms.prod: sharepoint
|
6 | 6 | localization_priority: Priority
|
7 | 7 | ---
|
@@ -273,35 +273,29 @@ In this step, you validate the provided list name and check if it corresponds to
|
273 | 273 | export default class ListInfoWebPart extends BaseClientSideWebPart<IListInfoWebPartProps> {
|
274 | 274 | // ...
|
275 | 275 |
|
276 |
| - private validateListName(value: string): Promise<string> { |
277 |
| - return new Promise<string>((resolve: (validationErrorMessage: string) => void, reject: (error: any) => void): void => { |
278 |
| - if (value === null || |
279 |
| - value.length === 0) { |
280 |
| - resolve('Provide the list name'); |
281 |
| - return; |
282 |
| - } |
283 |
| -
|
284 |
| - this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/web/lists/getByTitle('${escape(value)}')?$select=Id`, SPHttpClient.configurations.v1) |
285 |
| - .then((response: SPHttpClientResponse): void => { |
286 |
| - if (response.ok) { |
287 |
| - resolve(''); |
288 |
| - return; |
289 |
| - } |
290 |
| - else if (response.status === 404) { |
291 |
| - resolve(`List '${escape(value)}' doesn't exist in the current site`); |
292 |
| - return; |
293 |
| - } |
294 |
| - else { |
295 |
| - resolve(`Error: ${response.statusText}. Please try again`); |
296 |
| - return; |
297 |
| - } |
298 |
| - }) |
299 |
| - .catch((error: any): void => { |
300 |
| - resolve(error); |
301 |
| - }); |
302 |
| - }); |
| 276 | + private async validateListName(value: string): Promise<string> { |
| 277 | + if (value === null || value.length === 0) { |
| 278 | + return "Provide the list name"; |
| 279 | + } |
| 280 | +
|
| 281 | + try { |
| 282 | + let response = await this.context.spHttpClient.get( |
| 283 | + this.context.pageContext.web.absoluteUrl + |
| 284 | + `/_api/web/lists/getByTitle('${escape(value)}')?$select=Id`, |
| 285 | + SPHttpClient.configurations.v1 |
| 286 | + ); |
| 287 | +
|
| 288 | + if (response.ok) { |
| 289 | + return ""; |
| 290 | + } else if (response.status === 404) { |
| 291 | + return `List '${escape(value)}' doesn't exist in the current site`; |
| 292 | + } else { |
| 293 | + return `Error: ${response.statusText}. Please try again`; |
303 | 294 | }
|
| 295 | + } catch (error) { |
| 296 | + return error.message; |
304 | 297 | }
|
| 298 | + } |
305 | 299 | ```
|
306 | 300 |
|
307 | 301 | First, the `validateListName` method checks if a list name has been provided. If not, it resolves the promise with a relevant validation error. If the user has provided a list name, the `validateListName` method uses the `SPHttpClient` to call the SharePoint REST API and check if the list with the specified name exists.
|
|
0 commit comments