1

我正在尝试将多个文件上传到文档库并更新其列值。List(Doc Lib) 已经存在,但我坚持使用 uploadinf 文件

我试过这些方法

  1. 使用 list.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);
            }
    

ServerException :To add an item to a document library, use SPFileCollection.Add()它给出了一个错误AddAttachment()

  1. 使用

            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,但现在我得到了ServerException :To add an item to a document library, use SPFileCollection.Add()cc.ExecuteQuery()

但是如果这个方法完全有效,我想要的是我应该更新与这个文件相关的列值。在第一种方法中,我得到了 item.ID,所以我可以从那里更新列值

4

1 回答 1

2

关于第二种方法,以下示例演示如何将文件上传到文档库并设置其属性(例如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();
}  
于 2015-10-15T21:08:56.563 回答