Tuesday, May 27, 2008

Creating SPField Read Only + Only Date format

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();

3 comments:

riddy22 said...

thanks a lot for the post!

riddy22 said...

thanks a lot for the post!

Unknown said...

Just needed this post today. Thanks for putting this out there.