Does anyone have any suggestions on how to go about updating the Modified_x0020_By field? I'm trying everything but it's just not changing. (Before you ask, yes, I do need to update this field.) I can update the Editor no problem but the modified one refuses to update. Unfortunately the web page displays the Editor field, but the CSOM code uses the Modified_x0020_By field.
I've tried changing the readonly on the field but thats not helping me.
Here is my code to first display the values I want, then to try to change two. Yes, you can see my frustration in var name below.
var url = new Uri("http://sp2013-todd.qalabs.local/ss1/");
var libraryName = @"Test Doc Sets";
var folderPath = "/ss1/Test Doc Sets/kellytest3";
var isFolder = false;
var ctx = new ClientContext(url);
var site = ctx.Web;
var list = site.Lists.GetByTitle(libraryName);
var query = new CamlQuery();
query.ViewXml = "<View Scope=\"RecursiveAll\">" +
"<Query>" +
"<Where>" +
"<And>" +
"<Contains>" +
"<FieldRef Name='FSObjType'/>" +
"<Value Type='Integer'>" + (isFolder ? "1" : "0") + "</Value>" +
"</Contains>" +
"<Eq>" +
"<FieldRef Name='FileDirRef'/>" +
"<Value Type='Text'>" + folderPath + "</Value>" +
"</Eq>" +
"</And>" +
"</Where>" +
"</Query>" +
"</View>";
ListItemCollection collListItem = list.GetItems(query);
ctx.Load(collListItem, xItems => xItems.Include(
x => x["Id"],
x => x["Modified"],
x => x["Modified_x0020_By"],
x => x["Editor"],
x => x.File,
x => x.File.Author,
x => x.File.ModifiedBy
),
x => x.ListItemCollectionPosition);
ctx.ExecuteQuery();
Console.WriteLine($"There are {collListItem.Count} items to process");
int firstFile = 0;
StringBuilder sb = new StringBuilder();
foreach (var item in collListItem)
{
sb.Clear();
int fileId = item.Id;
if (firstFile == 0)
firstFile = fileId;
sb.Append($"Id = {GetStringValue(fileId)},\t");
sb.Append($"FileName = '{GetStringValue(item.File.Name)}',\t");
sb.Append($"File.Author = {item.File.Author.Id}:'${item.File.Author.Email}',\t");
sb.Append($"File.ModifiedBy = {item.File.ModifiedBy.Id}:'${item.File.ModifiedBy.Email}',");
sb.Append(Environment.NewLine + "\t");
foreach (var fv in item.FieldValues)
{
if (fv.Value is FieldUserValue)
{
var fvUser = ((FieldUserValue)fv.Value);
sb.Append($"{fv.Key} = {fvUser.LookupId}:'${fvUser.LookupValue}'");
}
else
{
sb.Append($"{fv.Key} = '{GetStringValue(fv.Value)}',\t");
}
}
Console.WriteLine(sb.ToString() + Environment.NewLine);
}
if (firstFile > 0)
{
ListItem firstItem = list.GetItemById(firstFile);
ctx.Load(firstItem);
ctx.ExecuteQuery();
User ramDownThroatUser = ctx.Web.EnsureUser(@"i:0#.w|qalabs\administrator");
ctx.Load(ramDownThroatUser);
ctx.ExecuteQuery();
firstItem["Editor"] = "1;#" + ramDownThroatUser;
firstItem["Modified_x0020_By"] = firstItem["Editor"];
firstItem.Update();
ctx.ExecuteQuery();
}