2

Here is the full error:

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

I am basically try to update a Calendar Event in Sharepoint.

First I get the following.

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();

Followed by a loop:

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();

            }

Then last:

clientContext.ExecuteQuery();

If I try to update any other item[x] in the ListItem works fine. When I try to update the "EndDate". I get the following error:

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: 
jmogera
  • 1,689
  • 3
  • 30
  • 65
  • 1
    If the date is not null, check the format of the date you are passing and make sure it is correct. Also, check that the date filed is truly of date type not a string. – NoChance Apr 09 '12 at 21:23
  • Wouldn't DateTime.Parse(string) make it a valid string? Also, I have verified that its not a string, because I tried saving a string value. – jmogera Apr 10 '12 at 13:05

1 Answers1

10

Sharepoint is wired like this. When you update the EndDate by itself, it will give you an error listed above. The solution is to Update the EventDate and EndDate together.

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

jmogera
  • 1,689
  • 3
  • 30
  • 65