Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

5 Answers 5

up vote 0 down vote accepted

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.

share|improve this answer
add comment

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

How about run with eleveated privileges ?

Ian.

share|improve this answer
    
I cannot do that because I am using .net Managed client object model –  RHM Mar 19 '12 at 10:26
    
How about running with elevated privileges ? –  Ian Mar 19 '12 at 11:04
1  
That is also not possible with client object model –  RHM Mar 19 '12 at 11:05
    
How about AllowUnsafeUpdates = true ? –  Ian Mar 19 '12 at 12:08
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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 '12 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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