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 want programmatically update (change) a read-only field (the "Completed" column) of a listitem in a survey, but on everthing I do, I get the message that it's not possible to update a ready only field.

This is my code on the moment:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using (SPSite site = new SPSite(SPContext.Current.Site.ID))
  {
    using (SPWeb web = site.OpenWeb())
    {
      site.AllowUnsafeUpdates = true;
      web.AllowUnsafeUpdates = true;
      SPList list = web.Lists[listTitle];
      SPListItem item = list.Items[itemID];

      item["Completed"] = "Yes";
      item.Update();
      web.Update();
      site.AllowUnsafeUpdates = false;
      web.AllowUnsafeUpdates = false;
    }
  }
});
share|improve this question
2  
Just wondering... are you trying to programmatically complete a task item (with some workflow attached)? If that's the case, I fear that the above method won't work. Leave a comment if you need some further reference. –  SPArchaeologist Jan 26 '12 at 8:30
add comment

5 Answers 5

Can't you turn off ReadOnly while you're updating the field, and then set it back? Something along the lines of this (haven't tested it).

SPList list = web.Lists[listTitle];
var completedField = list.Fields["Completed"];
completedField.ReadOnlyField = false;
completedField.Update();

SPListItem item = list.Items[itemID];

item["Completed"] = "Yes";
item.Update();

completedField.ReadOnlyField = true;
completedField.Update();

web.Update();

PS: If the field has been added to the content type, you might have to update the content type as well. Reference

share|improve this answer
    
Like omlin says: "the time while the field stays in non-ReadOnly mode is so little (actually, it'll be just a few milliseconds) that probability of such concurrent access is dead small." –  tarjeieo Jan 30 '12 at 19:34
    
@tarjeieo yep, and even if this happens, user will just get this field in the edit form. And right after this the field will be switched back into ReadOnly mode. Hence, user will not be able to save its value anyway (ok, unless another ReadOnly flag switch will happen exactly when the user presses Save). So ridiculous, really :( –  Andrey Markeev Jan 30 '12 at 20:37
    
amagonna vote you up again ;) –  tarjeieo Jan 31 '12 at 7:58
add comment

You should leverage SPField.ReadOnlyField property to achieve the desired behavior.

Before updating item, set ReadOnlyField for the "Completed" field to false and update the field (SPField.Update). Then, you will be able to update item. Finally, change ReadOnlyField back to false and update the field again.

share|improve this answer
    
I reckon the time while the field stays in non-ReadOnly mode is so little (actually, it'll be just a few milliseconds) that probability of such concurrent access is dead small. –  Andrey Markeev Jan 30 '12 at 17:08
add comment

If I remember correctly, you can run as administrator and update any list item field. But you should ask the business question what reason do you have to update this field? And is there another proper way to do it, like through a workflow. This field is used for workflow task tracking.

share|improve this answer
add comment

The field you are trying to change is a computed one. You can't change its value, no matter what the settings for the field are. You'll have to reverse engineer it in order to find out how it computes its values, then change the fields it is using in its formula.

By the way, the raw values in the "Completed" field of a survey are 0 for not completed and 1 for completed.

So I've spent a few hours trying to come up with a way to set a survey completion status programmatically. All the Completed field does is reading the value from the "Level" field, and change it from a number to a string. However, there is no way to change the value of "Level" without delving into SQL. Try changing it via the object model and you get an error without any meaningful message. Check the correlating ID in the logs and it says you've got invalid SQL. Wow.

share|improve this answer
add comment

May be this is due to incompatibility of provided property. It is a check box type property and the assigned value is a string. If you could try to assign a bool value to this "Completed" field instead of a string value it might get work.

share|improve this answer
add comment

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.