Customizing a Sharepoint Client Object Model ModifiedBy Field

I am trying to update the "ModifiedBy" field in the Sharepoint discussion panel using the client object model. By changing the "Editor" and "Author" fields, I can change the "Modified bit" that appears in the list. However, as soon as you click on the discussion post, the “ModifiedBy” field that appears there (the one with the inscription above it) does not reflect the changes. After experimenting, I found that the field that I need to change to fix this is called "MyEditor". Sorry, this field is read-only.

In the code below, I am trying to change the read-only parameters in the false field. When I look at the MyEditor field in the Visual Studio debugger after the ExecuteQuery () line at the bottom of the first block, it shows that the ReadOnlyField value was actually set to false.

        sharepointContext.Load(discussionList);
        sharepointContext.ExecuteQuery();
        var fields = discussionList.Fields;
        sharepointContext.Load(fields);
        sharepointContext.ExecuteQuery();
        var field = fields.GetByInternalNameOrTitle("MyEditor");
        field.ReadOnlyField = false;
        field.Update();
        sharepointContext.Load(field);
        sharepointContext.ExecuteQuery();

The above code is no problem. The problem is the following block:

        //...Code to initialize discussionItem...
        discussionItem["MyEditor"] = 0;
        discussionItem["Editor"] = 0;
        discussionItem["Author"] = 0;
        discussionItem["Body"] = "Testing";
        discussionItem["Title"] = "Hello Worlds";
        discussionItem.Update();
        sharepointContext.Load(discussionItem);
        sharepointContext.ExecuteQuery();

When the code reaches ExecuteQuery () at the bottom of the second block, it throws a ServerException with the following message:

        Invalid data has been used to update the list item. 
        The field you are trying to update may be read only.

To make sure the MyEditor field is the one that throws the exception, I commented out the line where I installed it and ran the code again. Everything worked fine. I don’t understand what’s wrong, can someone help me?

+5
source share
2 answers

- , :

    private static FieldUserValue GetUser(ClientContext clientContext, string userName)
    {
        var userValue = new FieldUserValue();
        var newUser = clientContext.Web.EnsureUser(userName);
        clientContext.Load(newUser);
        clientContext.ExecuteQuery();
        userValue.LookupId = newUser.Id;
        return userValue;
    }

[ "" ]

+3

ModifiedBy CreadtedBy Author and Editor, Author Editor :

        using (var clientContext = new ClientContext(@"http://server"))
        {
            var web = clientContext.Web;
            var lst = web.Lists.GetByTitle("Discus");

            var item = lst.GetItemById(2);
            item["Author"] = 3;
            item["Editor"] = 2;
            item.Update();
            clientContext.ExecuteQuery();                        

            Console.WriteLine("done");
        }
+1

All Articles