gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:17:18 24 4
gpt4 key购买 nike

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

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

但是这不起作用。

此外,我还想提供文件的标题:所以我想更新标题:

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

但是,一旦文件完成上传..我登录到 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);

最佳答案

将文件添加到文件集合后,您缺少对 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();
}

关于c# - 更新的 listitem 属性未提交对 sharepoint 的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6200654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com