i've currently got a requirement to create 3 columns in a list. Which i'm doing using the below function. This gets called 3 times and a different field name is passed each time.The guid that gets passed to the function is the id of the list I want to create the columns in.
private async addCol(fieldName, guid): Promise<any> {
const digestCache: IDigestCache = this.context.serviceScope.consume(DigestCache.serviceKey);
digestCache.fetchDigest(this.context.pageContext.web.serverRelativeUrl).then((digest: string): void => {
let obj = {};
if (fieldName == "Full Address") {
obj = "{ '__metadata': { 'type': 'SP.FieldText' }, 'FieldTypeKind': 2,'Title': 'Full Address'}";
}
if (fieldName == "Description") {
obj = "{ '__metadata': { 'type': 'SP.FieldText' }, 'FieldTypeKind': 2,'Title': 'Description' }";
}
if (fieldName == "Location") {
obj = "{ '__metadata': { 'type': 'SP.Field' }, 'FieldTypeKind': 31, 'Title': 'Location' }"
}
console.log("Post data: ");
console.log(obj);
$.post({
url: this.context.pageContext.site.serverRelativeUrl + "/_api/web/lists(guid'" + guid + "')/fields",
type: "POST",
data: obj,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": digest,
},
success: function (data) {
//console.log("done");
//console.log(data)
console.log("added: " + fieldName);
},
error: function (response) {
console.log(response.responseText);
}
});
});
}
When the code runs, i get this error from the console:
{"error":{"code":"-2147023746, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The specified program requires a newer version of Windows. (Exception from HRESULT: 0x8007047E)"}}}
And only one column gets created. Is this something to do with Sharepoint taking its time to update the list?
'odata-version':'3.0'
? Modify it asheaders: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": digest, "odata-version":"3.0" },
and check ?