C# 更新「;由“创建”;通过ClientContext创建Sharepoint文档库列

C# 更新「;由“创建”;通过ClientContext创建Sharepoint文档库列,c#,sharepoint-2013,C#,Sharepoint 2013,我想通过客户端上下文更新SharePoint中的Created By列,但我的代码不起作用。该列是文档库列 我尝试了一些选择。我的第一个代码块无法更新字段,但我的第二个代码块确实做到了这一点,但它无法像我的第一个代码块那样上传大于150MB的文件 这是我的第一个代码块: using (var ctx = new ClientContext(spSiteUrl)) { ctx.RequestTimeout = 1000000; Web web = ctx.Web; str

我想通过客户端上下文更新SharePoint中的Created By列,但我的代码不起作用。该列是文档库列

我尝试了一些选择。我的第一个代码块无法更新字段,但我的第二个代码块确实做到了这一点,但它无法像我的第一个代码块那样上传大于150MB的文件

这是我的第一个代码块:

using (var ctx = new ClientContext(spSiteUrl))
{
    ctx.RequestTimeout = 1000000;
    Web web = ctx.Web;

    string strUser = "spDomain\\adminUser1";

    var file = ctx.Web.GetFileByServerRelativeUrl(newServerRelPath);
    var item = file.ListItemAllFields;

    //First approach and it doesn't work
    // -> Start
    FieldUserValue fuvUser = new FieldUserValue();
    User oUser = web.EnsureUser(strUser);
    ctx.Load(oUser,
    props=>props.Id,
    props=>props.LoginName);
    item["Author"] = oUser;
    item["Created_x0020_By"] = oUser;
    // -> End

    //Second approach and still it doesn't work
    // -> Start
    List list = web.GetList(spDocLibUrl);
    list.Fields.GetByInternalNameOrTitle("Author").ReadOnlyField = false;
    list.Fields.GetByInternalNameOrTitle("Created_x0020_By").ReadOnlyField = false;
    list.Update();
    item["Author"]=web.EnsureUser(strUser);
    item["Created_x0020_By"] = web.EnsureUser(strUser);
    // -> End

    item.Update();
    ctx.ExecuteQuery();

}
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(spSiteUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            //read file in filestream
            System.IO.FileStream fStream = System.IO.File.OpenRead(filePath);
            byte[] contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();

            web.AllowUnsafeUpdates = true;
            SPFolder rootFolder = web.GetFolder(spFolderUrl);

            string strUser = "spDomain\\adminUser1";
            SPUser userCreatedBy = web.EnsureUser(strUser);

            //This part sets the created by field successfully but fails to add files bigger than 150mb.
            SPFile file = rootFolder.Files.Add(item.Name, contents,userCreatedBy,userCreatedBy,DateTime.Now,DateTime.Now);

            int id = file.Item.ID;
            SPListItem listItem = file.Item;//docsLibrary.GetItemById(id);
            listItem["Created"] = item.CreationTime;
            listItem["Modified"] = item.LastWriteTime;
            listItem.Update();

            web.AllowUnsafeUpdates = false;
        }
    }
});
如何更新“创建人”字段

我在代码中使用的另一种方法实际上可以更新Created By字段,但我这里的问题是,我无法上传大于150MB的文件

这是我的第二个代码块:

using (var ctx = new ClientContext(spSiteUrl))
{
    ctx.RequestTimeout = 1000000;
    Web web = ctx.Web;

    string strUser = "spDomain\\adminUser1";

    var file = ctx.Web.GetFileByServerRelativeUrl(newServerRelPath);
    var item = file.ListItemAllFields;

    //First approach and it doesn't work
    // -> Start
    FieldUserValue fuvUser = new FieldUserValue();
    User oUser = web.EnsureUser(strUser);
    ctx.Load(oUser,
    props=>props.Id,
    props=>props.LoginName);
    item["Author"] = oUser;
    item["Created_x0020_By"] = oUser;
    // -> End

    //Second approach and still it doesn't work
    // -> Start
    List list = web.GetList(spDocLibUrl);
    list.Fields.GetByInternalNameOrTitle("Author").ReadOnlyField = false;
    list.Fields.GetByInternalNameOrTitle("Created_x0020_By").ReadOnlyField = false;
    list.Update();
    item["Author"]=web.EnsureUser(strUser);
    item["Created_x0020_By"] = web.EnsureUser(strUser);
    // -> End

    item.Update();
    ctx.ExecuteQuery();

}
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(spSiteUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            //read file in filestream
            System.IO.FileStream fStream = System.IO.File.OpenRead(filePath);
            byte[] contents = new byte[fStream.Length];
            fStream.Read(contents, 0, (int)fStream.Length);
            fStream.Close();

            web.AllowUnsafeUpdates = true;
            SPFolder rootFolder = web.GetFolder(spFolderUrl);

            string strUser = "spDomain\\adminUser1";
            SPUser userCreatedBy = web.EnsureUser(strUser);

            //This part sets the created by field successfully but fails to add files bigger than 150mb.
            SPFile file = rootFolder.Files.Add(item.Name, contents,userCreatedBy,userCreatedBy,DateTime.Now,DateTime.Now);

            int id = file.Item.ID;
            SPListItem listItem = file.Item;//docsLibrary.GetItemById(id);
            listItem["Created"] = item.CreationTime;
            listItem["Modified"] = item.LastWriteTime;
            listItem.Update();

            web.AllowUnsafeUpdates = false;
        }
    }
});
很明显,我的第二个代码块没有RequestTimeOut设置。也许这就是它无法上传更大文件的原因。我想设置RequestTimeOut,但是如何在代码中添加它,或者是否有其他更好的方法来实现这一点

业务要求:以编程方式将大于150MB(300MB、800MB甚至1GB)的文件上载到SharePoint文档库,并将“创建人”字段设置为本地源文件夹中文件的作者


如果我的问题有任何不清楚的地方,请告诉我,以便我可以更新。

尝试设置MaxReceivedMessageSize:

SPWebService ws = SPWebService.ContentService;
SPClientRequestServiceSettings clientSettings = ws.ClientRequestServiceSettings;
clientSettings.MaxReceivedMessageSize = 10485760;
ws.Update();
Console.WriteLine(clientSettings.MaxReceivedMessageSize);
或使用HTTP DAV:

ClientContext context = new ClientContext("http://spdevinwin");
using (FileStream fs = new FileStream(@"C:\Work\Files\17580_FAST2010_S03_Arch.pptx", FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/documents/17580_FAST2010_S03_Arch from client OM.pptx", fs, true);
}
要通过使用来修改,请执行以下操作:

RunCodeWithNewClientContext((ctx, item) => {
    const string template = "i:0#.f|membership|{0}@<tenant>.onmicrosoft.com";
    var modifiedBy = ctx.Web.EnsureUser(string.Format(template, vm.ModifiedBy));
    var createdBy = ctx.Web.EnsureUser(string.Format(template, vm.CreatedBy));

    ctx.Load(modifiedBy);
    ctx.Load(createdBy);
    ctx.ExecuteQuery();

    item["Editor"] = modifiedBy.Id;
    var modifiedByField = new ListItemFormUpdateValue {
        FieldName = "Modified_x0020_By",
        FieldValue = modifiedBy.Id.ToString()
    };

    item["Author"] = createdBy.Id;
    var createdByField = new ListItemFormUpdateValue {
        FieldName = "Created_x0020_By",
        FieldValue = createdBy.Id.ToString()
    };

    item["Modified"] = vm.ModifiedAt.ToUniversalTime();
    item["Created"] = vm.CreatedAt.ToUniversalTime();

    // it doesn't matter if you add both modifiedByField and createdByField.
    // As long as the list is non-empty all changes appear to carry over.
    var updatedValues = new List<ListItemFormUpdateValue> { modifiedByField, createdByField };
    item.ValidateUpdateListItem(updatedValues, true, "Ignored on explicit checkin/checkout");
    ctx.ExecuteQuery();
});
RunCodeWithNewClientContext((ctx,item)=>{
const string template=“i:0#.f | membership |{0}@.onmicrosoft.com”;
var modifiedBy=ctx.Web.EnsureUser(string.Format(template,vm.modifiedBy));
var createdBy=ctx.Web.EnsureUser(string.Format(template,vm.createdBy));
ctx.荷载(根据修改);
ctx.Load(createdBy);
ctx.ExecuteQuery();
项[“编辑器”]=modifiedBy.Id;
var modifiedByField=新ListItemFormUpdateValue{
FieldName=“修改人”,
FieldValue=modifiedBy.Id.ToString()
};
项目[“作者”]=createdBy.Id;
var createdByField=新的ListItemFormUpdateValue{
FieldName=“创建人”,
FieldValue=createdBy.Id.ToString()
};
item[“Modified”]=vm.ModifiedAt.ToUniversalTime();
item[“Created”]=vm.CreatedAt.ToUniversalTime();
//无论是否同时添加modifiedByField和createdByField。
//只要列表不为空,所有更改都将继续。
var updatedValues=新列表{modifiedByField,createdByField};
item.ValidateUpdateListItem(updatedValues,true,“显式签入/签出时忽略”);
ctx.ExecuteQuery();
});

这看起来很有希望。我可能需要回到我的代码上,尝试一下,也许它会帮助我修复它。让我看看它是否有效。谢谢@BOG Lab。我想后面的代码取自这个博客。对但我们不应该发布链接,因为我复制了这段代码片段。我希望它能解决你的问题。