2

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();

    }
2

Here its how you can update the read only fields of SharePoint.

public static void UpdateValue()
    {
        var secureStringPassword = new SecureString();
        "*******".ToList().ForEach(c => secureStringPassword.AppendChar(c));
        using (ClientContext cc=new ClientContext(siteUrl))
        {
            cc.Credentials = new SharePointOnlineCredentials("asasdd@asdsa.com", secureStringPassword);

            var web = cc.Web;
            var site = cc.Site;

            cc.Load(web);
            cc.Load(site);
            cc.ExecuteQuery();

            List oList = cc.Web.Lists.GetByTitle("test");


            ListItemCollection collListItem = oList.GetItems(CamlQuery.CreateAllItemsQuery());

            cc.Load(collListItem);

            cc.ExecuteQuery();
            oList.Fields.GetByInternalNameOrTitle("Author").ReadOnlyField = false;
            oList.Fields.GetByInternalNameOrTitle("Editor").ReadOnlyField = false;
            oList.Fields.GetByInternalNameOrTitle("Modified").ReadOnlyField = false;
            oList.Fields.GetByInternalNameOrTitle("Created").ReadOnlyField = false;
            oList.Update();
            cc.ExecuteQuery();
            foreach (ListItem oListItem in collListItem)
            {
                oListItem["Author"] = CreatedByValue;
                oListItem["Editor"] = ModifiedByValue;
                oListItem["Modified"] = ModifiedDate;
                oListItem["Created"] = CreatedDate;
                oListItem.Update();
                cc.ExecuteQuery();

            }
            oList.Fields.GetByInternalNameOrTitle("Author").ReadOnlyField = true;
            oList.Fields.GetByInternalNameOrTitle("Editor").ReadOnlyField = true;
            oList.Fields.GetByInternalNameOrTitle("Modified").ReadOnlyField = true;
            oList.Fields.GetByInternalNameOrTitle("Created").ReadOnlyField = true;
            oList.Update();
            cc.ExecuteQuery();
        }
    }
  • Hi Raging River. Thank you for the attempt, but unfortunately it's not working for me with csom, SP2013 on premise (I'm not using on cloud). The key here is the modified_x0020_by that's not updating. When I add in this field to your code, I get the following exception, "Additional information: Column 'Modified_x0020_by' does not exist. It may have been deleted by another user. /ss1/Test Doc Sets" – PHenry May 5 '17 at 14:29
  • 1
    I think "Modified_x0020_By" should be "Editor", if it is an OOTB field. Similarly, "Created_x0020_By" should be "Author". – Nullldata Mar 1 '18 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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