c# - Sharepoint 客户端对象模型设置 ModifiedBy 字段

标签 c# .net sharepoint com

我正在尝试使用客户端对象模型更新 Sharepoint 讨论区中的“ModifiedBy”字段。通过更改“Editor”和“Author”字段,我可以更改出现在 ListView 中的“ModifiedBy”。但是,一旦您单击讨论帖子,出现在那里的“ModifiedBy”字段(上面有图片的那个)不会反射(reflect)更改。经过试验,我发现我需要更改以更正此字段的字段称为“MyEditor”。不幸的是,该字段是只读的。

在下面的代码中,我尝试将字段的只读设置更改为 false。当我在第一个 block 底部的 ExecuteQuery() 行之后查看 Visual Studio 调试器中的 MyEditor 字段时,它表明 ReadOnlyField 值实际上已设置为 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();

上面的代码执行没有问题。问题来自下一个 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();

当代码到达第二个 block 底部的 ExecuteQuery() 时,它会抛出带有以下消息的 ServerException:

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

为确保 MyEditor 字段是引发异常的原因,我注释掉了设置它的行并再次运行代码。一切正常。我不明白哪里出了问题,有人可以帮助我吗?

最佳答案

如果有人需要通过名字找到用户,它是这样的:

    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;
    }

返回值可以通过item["Author"]设置

关于c# - Sharepoint 客户端对象模型设置 ModifiedBy 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11460431/

相关文章:

sharepoint - 为 SharePoint 高信任应用程序实现 SSL

使用 Internet Explorer 的 Sharepoint 下拉列表无法正确显示超过 20 个项目

c# - 从 C# 以编程方式调用 CmdLet 时如何捕获 Powershell CmdLet 的对象输出

c# - DbTransaction.Commit 回滚,导致代码中断?

c# - 修复通用 UserControl 的嵌入式资源

c# - 如何快速而可靠地确定是否安装了 Visual C++ 2013 Runtime

c# - 是否可以在不序列化的情况下将对象从 SilverLight 传递到 JavaScript?

c# - 在什么情况下我需要 Entity Framework 中的外键和导航属性

c# - 编译器错误 : "error CS0307: The variable ' int' cannot be used with type arguments"

jquery - SharePoint 2010 jQuery 自动完成