"Invalid data has been used to update the list item. The field you are trying to update may be read only" - when updating BEGIN - END fields in an event list through Object Model code
When updating the "Begin" or the "End" datetime fields in a SharePoint 2003/2007 event list, the error "Invalid data has been used to update the list item. The field you are trying to update may be read only" is seen at the point where Update() method is called on the list item object.
SharePoint somehow seems to look for both the BEGIN – END parameter when the Update() method is called. If either is not provided, you’ll see the above error.
Values for both "Begin" and "End" should be specified before calling the Update() method of the List item object. If either one of them needs to be updated with a new value, the other also needs to be set to a datetime value to prevent this error.
Set both the value through the code before calling the Update() method on the list item object.
Code that'll error out:
DateTime dtNow = DateTime.Now;
SPSite site = new SPSite("https://sharepoint");
SPWeb web = site.OpenWeb();
SPListItem listItem = web.Lists["test_list"].Items[0];
listItem["Begin"] = dtNow;
listItem.Update(); // this call will error out
To get past this error in this specific scenario, modify the code something like the below:
DateTime dtNow = DateTime.Now;
SPSite site = new SPSite("https://sharepoint");
SPWeb web = site.OpenWeb();
SPListItem listItem = web.Lists["test_list"].Items[0];
listItem["Begin"] = dtNow;
listItem["End"] = dtNow.AddDays(5);
listItem.Update(); // this call will pass through
Comments
Anonymous
October 02, 2007
Sridhar, I am getting this error with Sharepoint 2003, and in my list, I don't have the fields "Begin" and "End"?? I'm a little confused, I define the fields in my Sharepoint list. So, why are you saying update "Begin" and "End"? Does every Sharepoint list have a "Begin" and "End"? Thanks for any help... Regards..Anonymous
October 16, 2007
The comment has been removedAnonymous
October 25, 2007
I'm trying to add a custom list item from aspx page. I'm passing all required data and not touching any readonly field still i'm getting this error.Anonymous
July 23, 2008
The comment has been removedAnonymous
June 11, 2009
i'm getting similar error. i finally figured out that in my list i have two fields which takes LOOK UP VALUES. Is there anyway i can add values to these look up fields alsoAnonymous
December 21, 2009
i'm getting the same error when trying to edit an item through edit item form. i'm using sharepoint dropdown control to lookup data from another list.when i change the value in the dropdown and save, it gives the error. can you please help me here?Anonymous
November 09, 2010
The comment has been removedAnonymous
June 23, 2015
I wasted a day, because i thought it was some format problem, with my 'EndDate'... You are a hero! Thanks!