Jan 06 2021 01:06 AM
If I create a list column manually, I can set the default to "Today's date" without a date, so the field is populated when the item is created. If I try the same with the REST API it keeps adding the date/time of the column creation and setting the default to the unnamed radio button. Is there a way to set "Today's date" as the default without it creating a value?
the following sets the default to the unnamed radio button below "Today's date" and sets the value to the date/time the column was created:
fieldJSON["DefaultValue"] = "Today's date";
For "Today's date" there shouldn't be a value as per the user interface way of setting the default value.
Jan 06 2021 04:49 AM
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.
Jan 06 2021 06:26 AM
@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
Jan 06 2021 07:41 AM - edited Jan 06 2021 07:45 AM
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.
Jan 06 2021 07:58 AM
@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.