"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

Comments (8)
  1. Michael says:

    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..

  2. Sowmya says:

    Is it possible to update "Created By" and "Modified By" field from .net.when i tried updating it is throwing this error "+ InnerException {"Invalid data has been used to update the list item. The field you are trying to update may be read only."} System.Exception {System.Runtime.InteropServices.COMException}

    "

  3. aman tur says:

    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.

  4. Montanus says:

    Just want to share my findind, this was also my problem when I’m attaching a file to an item and I’m getting a Data is invalid error. I tried everything I’ve found including on this blog. And I found out that, that the cause of the error is… that the actual is zero byte. So, to prevent the error mention I have to check

    byte[] b = item.File.openBinary();

    if(b.length==0)

    I hope, I can be help for someone.

    Tony

  5. hd says:

    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 also

  6. Sanjay says:

    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?

  7. Nathan Pillai says:

    This error will happen even if the data type mismatch.

    Eg. User field (Assigned To) cant be updated with string value.

    newItem[newItem.Fields["Assigned To"].InternalName] = web.AllUsers[@"DevInternetManager"];

    Pls check the following site for reference.

    msdn.microsoft.com/…/microsoft.sharepoint.splistitem.aspx

    Thanks

  8. kg says:

    I wasted a day, because i thought it was some format problem, with my 'EndDate'…

    You are a hero!

    Thanks!

Comments are closed.

Skip to main content