![](/img/trans.png)
[英]Invalid data has been used to update the list item. The field you are trying to update may be read only
[英]“Invalid data has been used to update the list item. The field you are trying to update may be read only.”
我正在嘗試使用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();
如果有人通過搜索此SO問題標題中的錯誤消息來找到此問題,我將在此處回答。 此答案可能會幫助遇到此奇怪錯誤消息的其他人。
使用服務器端代碼更新日歷事件時,我遇到了相同的錯誤。
我的代碼首先添加了一個新的空列表項。 此新列表項具有開始日期和結束日期的默認值。 幾行之后,列表項字段將被一次更新(類似於操作碼),然后調用列表項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;}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.