I am trying to add values to a list item dynamically (using Managed client object model) but its not working. But when I am giving it hard coded values it works fine below is the example:
This one works:
ListItem newitem = list.AddItem(new ListItemCreationInformation());
newitem["Title"] = "This is test event";
newitem["EventDate"] = "2/26/2012";
newitem["EndDate"] = "2/26/2012";
newitem.Update();
clientContext.ExecuteQuery();
This one doesn't works: (where thisfield is a variable from FOREACH loop which iterates the fields in spitem, as this code is written in a workflow Code)
ListItem newitem = list.AddItem(new ListItemCreationInformation());
//-----below code is inside foreach loop------
if (thisfield.StaticName.ToLower() == "title")
newitem["Title"] = Convert.ToString(spitem[thisfield.StaticName]);
if (thisfield.StaticName.ToLower() == "enddate"){
string date1 = Convert.ToDateTime(spitem[thisfield.StaticName]).ToShortDateString();
newitem["EndDate"] = date1;}
if (thisfield.StaticName.ToLower() == "eventdate"){
string date2 = Convert.ToDateTime(spitem[thisfield.StaticName]).ToShortDateString();
newitem["EventDate"] = date2;
}
newitem.Update();
//----end foreach-------
clientContext.ExecuteQuery();
The error which is coming is :
Invalid data has been used to update the list item. The field you are trying to update may be read only.
Let me know what may be the problem here?
Thanks RHM