0

I use SharePoint 2013 with the following code:

SPListItem listItem;
...
listItem["MyCol"] = " "; // Space character.
Console.WriteLine($"listItem[\"MyCol\"] = {listItem["MyCol"]}"); // Here i see the space character.
listItem.Update();
Console.WriteLine($"listItem[\"MyCol\"] = {listItem["MyCol"]}"); // Here value is became null.

Looks like issue reproduces only for space character.

1
  • What is the data type pf your MyCol?? Commented Dec 31, 2019 at 12:32

3 Answers 3

1

you have to execute your query with clientContext.ExecuteQuery()

ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = "My New Item!";
oListItem.Update();
clientContext.ExecuteQuery(); 
0

According to the below explanation, you need to call ExecuteQuery() to take effect of Update() call.

To set most list item properties, you can use a column indexer to make an assignment, and call the Update() method so that changes will take effect when you call ExecuteQuery() or ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler).

Source: Updating a list item.

Try your code like below:

string siteUrl = "http://MyServer/sites/MySiteCollection";

ClientContext clientContext = new ClientContext(siteUrl);
SPListItem listItem;
...
listItem["MyCol"] = " "; // Space character.
Console.WriteLine($"listItem[\"MyCol\"] = {listItem["MyCol"]}"); // Here i see the space character.
listItem.Update();

clientContext.ExecuteQuery(); //Call this method before writing the value to Console.

Console.WriteLine($"listItem[\"MyCol\"] = {listItem["MyCol"]}"); // Here value is became null.
0

In case, if the current user does not have appropriate rights then also it won't allow you to update. Kindly keep your code enclosed within RunWithElevatedPrivileges which uses System User Account

SPSecurity.RunWithElevatedPrivileges(delegate(){ //your code  });

Your Answer

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

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