When adding item to List, I get error Invalid data has been used to update the list item. The field you are trying to update may be read only
-
17-01-2021 - |
Question
I'm using Client Object Model to update a list on SharePoint.
The list is very simple, it only has 3 columns ([Title], [Author], [Year Published])
[Title] is the standard column added by default,
[Author] is my own column and it is a string field (it's not pointing to User, it's just plain text)
and [Year Published] published is a Number.
all fields are marked as required.
string strUrl = "http://server/sites/training";
using (SPSite oSite = new SPSite(strUrl))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
SPList list = oWeb.Lists["Books"];
SPListItem book = list.AddItem();
book["Title"] = "SQL Server Internals";
book["Author"] = "Mc Fredy";
book["Year Published"] = 2015;
book.Update();
}
}
I get exception on book.Update();
Invalid data has been used to update the list item. The field you are trying to update may be read only.
I looked into everything I found on web but did not find any answer. Please advise.
Solution
Author is an internal field for every SharePoint list which specifies who created the list item. Its display name is Created By. In your case when you created Author column the internal name of this column must have changed to something else like Author0 or any other value. So first go to
List Settings -> In Column section select Author-> From page URL get value of query string parameter Field and use that in your code
So your line of code will change most probabily like this
book["Author0"] = "Mc Fredy"