0

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.

FLICKER
  • 6,439
  • 4
  • 45
  • 75
  • 1
    I have a bad feeling that author is built in SPListItem field and you cannot change it. Author is display name? – Yevgeniy.Chernobrivets May 31 '18 at 17:58
  • 1
    Checkout [this] (https://social.msdn.microsoft.com/Forums/office/en-US/75ca6fab-56f3-4bf4-aae0-2d29821778a2/how-to-get-internal-names-of-columns-in-sharepoint-lists?forum=sharepointdevelopmentlegacy) post to find out internal name of your field and to use it instead. – Yevgeniy.Chernobrivets May 31 '18 at 18:00
  • interesting. you are right!!! the field "Created By" has internal name as "Author". Thank you so much, please post your advise as answer so I can give you a credit. – FLICKER May 31 '18 at 18:43
  • BTW it does not look like Client Object Model. It is Server Object Model i believe. :) – Yevgeniy.Chernobrivets May 31 '18 at 18:57
  • I'm new to SharePoint so I'm not sure if it's COM or SOM. I'm using Microsoft.SharePoint.Client.dll assembly. thanks for mentioning – FLICKER May 31 '18 at 18:59
  • I am pretty sure that you are messing up dll names because SPWeb, SPLIstItem, etc., are part of Microsoft.SharePoint.dll not Microsoft.SharePoint.Client.dll. In Microsoft.SharePoint.Client.dll you have ListItem, Web, etc. – Yevgeniy.Chernobrivets May 31 '18 at 19:03
  • I also have added Microsoft.SharePoint.dll to my solution so I think I'm using a mix of them. I'll research to understand the differences. :) – FLICKER May 31 '18 at 19:05
  • You won't be able to use mix of them) To decide which one you need ask yourself if you need your application to run outside of SharePoint server. If answer is yes then you have to go with Client Object Model because Server Object Model can run only on SharePoint server. If answer is no then i would go with Server Object Model because its much easier to use. – Yevgeniy.Chernobrivets May 31 '18 at 19:13

1 Answers1

1

Author is internal Sharepoint field which contains information about who created record (display name Created By) and you cannot change it. Checkout out this post in order to find out internal name of your field and try to use it instead.

Yevgeniy.Chernobrivets
  • 3,194
  • 2
  • 12
  • 14