Search code examples
c#sharepointsharepoint-2013

Unable to update "Last_x0020_Modified" in a SharePoint document


I would like to update "Last_x0020_Modified" in the SharePoint documentation.

I am unable to update with the error.

"Invalid data is being used to update the list item. The field you are trying to update may be read-only."

Is there a way to update "Last_x0020_Modified"?

    using (var context = new ClientContext("URL"))
    {
        var passWord = new SecureString();
        foreach (char c in "pass".ToCharArray())
        {
            passWord.AppendChar(c);
        }
        context.Credentials = new SharePointOnlineCredentials("account", passWord);
    
        var documents = context.Web.Lists.GetByTitle("Document");
        documents.Fields.GetByInternalNameOrTitle("Last_x0020_Modified").ReadOnlyField = false;
        documents.Update();
        context.ExecuteQuery();
    
        var query = CamlQuery.CreateAllItemsQuery();
        var collListItem = documents.GetItems(query);
    
        context.Load(collListItem,
                     items => items.Include(
                        item => item.Id,
                        item => item.DisplayName,
                        item => item.ContentType,
                        item => item["Modified"],
                        item => item["Last_x0020_Modified"]
                    ));
        context.ExecuteQuery();
    
        foreach (var listItem in collListItem.Where(item => item.ContentType.Name == "folder"))
        {
            listItem["Last_x0020_Modified"] = "2020/09/01T09:00:00";
        }
        context.ExecuteQuery();
    
        documents.Fields.GetByInternalNameOrTitle("Last_x0020_Modified").ReadOnlyField = true;
        documents.Update();
    
        context.ExecuteQuery();
    }

Best Regard.


Solution

  • Per my test, I got the same result as yours on my end. Looks like we cannot change the property "Last_x0020_Modified".

    Besides, I found the field "Last_x0020_Modified" is a lookup field.

    enter image description here