Create DateTime Type Site Column using CSOM

Create Date Time Type Site Column using CSOM

In this post, I described you to Create Date Time Type Site Column using CSOM. When we create date time type field then we need to set some additional properties, like the Date Format property. In the case of  Date Time field, keep in mind format of date time, either will be date only or date time.

[AdSense-A]

Date field

 try
            {
                ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
                string schemaArrivalDateField= "<Field Type='DateTime' Name='DeliveryDate' StaticName='DeliveryBirthDate'  DisplayName='Delivery date' Format='DateOnly' >"+ "<Default>[Today]</Default></Field>";
                Field multilineTextField = rootWeb.Fields.AddFieldAsXml(schemaArrivalDateField, true, AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
            }

Here the default node is optional. But in this code default date will show as Today’s date to the user. As I told you if the default node is empty then no date will be shown.

Date and time field

try
            {
                ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
                string schemaArrivalDateField = "<Field ID='<GUID>' Type='DateTime' Name='DeliveryDateTime' StaticName='DeliveryDateTime' DisplayName='Arrival' Format='DateTime'>"+ "<Default>[Now]</Default></Field>";
                Field multilineTextField = rootWeb.Fields.AddFieldAsXml(schemaArrivalDateField, true, AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
            }

Here the default node is optional. But in this code default date will show as Today’s date and time to the user. As I told you if the default node is empty then no date will be shown.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!