[英]Can't upload file to Document Library using CSOM
I am trying to upload multiple file to a Document Library and also update its coloumn values. 我正在尝试将多个文件上传到文档库,并更新其列值。 List(Doc Lib) already exists but I am stuck with uploadinf the file
List(Doc Lib)已经存在,但是我被上载了该文件
I've tried these methods 我已经尝试过这些方法
using lists.asmx 使用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); }
It gives a error ServerException :To add an item to a document library, use SPFileCollection.Add()
on AddAttachment()
出现错误
ServerException :To add an item to a document library, use SPFileCollection.Add()
上使用AddAttachment()
Using 运用
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();
I was able to upload once
using this method, but now I am getting ServerException :To add an item to a document library, use SPFileCollection.Add()
on cc.ExecuteQuery()
我曾经可以使用此方法上传
once
,但是现在我得到了ServerException :To add an item to a document library, use SPFileCollection.Add()
上使用cc.ExecuteQuery()
But if at all this method works, what I want is that I should update the coloumn values related to this file. 但是,如果此方法完全可行,我想要的是更新与该文件相关的列值。 In first method I get item.ID so from there I can update the Coloumn Values
在第一种方法中,我获得了item.ID,因此可以从那里更新Coloumn值
Regarding the second method, the following example demonstrates how to upload a file into Documents library and set it's properties (eg Category
text field) 关于第二种方法,下面的示例演示如何将文件上传到文档库并设置其属性(例如,
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.