spfx code to check list exists, create list and add a drop down column

spfx code to check list exists, create list and add a drop down column

2018/06/16 Uncategorized 0
const opt: ISPHttpClientOptions = {};
			this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + "/_api/Web/Lists?$filter=title eq 'Icon'", SPHttpClient.configurations.v1, opt).then((response: SPHttpClientResponse) => {
				if (response.status == 200) {
					response.json().then((json: any) => {
						console.log('json.value.length=' + json.value.length);
						if (json.value.length == 0) {
							const opt: ISPHttpClientOptions = {
								body: `{
									'AllowContentTypes': true,
									'BaseTemplate': 100,
									'ContentTypesEnabled': true,
									'Description': 'Storing website icons',
									'Title': 'Icon'
								}`
							};
							this.context.spHttpClient.post(this.context.pageContext.web.absoluteUrl + "/_api/web/lists", SPHttpClient.configurations.v1, opt).then((response: SPHttpClientResponse) => {
								if (response.status == 201) {
									response.json().then((json: any) => {
										const opt: ISPHttpClientOptions = {
											body: `{
												'@odata.type': 'SP.FieldChoice',
												'Title': 'Behaviour',
												'FieldTypeKind': 6,
												'Required': true,
												'EnforceUniqueValues': 'false',
												'StaticName': 'Behaviour',
												Choices: ['Choice1', 'Choice2', 'Choice3']
											}`
										};
										this.context.spHttpClient.post(this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('Icon')/Fields", SPHttpClient.configurations.v1, opt).then((response: SPHttpClientResponse) => {
											if (response.status == 201) {
												alert('List `Icon` created successfully');
											} else {
												alert('Unable to creat columns');
											}
										});
									});
								} else {
									alert('Unable to creat list');
								}
							});
						}
					});
				} else {
					alert('Error');
				}

read count : 2

Leave a Reply

Your email address will not be published. Required fields are marked *