c# – 更新的listitem属性未提交对sharepoint的更改

我正在将文档上传到sharepoint ..但是我想提供一个自定义名称而不是它继承我上传文件名称.

我的代码基于这个解决方案:http://www.codeproject.com/Articles/103503/How-to-upload-download-a-document-in-SharePoint-20.aspx

但这不起作用.

另外,我还想提供文件标题
所以我想更新标题

uploadFile.ListItemAllFields.FieldValues["Title"] = "my custom title";

但是,一旦文件完成其upload..i登录到sharepoint并注意标题尚未应用.

我如何整合上传文件并应用新名称

非常感谢,

编辑:

using (var clientContext = GetNewContext())
        {
            var uploadLocation = string.Format("{0}{1}/{2}",SiteUrl,Helpers.ListNames.RequestedDocuments,Path.GetFileName(document));

            //Get Document List
            var documentslist = clientContext.Web.Lists.GetByTitle(Helpers.ListNames.RequestedDocuments);

            var fileCreationinformation = new FileCreationinformation
            {
                Content = System.IO.File.ReadAllBytes(document),//Assign to content byte[] i.e. documentStream
                Overwrite = true,//Allow owerwrite of document
                Url = uploadLocation //Upload URL,};

            var uploadFile = documentslist.RootFolder.Files.Add(fileCreationinformation);

            uploadFile.ListItemAllFields.FieldValues["Title"] = title;

            uploadFile.ListItemAllFields.Update();

            clientContext.ExecuteQuery();           
        }
        site.SubmitChanges(ConflictMode.FailOnFirstConflict,true);

解决方法

文件添加到Files集合后,您将缺少对clientContext.Load的调用.有关更多信息,请参阅这些博文:

https://www.c-sharpcorner.com/code/965/programmatically-upload-document-using-client-object-model-in-sharepoint.aspx

https://zimmergren.net/sp-2010-uploading-files-using-the-client-om-in-sharepoint-2010/

代码示例来自上面链接的第一篇博文:

public Boolean UploadDocument(String fileName,String filePath,List MetaDataList)   
{  
    SP.ClientContext ctx = new SP.ClientContext("http: //yoursharepointURL");  
    Web web = ctx.Web;  
    FileCreationinformation newFile = new FileCreationinformation();  
    newFile.Content = System.IO.File.ReadAllBytes(@"C: \TestFile.doc");  
    newFile.Url = " / " + fileName;  
    List docs = web.Lists.GetByTitle("Shared Documents");  
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);  
    context.Load(uploadFile);  
    context.ExecuteQuery();  
    SPClient.ListItem item = uploadFile.ListItemAllFields;  
    //Set the Metadata  
    string docTitle = string.Empty;  
    item["Title"] = docTitle;  
    item.Update();  
    context.ExecuteQuery();  
}

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...