2012-04-09 16 views
1

以下是完整的錯誤:更新結束日期將導致「無效數據已被用於更新列表項」

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

我基本上嘗試更新SharePoint中的日曆事件。

首先我得到以下內容。

ClientContext clientContext = new ClientContext(deptUrl); 
Web web = clientContext.Web; 
List list = web.Lists.GetByTitle(deptCal); 
clientContext.Load(list); 

CamlQuery query = new CamlQuery(); 
query.ViewXml = "<View><Query><Where><IsNull><FieldRef Name='EndDate' /></IsNull></Where></Query><RowLimit>1</RowLimit></View>"; 

ListItemCollection allEventIds = list.GetItems(query); 

clientContext.Load(allEventIds, 
items => items.Include(
    item => item["EventID"], 
    item => item["EventDate"], 
    item => item["EndDate"] 
)); 

clientContext.ExecuteQuery(); 

隨之而來的是循環:

foreach (var item in allEventIds) 
{ 
    Console.Write("EventId: {0} StartDate: {1}\n", item.FieldValues["EventID"], item.FieldValues["EventDate"]); 

       if (item.FieldValues.ContainsKey("EventDate")) 
       { 
        object objValue = item.FieldValues["EventDate"]; 
        if (objValue != null) 
        { 
         clientContext.Load(item);       
         DateTime endDate = DateTime.Parse(objValue.ToString()); 
         item["EndDate"] = endDate; //Updated this! 

        } 
       } 
       item.Update(); 

      } 

然後最後:

clientContext.ExecuteQuery(); 

如果我嘗試更新任何其他項目[X]在列表項工作正常。當我嘗試更新「EndDate」時。我收到以下錯誤:

Microsoft.SharePoint.Client.ServerException was unhandled 
    Message="Invalid data has been used to update the list item. The field you are trying to update may be read only." 
    Source="Microsoft.SharePoint.Client.Runtime" 
    ServerErrorCode=-2147024809 
    ServerErrorTypeName="System.ArgumentException" 
    ServerStackTrace="" 
    StackTrace: 
     at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) 
     at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() 
     at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb) 
     at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery() 
     at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery() 
     at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() 
     at DisplayOnCalendarUtility.Program.Main(String[] args) in C:\Projects\DisplayOnCalendarUtility\DisplayOnCalendarUtility\Program.cs:line 61 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+1

如果日期不爲空,請檢查您傳遞的日期格式,並確保它是正確的。另外,檢查日期字段是否真的是日期類型而不是字符串。 – NoChance 2012-04-09 21:23:16

+0

不會DateTime.Parse(字符串)使它成爲一個有效的字符串?另外,我已經驗證它不是一個字符串,因爲我嘗試保存一個字符串值。 – jmogera 2012-04-10 13:05:58

回答

7

Sharepoint是這樣連接的。當你自己更新EndDate時,它會給你上面列出的一個錯誤。解決方案是一起更新EventDate和EndDate。

DateTime startDate = DateTime.Parse(objValue.ToString()); 
item["EventDate"] = startDate; 
item["EndDate"] = startDate; //Or Any other date you want to set to. 

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/31e84d74-3ea8-44df-86dc-2dc62381ab3b/#33ad370b-b137-4c2f-bcaa-d6f5c714f4dd

+0

WoW ...完全意想不到的解決方案! – NoChance 2012-04-10 15:28:30

+0

告訴我這件事。瘋! – jmogera 2012-04-10 15:35:01

相關問題