简体   繁体   English

无法使用CSOM将文件上传到文档库

[英]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 我已经尝试过这些方法

  1. 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()

  1. 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.

相关问题 使用CSOM打开和读取位于SharePoint文档库中的文本文件 - Open and Read a text file Located in a SharePoint Document Library using CSOM C# CSOM - 检查文档库中是否存在文件 - C# CSOM - Check if File Exists in Document Library 如何使用 CSOM 将文件从一个库复制到另一个库? - How to copy file from one library to another library using CSOM? 库的子文件夹中的 CSOM 更新文件文档元数据。 找不到错误文件 - CSOM Update File Document MetaData in a Subfolder of a library. Getting Error File Not Found 使用C#CSOM检查SharePoint 2013文档库中的DocumentSet是否存在 - Check DocumentSet exist in SharePoint 2013 Document Library using C# CSOM 如何将文件上传到sharepoint文档库 - How to upload file to sharepoint document library 如何将文件上传到Sharepoint中的文档库? - How to upload a file to a document library in sharepoint? 使用客户端对象模型(CSOM)将多个用户添加到SharePoint Online文档库项目“人员或组”列 - Add multiple users to SharePoint Online document library item “Person or Group” column using Client Side Object Model (CSOM) CSOM:如何将文档从一个文档库复制到另一个文档库? - CSOM: How to copy a document from one document library to another documents library? C# - CSOM - 将文件上传到 Sharepoint Online 中的子目录 - C# - CSOM - Upload a file to a subdirectory in Sharepoint Online
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM