"已使用无效数据更新列表项.您尝试更新的字段可能是只读的

本文关键字:更新 字段 只读 无效 数据 列表 | 更新日期: 2023-09-27 18:18:18

我正在尝试使用c#中的客户端对象模型更新Sharepoint讨论板中的字段。当代码到达clientContext.ExecuteQuery()时,它抛出异常:

Invalid data has been used to update the list item. The field you are trying to update may be read only.

下面是部分代码:

foreach (var field in newItemProperties)
{
    if (field.Key.Equals("ContentType"))
        continue;
    if (ctFields == null)
        spItem[field.Key] = field.Value;
    else
    {
        bool foundField = false;
        foreach (var fieldCT in ctFields)
        {
            fieldCT.ReadOnlyField=false;
            if (fieldCT.InternalName == field.Key)
            {
                foundField = true;
                if (isAllDayEvent)
                {
                    if (field.Key == "EventDate")
                    {
                        // For all day event, Add 10 hour in order to prevent the EventDate to be decreased by 1 day
                        var dateVal = (DateTime)field.Value;
                        // spItem[field.Key] = dateVal.Add(new TimeSpan(0, 10, 0, 0, 0));
                    }
                    else if (field.Key == "EndDate")
                    {
                        // For all day event, Subtract 10 hour in order to prevent the EndDate to be increased by 1 day
                        var dt = (DateTime)field.Value;
                        // spItem[field.Key] = dt.Subtract(new TimeSpan(0, 10, 0, 0, 0));
                    }
                    else
                        spItem[field.Key] = field.Value;
                }
                else
                    spItem[field.Key] = field.Value;
                break;
            }
        }
        if (!foundField)
        {
            // Update the item properties values even its not part of CT
            // MM hidden fields are not part of CT fields
            if (isAllDayEvent)
            {
                if (field.Key == "EventDate")
                {
                    // For all day event, Add 10 hour in order to prevent the EventDate to be decreased by 1 day
                    var dateVal = (DateTime)field.Value;
                    //spItem[field.Key] = dateVal.Add(new TimeSpan(0, 10, 0, 0, 0));
                }
                else if (field.Key == "EndDate")
                {
                    // For all day event, Subtract 10 hour in order to prevent the EndDate to be increased by 1 day
                    var dt = (DateTime)field.Value;
                    //spItem[field.Key] = dt.Subtract(new TimeSpan(0, 10, 0, 0, 0));
                }
                else
                    spItem[field.Key] = field.Value;
            }
            else
                spItem[field.Key] = field.Value;
        }
    }
}
spItem.Update();            
clientContext.ExecuteQuery();

我把我的答案放在这里,以防人们通过谷歌搜索这个问题的标题中的错误信息找到这个问题。这个答案可能会帮助其他人遇到这个奇怪的错误信息。

当我用服务器端代码更新日历事件时,我有同样的错误。

我的代码首先添加了一个新的空列表项。这个新的列表项具有开始日期和结束日期的默认值。几行之后,列表项字段一个接一个地更新(类似于op代码),然后调用列表项update。在我的例子中,我的代码没有更新开始日期,并保持默认值。我的代码更新了结束日期,并且结束日期比默认的开始日期早。当列表项update被触发时,这个错误将显示在我的异常日志中。

一旦我纠正了这一点,并调整开始日期总是落在结束日期之前,然后调用update,错误就消失了。

请尝试以下操作:

Microsoft.SharePoint.Client.CamlQuery caml = new Microsoft.SharePoint.Client.CamlQuery();
caml.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>" + itemname+ "</Value></Eq></Where></Query></View>";

caml.FolderServerRelativeUrl = relativepath;

Microsoft.SharePoint.Client.ListItemCollection items = list.GetItems(caml);

clientContext.Load(items);
clientContext.Credentials = new NetworkCredential("username","password","domain");

clientContext.ExecuteQuery();

if (items.Count > 0){item["attribute"]=value;}