Forum Discussion
Setting Today's date as default for SP.FieldDateTime
Try using below code to create a date column with default value of today's date using SharePoint REST API:
var fieldEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('ListName')/fields";
$.ajax({
url: fieldEndpoint,
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.FieldDateTime' },
'FieldTypeKind': 4,
'Title': 'HandoffDate',
'SchemaXml': '<Field Type="DateTime" DisplayName="Handoff Date" Name="HandoffDate" Required="FALSE" ID="{d6b835d3-820f-4926-85bd-7711cc31c4c8}" Format="DateTime"><Default>[today]</Default></Field>'
}),
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
console.log("Field created successfully");
},
error: function (error) {
console.log(error);
}
});
Microsoft documentations: Fields REST API reference
Please click Mark as Best Response if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
ganeshsanapthanks for that but I've tried them all and none of them allow "Today's date" to be selected. Any default makes it use the unnamed radio button
- ganeshsanapJan 06, 2021MVP
Using code in my above answer I am able to create a date column with default value set to "Today's Date". Check below images:
Column settings:
New Item List form: With today's date selected by default
Give it a try and let me know if it is not working for you.
Please click Mark as Best Response if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
- Alistair YoungJan 06, 2021Copper Contributor
ganeshsanapI'm not using that version of the API though, e.g. __metadata causes it to refuse the request and it uses JSON rather than the XML format. The old XML format used by CSOM works but it seems the new API can't do it. It always defaults to the unnamed radio button.