0

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

5 Answers 5

0

Could you please try to move the Update() call outside of the foreach loop?

Keep in mind the Managed Client Object Model does nothing until the Execute command is run, as such it makes sense to only update a given item once.

I have not tested this but i would imagine it is locking down the item for the duration of the update and thus returning a read-only error when a second update is run.

0

Sounds like permissions. You could try using a newitem.SystemUpdate() ?

How about run with eleveated privileges ?

Ian.

4
  • I cannot do that because I am using .net Managed client object model
    – RHM
    Mar 19, 2012 at 10:26
  • How about running with elevated privileges ?
    – Ian
    Mar 19, 2012 at 11:04
  • 1
    That is also not possible with client object model
    – RHM
    Mar 19, 2012 at 11:05
  • How about AllowUnsafeUpdates = true ?
    – Ian
    Mar 19, 2012 at 12:08
0

Is the machine running your client code using the same localization setup as your sharepoint server? It's possible the ToShortDateString() command on the client is generating a short date string in one format, while sharepoint is expecting it in a different format.

0

It seems that one of your fields (spitem["EventDate"] or spitem["EndDate"]) returns date not in correct format, or in format that sharepoint may not accept (for example "01/01/0001", or may be event date greater than end date). Try to check it.

0

Just try List.UpdateOverwriteVersion() rather than List.Update() it may solve your problem.

This link http://omourad.blogspot.in/2009/04/moss-splistitemupdate-vs.html presents you with the use of the each of the option.

While i was working with updation of list values, after using UpdateOverwriteVersion() my problem was solved.

Hope it helps.

1
  • While this post may answer the question, we prefer more details such as "why". Please edit your post with additional details. See answer for general guidelines. Thanks.
    – SPDoctor
    Mar 19, 2012 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.