# Read-only column in a SharePoint list

I want to create a read-only column in my "Request Approval" Nintex workflow task that I sent to the user. I did the following:

1. Created a column in my list. Field name: “Guideline”
2. In the default view this column is not displayed
3. Created content types that contains the Guideline column.
4. Content type name: “Workflow task”
5. This is the default content type
6. Now when the user is approving an item, he sees the “guideline” field

However the field is in edit mode and the user can add information to this field. I want to prevent from the user to edit this field!

What should I do?

• Does the content in the Guideline field change for every item? If not can you not just make it a static text within the workflow task notification? – Karthik Murugesan May 15 '11 at 10:52

To achieve true column level security in SharePoint, I work with two lists and create the column as a lookup. This way, I can set specific permissions on the lookup source (e.g. read only or even access denied).

It requires more effort than a single list, but as far as I know this is the only way without server-side code. But in case of only a single list,means if we require only one list and can't add any other list then we face a problem.

• Note: the last sentence was added by a moderator and I have no idea what it means. – Christophe Mar 6 '15 at 16:53
• I added an edit to remove the nonsense sentence, but apparently it needs to be "peer reviewed". XD – BrainSlugs83 May 18 at 0:31

You could use an item event receiver that prevents the value from being changed. Something like this:

public class EventReceiver1 : SPItemEventReceiver
{
public override void ItemUpdating(SPItemEventProperties properties)
{
if (properties.ListItem["Guideline"] != properties.AfterProperties["Guideline"])
{
properties.Cancel = true;
properties.ErrorMessage = "Guideline is a read-only field";
}
}
}

• Its a good idea to use this technique in combination with the UI based approaches as this event receiver will cancel the edit after the user has pressed save and may cause other important edits to be lost. This is then used as a failsafe to prevent any edits that come via direct object model calls or web service calls. – Steve P May 16 '11 at 6:06
• Alternatively, simply discard the change and force AfterProperties to be equal to BeforeProperties. – Kit Menke May 16 '11 at 16:14
• @Kit I didn't know that worked! Its not explicitly documented but other members of the community have used it. msdn.microsoft.com/en-us/library/… – Steve P May 18 '11 at 5:36
• So... how would you set the property in the first place? -- Seems like this field would just always be null with no way to change that. – BrainSlugs83 May 18 at 0:32

I completely agree with @Marc D Anderson and @Matt Weimer. You can use JavaScript to help with the UI presentation and an event receiver to enforce the values server-side.

As for the JavaScript portion, I suggest taking a look at SPUtility.js (full disclosure, I maintain this open source library). It allows you to do the following:

<script type="text/javascript">
function InitializeMyForm()
{
// more code here...
}
</script>


This could go in a Content Editor Web Part on your form (see the installation page for more options).

look at this tools it's helpful for making columns readonly or hidden http://splistsecurity.codeplex.com/

• Hi and welcome! When you promote your own blog or similar you should always state this in your post! Please add this disclaimer. Also please read our FAQ regarding self promotion. – Anders Rask Nov 29 '12 at 9:58

You can customize the editform.aspx using SharePoint Designer. Take a look at the article below to see how to do this.

http://office.microsoft.com/en-us/sharepoint-designer-help/create-a-custom-list-form-HA010119111.aspx

"In step 6 - Under Type of form to create, indicate whether you want to create a New item form, an Edit item form, or a Display item form."

You will choose "Edit item form". Once you create this form go through the source code and locate the HTML snippet that displays "Guideline" field. You can comment this section to prevent users from editing. Or change the edit mode to "Display" to show the users read only content.

Final thoughts - If the content in the Guideline field doesn't change for every item, just make it Guideline a static text within the workflow task notification. Don't create it as a field. That way users will see a Guideline text as part of the approval notification.

The bottom line is that there is no such thing as a "read-only column" in SharePoint. You can mask it, hide it, etc., but there may still be a place (usually the datasheet view is a culprit) where a user can enter data into it.

Customizing the forms is the best approach for reliability over script. (Anyone who knows me knows I love working with script!) If you protect the column using script and the script doesn't run for any reason, then the column won't be protected. (This might just be due to your own bug in the script.)

You can customize forms in SharePoint Designer or in managed code.

If you are describing the column as a site column in a feature, you could add the ReadOnly attribute (set to TRUE) along with the following attributes:

PITarget="" PrimaryPITarget="" PIAttribute="" PrimaryPIAttribute="" Node=""


In order to make the site column ReadOnly and only updatable via code. I haven't tested this to see if one can override it with the DataSheet view; but I would have thought not.

• It works! I'v been searching for how to make field ReadOnly for users in WSS3 but still allow updating field value programmatically in code. Thank you for solution. And the DataSheet view says the field is read only - you cannot edit it as expected. Note that SharePoint Foundation has FieldRef attribute ReadOnlyClient. Another note: If we set ShowInDisplayForm, then the field appears in display form, but if we set ShowInEditForm, the field won't appear there (also expected behaviour). – Janis Veinbergs Mar 8 '12 at 15:22

I've explored another approach to this which uses the object model to set the ReadOnlyField property (see: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfield.readonlyfield.aspx).

SPList list = web.Lists["Your list name"];
SPField guideline = list.Fields["Guideline"];