SharePoint Online Update default value to a field in a list using Rest API
Here we will discuss how we can change or set the default value to a field or column in SharePoint list using Rest API. I am doing this demo in a SharePoint online site. Here I have a list name as: MyChartList and in that list I have a column name as MyDefaultValueColumn. In the MyDefaultValueColumn I have set a default value as Hiii like below:
Then I have added a script editor web part in a web part page and then added the below code inside the script editor web part.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('MyChartList')/fields/getbytitle('MyDefaultValueColumn')";
$.ajax({
url: requestUri,
type: "POST",
data: "{ '__metadata': { 'type':'SP.Field' }, 'DefaultValue': 'Hello Updated Value' }",
headers: {
"Accept":"application/json;odata=verbose",
"X-HTTP-Method":"MERGE",
"content-type":"application/json;odata=verbose",
"X-RequestDigest":$('#__REQUESTDIGEST').val(),
"If-Match": "*",
},
success: onSuccess,
error: onError
});
function onSuccess(data) {
alert('Success');
}
function onError(error) {
alert('Error');
}
});
});
</script>
<input type="button" id="btnClick" value="Update Default Column Value"/>
Here Once user click on the button, then the Default value will be updated with "Hello Updated Value" which we have set inside the code. And now if you will check the default value for the particular column it will look like below:
https://www.enjoysharepoint.com/wp-content/uploads/2018/07/sharepoint-online-update-default-value-using-rest-api.png