Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have 4 date columns in a list that need to be read only after the record is created. Does any one have code to do this?

share|improve this question

3 Answers 3

I've used the following approach. You can define your own column based on DateTime column and define a custom field control. There you can use the control derived from the original control for the DateTime field and set the control mode to Display:

// Custom field class
public class SPFieldDateTimeReadOnly : SPFieldDateTime
    {
        public SPFieldDateTimeReadOnly(SPFieldCollection fields, string fieldName) : base(fields, fieldName)
        {

        }

        public SPFieldDateTimeReadOnly(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName)
        {

        }

        public override BaseFieldControl FieldRenderingControl
        {
            get { return new DateTimeFieldReadonly(); }
        }
    }

    // Custom field control class
    public class DateTimeFieldReadonly : DateTimeField
    {
        protected override void CreateChildControls()
        {
            // Here's all the magic
            ControlMode = SPControlMode.Display;

            base.CreateChildControls();
        }
    }
share|improve this answer

You can add event receiver: OnItemUpdating end cancel properties updating.

public class MyEventReceiver : SPItemEventReceiver
    {
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           string myDateColumnName = "date1";
           properties.AfterProperties[myDateColumnName] =
           SPUtility.CreateISO8601DateTimeFromSystemDateTime((DateTime)properties.ListItem[myDateColumnName]);
       base.ItemUpdating(properties);
       }
share|improve this answer

Adding to what BioGeneZ said, you can hide the the field to show up in editform, so users will never see the field to update

$web = Get-SPWeb "http://test"
$list = $web.Lists["listname"]
$fields = $list.Fields.GetFieldByInternalName("nameofcolumn")
$field.ShowInEditForm = $false
$web.Dispose()
share|improve this answer
1  
Just adding to your post... I have used this as a "poor man's" approach to making the field read only, but this is not the way to go if there is a more than casual reason to require the field not be allowed to change. It also would not be able to address a potential issue where you might want to allow the field to be edited by members of certain groups. –  Robert Kaucher Mar 8 '12 at 19:05
    
You are absolutely right, as we are only hiding the field in as opposed to actually making it read-only. –  sssreddy Mar 8 '12 at 20:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.