I have created below function in separate library to create list and its 3 fields in the react project, however, its working with normal character SharePoint list but not with special characters list e.g Employee`~!@#$%^&*()_+=-,.<>?;:"[]{}|. System created special character SharePoint list but don't added three fields and throwing error:
isHttpRequestError: true
response:
Response {type: "default", url: "", redirected: false, status: 400, ok: false, …}
status: 400
statusText: "Bad Request"
message: "Error making HttpClient request in queryable [400] Bad Request ::>
{"odata.error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
"message":{"lang":"en-US","value":"The expression \"web/lists/getByTitle('~!@\" is not valid."}}}"
public async CreateList(listTitle: string, siteURL: string, listDescription: string, fields: any[], isHidden: boolean = true) {
return new Promise<any>(async (resolve, reject) => {
try {
let _web = new Web(encodeURI(siteURL));
// let _listName = this.utilityService.DecodeString(this.utilityService.decodeSpecialCharacters(listTitle));
var createListAddResult: ListAddResult = await _web.lists.add(listTitle, listDescription, 100, false, { 'Hidden': false });
this.getRollUpField(_web,listTitle);
if (createListAddResult.list != null) {
let listfieldsaddBatch = _web.createBatch();
//add fields to list
fields.map(field => {
if (field.FieldTypeKind == FieldTypes.Text) {
createListAddResult.list.fields.inBatch(listfieldsaddBatch).addText(field.title, 255);
}
if (field.FieldTypeKind == FieldTypes.Note) {
createListAddResult.list.fields.inBatch(listfieldsaddBatch).addMultilineText(field.title, 100, false, false, false, false);
}
if (field.FieldTypeKind == FieldTypes.Boolean) {
createListAddResult.list.fields.inBatch(listfieldsaddBatch).addBoolean(field.title);
}
});
await listfieldsaddBatch.execute();
let listfieldsinViewBatch = _web.createBatch();
fields.map(field => {
createListAddResult.list.defaultView.fields.inBatch(listfieldsinViewBatch).add(field.title)
});
await listfieldsinViewBatch.execute();
}
resolve(createListAddResult)
} catch (ex) {
console.log(ex);
reject(false);
}
});
}