我正在尝试使用C#中的客户端对象模型更新Sharepoint Discussion板中的字段。当代码到达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();
答案 0 :(得分:0)
我在这里给出答案,以防人们通过Google搜索此SO问题标题中的错误消息来查找此问题。这个答案可能会帮助那些遇到这个奇怪错误信息的人。
当我使用服务器端代码更新日历事件时,我遇到了同样的错误。
我的代码首先添加了一个新的空列表项。此新列表项具有开始日期和结束日期的默认值。几行后,列表项字段逐个更新(类似于操作码),然后调用列表项update
。在我的情况下,我的代码没有更新开始日期并保持默认值。结束日期由我的代码更新,结束日期早于默认开始日期。当列表项update
被触发时,此错误将显示在我的异常日志中。
我更正了此问题并将开始日期调整为之前结束日期,然后致电update
,错误就消失了。
答案 1 :(得分:-1)
请尝试以下方法:
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;}