SPFx - Update a calculated column formula
-
13-02-2021 - |
Question
I am trying to use this.context.spHttpClient.post()
to update a calculated column formula in my SPFx WebPart.
I get a status code of 400 with the error message: "The parameter __metadata does not exist in method GetByTitle."
I have created the following function to perform this operation:
private _setCalculatedColumn(ListURL: string, ListTitle: string): void{
const body: ISPHttpClientOptions = {
body: `{'__metadata':{'type':'SP.FieldCalculated'},'Formula':'[my new formula]'}`
};
this.context.spHttpClient.post(
ListURL + `/_api/web/Lists/GetByTitle('`+ ListTitle +`')/Fields/GetByTitle('AutoTaskDueDate')`,
SPHttpClient.configurations.v1,
body)
.then((response: SPHttpClientResponse) => {
console.log(`Status code: ${response.status}`);
console.log(`Status text: ${response.statusText}`);
//response.json() returns a promise so you get access to the json in the resolve callback.
response.json().then((responseJSON: JSON) => {
console.log(responseJSON);
});
});
}
I have found an example using Microsoft Flow but that is the closest I can find to updating a formula via a POST request.
I have changed around the body
string and get a similar error message.
Thanks
No correct solution
OTHER TIPS
Rest api update calculated formula:
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
SetCalculatedColumnFormula();
function SetCalculatedColumnFormula() {
$.ajax
({
// _spPageContextInfo.webAbsoluteUrl - will give absolute URL of the site where you are running the code.
// You can replace this with other site URL where you want to apply the function
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('test_test')/fields/getbytitle('calculated')",
method: "POST",
data: JSON.stringify({
'__metadata': {
// Type that you are modifying.
'type': 'SP.FieldCalculated'
},
// Enter Formula over here
'Formula': '=Title+1'
}),
headers:
{
// IF-MATCH header: Provides a way to verify that the object being changed has not been changed since it was last retrieved.
// "IF-MATCH":"*", will overwrite any modification in the object, since it was last retrieved.
"IF-MATCH": "*",
"X-HTTP-Method": "PATCH",
// Accept header: Specifies the format for response data from the server.
"Accept": "application/json;odata=verbose",
//Content-Type header: Specifies the format of the data that the client is sending to the server
"Content-Type": "application/json;odata=verbose",
// X-RequestDigest header: When you send a POST request, it must include the form digest value in X-RequestDigest header
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log("Success");
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
}
</script>
Source:https://www.codesharepoint.com/rest-api/set-calculated-field-formula-in-sharepoint-using-rest-api
In spfx,you could use pnp js to update calculated field:
I managed to get my function working. This is without using the @pnp library. I read here that you cannot use POST requests for updating fields? So I switched it to PATCH instead and it is now working.
private _setCalculatedColumn(ListURL: string, ListTitle: string): void{
const bodyJSON: string = JSON.stringify({
'__metadata': {
// Type that you are modifying.
'type': 'SP.FieldCalculated'
},
// Enter Formula over here
'Formula': '=Modified'
});
this.context.spHttpClient.fetch(
ListURL + `/_api/web/Lists/GetByTitle('`+ ListTitle +`')/Fields/GetByTitle('AutoTaskDueDate')`,
SPHttpClient.configurations.v1, {
method: "PATCH",
headers: {
'IF-MATCH': '*',
'Content-type': 'application/json;odata=verbose',
"accept": "application/json;odata=verbose",
"odata-version":"3.0",
'X-HTTP-Method': 'PATCH'
},
body: bodyJSON})
.then((response: SPHttpClientResponse) => {
if(response.status === 204){
console.log("Updated formula for " + ListTitle + " successfully!")
}
}
);
}