无法使用 CSOM 将文件上传到文档库
Can't upload file to Document Library using CSOM
我正在尝试将多个文件上传到文档库并更新其列值。
List(Doc Lib) 已经存在,但我坚持上传文件
这些方法我都试过了
使用lists.asmx
NetworkCredential credentials = new NetworkCredential("user", "Pass", "domain");
#region ListWebService
ListService.Lists listService = new ListService.Lists();
listService.Credentials = credentials;
List list = cc.Web.Lists.GetByTitle(library);
listService.Url = cc.Url + "/_vti_bin/lists.asmx";
try
{
FileStream fStream = System.IO.File.OpenRead(filePath);
string fName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
string attach = listService.AddAttachment(library, itemId.ToString(), Path.GetFileName(filePath), contents);
}
#endregion
catch (System.Web.Services.Protocols.SoapException ex)
{
CSVWriter("Message:\n" + ex.Message + "\nDetail:\n" +
ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace, LogReport);
}
它在 AddAttachment()
上给出错误 ServerException :To add an item to a document library, use SPFileCollection.Add()
使用
List lib = cc.Web.Lists.GetByTitle("TestLib");
FileCreationInformation fileInfo = new FileCreationInformation();
fileInfo.Content = System.IO.File.ReadAllBytes("C:\Users\AJohn\Desktop\sample.docx");
fileInfo.Url = "https://serverm/sites/Testing1/TestLib/sample.docx";
fileInfo.Overwrite = true;
Microsoft.SharePoint.Client.File upFile = lib.RootFolder.Files.Add(fileInfo);
cc.Load(upFile);
cc.ExecuteQuery();
我能够使用此方法上传 once
,但现在我在 cc.ExecuteQuery()
上上传 ServerException :To add an item to a document library, use SPFileCollection.Add()
但是如果这个方法真的有效,我想要的是我应该更新与这个文件相关的列值。在第一种方法中,我得到 item.ID 所以我可以从那里更新 Coloumn Values
关于第二种方法,以下示例演示如何将文件上传到文档库并设置其属性(例如Category
文本字段)
using (var ctx = new ClientContext(webUri))
{
var targetList = ctx.Web.Lists.GetByTitle("Documents");
var fileInfo = new FileCreationInformation
{
Url = System.IO.Path.GetFileName(sourcePath),
Content = System.IO.File.ReadAllBytes(sourcePath),
Overwrite = true
};
var file = targetList.RootFolder.Files.Add(fileInfo);
var item = file.ListItemAllFields;
item["Category"] = "User Guide";
item.Update();
ctx.ExecuteQuery();
}
我正在尝试将多个文件上传到文档库并更新其列值。 List(Doc Lib) 已经存在,但我坚持上传文件
这些方法我都试过了
使用lists.asmx
NetworkCredential credentials = new NetworkCredential("user", "Pass", "domain"); #region ListWebService ListService.Lists listService = new ListService.Lists(); listService.Credentials = credentials; List list = cc.Web.Lists.GetByTitle(library); listService.Url = cc.Url + "/_vti_bin/lists.asmx"; try { FileStream fStream = System.IO.File.OpenRead(filePath); string fName = fStream.Name.Substring(3); byte[] contents = new byte[fStream.Length]; fStream.Read(contents, 0, (int)fStream.Length); fStream.Close(); string attach = listService.AddAttachment(library, itemId.ToString(), Path.GetFileName(filePath), contents); } #endregion catch (System.Web.Services.Protocols.SoapException ex) { CSVWriter("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace, LogReport); }
它在 AddAttachment()
ServerException :To add an item to a document library, use SPFileCollection.Add()
使用
List lib = cc.Web.Lists.GetByTitle("TestLib"); FileCreationInformation fileInfo = new FileCreationInformation(); fileInfo.Content = System.IO.File.ReadAllBytes("C:\Users\AJohn\Desktop\sample.docx"); fileInfo.Url = "https://serverm/sites/Testing1/TestLib/sample.docx"; fileInfo.Overwrite = true; Microsoft.SharePoint.Client.File upFile = lib.RootFolder.Files.Add(fileInfo); cc.Load(upFile); cc.ExecuteQuery();
我能够使用此方法上传 once
,但现在我在 cc.ExecuteQuery()
ServerException :To add an item to a document library, use SPFileCollection.Add()
但是如果这个方法真的有效,我想要的是我应该更新与这个文件相关的列值。在第一种方法中,我得到 item.ID 所以我可以从那里更新 Coloumn Values
关于第二种方法,以下示例演示如何将文件上传到文档库并设置其属性(例如Category
文本字段)
using (var ctx = new ClientContext(webUri))
{
var targetList = ctx.Web.Lists.GetByTitle("Documents");
var fileInfo = new FileCreationInformation
{
Url = System.IO.Path.GetFileName(sourcePath),
Content = System.IO.File.ReadAllBytes(sourcePath),
Overwrite = true
};
var file = targetList.RootFolder.Files.Add(fileInfo);
var item = file.ListItemAllFields;
item["Category"] = "User Guide";
item.Update();
ctx.ExecuteQuery();
}