Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Set a column in document library to read only

  Asked By: Caleb    Date: Mar 10    Category: Sharepoint    Views: 2819

I have a custom column in document library called "Doc ID" which I used
ItemAdded and ItemUpdated to retrieve information and put it in this column.

listItem["Document ID"] =
properties.ListItem.ParentList.ParentWeb.Title.ToString() + "-" +
properties.ListTitle.ToString() + "-" + properties.ListItem.UniqueId.ToString();

I want to make this column a read-only so end-users can not change the value.



6 Answers Found

Answer #1    Answered By: Linsey Bauer     Answered On: Mar 10

I think if you hide this field in the EditForm.aspx, it can be regarded as
read-only? if so, you can just set  spfield.ShowInEditForm = false.

you can also use javascript to hide the control in the edit form, but you are
already in code.

Answer #2    Answered By: Ivy Salinas     Answered On: Mar 10

Since you've already got an event handler the easiest way would be to add an
ItemUpdating event handler to check for changes and then cancel them before they
are saved if made by an end user.

Answer #3    Answered By: Kevin Davis     Answered On: Mar 10

Sorry, but i'm very new with coding so this may sound stupid. How would I code
to check this. I know the basic but not sure how to check if the value in Doc
ID got change?

public override voi ItemUpdating(SPItemEventProperties properties)
properties.Cancel = true;
properties.Errormessage = "Modifying Doc ID is not allowed"

Answer #4    Answered By: Meenakshi Khochar     Answered On: Mar 10

I end up adding the ItemUpdating to my event handler like you said and it
working fine. I didn't know just 1 simple line of code properties.Cancel = true
would do the job.

Answer #5    Answered By: Latrice Henson     Answered On: Mar 10

You also need to add a check to compare the value of the field in the
AfterProperties to the value of the list item to see if it hasn't been changed.
Otherwise you'll cancel every update and no one will be able to modify the
entire record. The code should look something like this:

string before=properties.ListItem[fieldToCheck].ToString();
string after=properties.AfterProperties[fieldToCheck].ToString();
if (before != after)
properties.ErrorMessage = "The " + fieldToCheck + " column  is ReadOnly and
can't be changed!";
properties.Cancel = true;

Answer #6    Answered By: Nidhi Tiwary     Answered On: Mar 10

The following link describes a custom  field type (CFT) where you can make a
field as "display only":


When you add this CFT to your list or document  library, you update this field

Didn't find what you were looking for? Find more on Set a column in document library to read only Or get search suggestion and latest updates.