There are many scenarios that you might need to have read only field which hold only the date. Using below code snippet I was able to create a date type filed which as read only value for end users.
SPFieldDateTime spDateField = (SPFieldDateTime)sourceList.Fields.CreateNewField(SPFieldType.DateTime.ToString(), "To");
spDateField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
spDateField.ShowInViewForms = true;
// spDateField.DefaultValue = "[today]"; // This assigns Default value today to datetime
spDateField.ReadOnlyField = true;
sourceList.Fields.Add(spDateField);
sourceList.Update();
SPFieldDateTime spDateField = (SPFieldDateTime)sourceList.Fields.CreateNewField(SPFieldType.DateTime.ToString(), "To");
spDateField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
spDateField.ShowInViewForms = true;
// spDateField.DefaultValue = "[today]"; // This assigns Default value today to datetime
spDateField.ReadOnlyField = true;
sourceList.Fields.Add(spDateField);
sourceList.Update();
3 comments:
thanks a lot for the post!
thanks a lot for the post!
Just needed this post today. Thanks for putting this out there.
Post a Comment