Ashish Patel
Ashish Patel

Reputation: 1306

How do I make a list item read-only

I have a list (assume an issues list) and there is a workflow associated with it. The workflow can adjust the status column of the item to "Closed". Once an item's status is closed, I want to make it read-only so that noone can edit the item or create another workflow instance for that item.

What's the best way to achieve this?

Upvotes: 3

Views: 26705

Answers (5)

tking
tking

Reputation: 1

Idea (un-tested): Add a custom content type to the list (this will allow you control to edit as an admin later). Have workflow switch to the custom CT one the item is "closed". Add a read-only view of the item data to your EditForm.aspx, and in Designer add class "hidden" to your read-only view. Then add custom css in a CDWP on the page for class .hidden display:none. Then use JavaScript to add/remove that class based on the CT, so that the read-only view is only visible for the custom CT, while the edit wp is visible for all others.

Or use a custom InfoPath form to switch to read-only view based on status...

Upvotes: 0

ChiliYago
ChiliYago

Reputation: 12329

Try creating an Event Receiver and handle the deleting event.

Upvotes: 0

Rich Bennema
Rich Bennema

Reputation: 10345

I would attack it one of two ways:

  1. Once the workflow completes and sets the item to Closed, you can break the permissions inheritance from the parent list and set the list item permissions to read only. You could do this within a custom workflow or as either a Workflow or List Item event receiver.
  2. Have an ItemUpdating List Item event receiver that sets properties.Status = SPEventReceiverStatus.CancelWithError if status is Closed.

Personally, I like the first option better as it fits in with SharePoint's security philosophy of not letting the user attempt what they do not have permissions to do. The following code is an example of setting read only permissions on a list item:

item.BreakRoleInheritance(false);
SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment assignment = new SPRoleAssignment(web.AssociatedVisitorGroup);
assignment.RoleDefinitionBindings.Add(role);
item.RoleAssignments.Add(assignment);

Upvotes: 1

Kit Menke
Kit Menke

Reputation: 7056

Have you looked at SPUtility.js? You could get the value of your status field, and then if it is Closed, make the other fields read only (or hide them). This is done using JavaScript that is added in a content editor web part on your EditForm.aspx.

var myChoiceField = SPUtility.GetSPField('Single Choice Field');
if (myChoiceField.GetValue() == 'Closed') {
    SPUtility.GetSPField('Field A').MakeReadOnly();
    SPUtility.GetSPField('Field B').MakeReadOnly();
    SPUtility.GetSPField('Field C').MakeReadOnly();
    // etc..
}

Full disclosure.. this is an open source library I maintain. I've tested it with SharePoint 2007 only, but it may also work with SharePoint 2010 (unfortunately I don't have access to a SharePoint 2010 environment to test).

Upvotes: 2

CBono
CBono

Reputation: 3803

There are item-level permissions that can be set, so you can override list-level permissions on an item-by-item basis. Adding this functionality into your existing workflow probably makes the most sense, but of course there is nothing out-of-the-box that SharePoint provides you.

Luckily, you can extend SharePoint's workflow by creating your own custom actions. The process for doing this in SP2010 is fundamentally the same as 2007; check out this MSDN tutorial for an overview of the process.

There is also a convenient package of custom activities provided in an open-source product called SPDActivities at CodePlex. Specifically of interest to you is the Grant Permission on Item activity. Even if you opt not to use the whole package, you can examine the source code and see about implementing your own version of it (I did something similar for a past project).

Once you have a workflow action for setting an item's permission level, simply add a step to your existing workflow to set Read permission for the affected audience or group.

Upvotes: 4

Related Questions