"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("http://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("http://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