Setting Today's date as default for SP.FieldDateTime

%3CLINGO-SUB%20id%3D%22lingo-sub-2035348%22%20slang%3D%22en-US%22%3ESetting%20Today's%20date%20as%20default%20for%20SP.FieldDateTime%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2035348%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20I%20create%20a%20list%20column%20manually%2C%20I%20can%20set%20the%20default%20to%20%22Today's%20date%22%20without%20a%20date%2C%20so%20the%20field%20is%20populated%20when%20the%20item%20is%20created.%20If%20I%20try%20the%20same%20with%20the%20REST%20API%20it%20keeps%20adding%20the%20date%2Ftime%20of%20the%20column%20creation%20and%20setting%20the%20default%20to%20the%20unnamed%20radio%20button.%20Is%20there%20a%20way%20to%20set%20%22Today's%20date%22%20as%20the%20default%20without%20it%20creating%20a%20value%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20following%20sets%20the%20default%20to%20the%20unnamed%20radio%20button%20below%20%22Today's%20date%22%20and%20sets%20the%20value%20to%20the%20date%2Ftime%20the%20column%20was%20created%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EfieldJSON%5B%22DefaultValue%22%5D%20%3D%20%22Today's%20date%22%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20%22Today's%20date%22%20there%20shouldn't%20be%20a%20value%20as%20per%20the%20user%20interface%20way%20of%20setting%20the%20default%20value.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2035348%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2036061%22%20slang%3D%22en-US%22%3ERe%3A%20Setting%20Today's%20date%20as%20default%20for%20SP.FieldDateTime%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2036061%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F157988%22%20target%3D%22_blank%22%3E%40Alistair%20Young%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20using%20below%20code%20to%20create%20a%20date%20column%20with%20default%20value%20of%20today's%20date%20using%20SharePoint%20REST%20API%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Evar%20fieldEndpoint%20%3D%20_spPageContextInfo.webAbsoluteUrl%20%2B%20%22%2F_api%2Fweb%2Flists%2FgetByTitle('ListName')%2Ffields%22%3B%0A%0A%24.ajax(%7B%0A%20url%3A%20fieldEndpoint%2C%0A%20type%3A%20%22POST%22%2C%0A%20data%3A%20JSON.stringify(%7B%0A%20%20'__metadata'%3A%20%7B%20'type'%3A%20'SP.FieldDateTime'%20%7D%2C%0A%20%20'FieldTypeKind'%3A%204%2C%0A%20%20'Title'%3A%20'HandoffDate'%2C%0A%20%20'SchemaXml'%3A%20'%3CFIELD%20type%3D%22DateTime%22%20displayname%3D%22Handoff%20Date%22%20name%3D%22HandoffDate%22%20required%3D%22FALSE%22%20id%3D%22%7Bd6b835d3-820f-4926-85bd-7711cc31c4c8%7D%22%20format%3D%22DateTime%22%3E%3CDEFAULT%3E%5Btoday%5D%3C%2FDEFAULT%3E%3C%2FFIELD%3E'%0A%20%7D)%2C%0A%20headers%3A%20%7B%0A%20%20%22X-RequestDigest%22%3A%20%24(%22%23__REQUESTDIGEST%22).val()%2C%0A%20%20%22accept%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%2C%0A%20%20%22content-type%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%0A%20%7D%2C%0A%20success%3A%20function%20(data)%20%7B%0A%20%20console.log(%22Field%20created%20successfully%22)%3B%0A%20%7D%2C%0A%20error%3A%20function%20(error)%20%7B%0A%20%20console.log(error)%3B%0A%20%7D%0A%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EMicrosoft%20documentations%3C%2FSTRONG%3E%3A%26nbsp%3B%3CA%20title%3D%22Fields%20REST%20API%20reference%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fprevious-versions%2Foffice%2Fdeveloper%2Fsharepoint-rest-reference%2Fdn600182(v%3Doffice.15)%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EFields%20REST%20API%20reference%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CHR%20%2F%3E%3CP%3EPlease%20click%20%3CSTRONG%3EMark%20as%20Best%20Response%3C%2FSTRONG%3E%20if%20my%20post%20helped%20you%20to%20solve%20your%20issue.%20This%20will%20help%20others%20to%20find%20the%20correct%20solution%20easily.%20It%20also%20closes%20the%20item.%20If%20the%20post%20was%20useful%20in%20other%20ways%2C%20please%20consider%20giving%20it%20%3CSTRONG%3ELike%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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.

4 Replies

@Alistair Young 

 

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 documentationsFields 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

@Alistair Young 

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:

Date column with today's date.PNG

 

New Item List form: With today's date selected by default

New item list form.PNG

 

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.

 

@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.