Forum Discussion

Alistair Young's avatar
Alistair Young
Copper Contributor
Jan 06, 2021

Setting Today's date as default for SP.FieldDateTime

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.

     

    • Alistair Young's avatar
      Alistair Young
      Copper Contributor

      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

      • ganeshsanap's avatar
        ganeshsanap
        MVP

        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:

         

        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.

         

Resources