1

複数のファイルをドキュメント ライブラリにアップロードし、その列の値も更新しようとしています。List(Doc Lib) は既に存在しますが、ファイルのアップロードに行き詰まっています

私はこれらの方法を試しました

  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

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 に答える