2017-11-30 297 views
0

可能であれば、この仕事を手伝ってください。私はSharepoint Onlineのフォルダにファイルをアップロードできるようにしたいと思います。現時点では、私は図書館にしかアップロードできません。以下は私が現在使っているコードです。Sharepoint上のライブラリ内の特定のフォルダにファイルをアップロード

private void BtnUpload_Click(object sender, EventArgs e) 
{ 
    siteURL += "/" + tbDept.Text; 

    try 
    { 
     using (ClientContext ctx = new ClientContext(siteURL)) 
     { 
      SecureString passWord = new SecureString(); 
      foreach (var c in logUser.getPassword()) 
      { 
       passWord.AppendChar(c); 
      } 
      ctx.Credentials = new SharePointOnlineCredentials(logUser.getUsername(), passWord); 
      Web web = ctx.Web; 

      foreach (string fname in arrAllFiles) //arrAllFiles --> contains multiple files to be uploaded 
      { 
       FileCreationInformation newFile = new FileCreationInformation(); 
       newFile.Overwrite = true; 
       newFile.Content = System.IO.File.ReadAllBytes(fname); 
       newFile.Url = System.IO.Path.GetFileName(fname); 
       List docs = web.Lists.GetByTitle(tbLibrary.Text); //tbLibrary --> Library's Name 
       Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile); 
       ctx.ExecuteQuery(); 
      } 
      MessageBox.Show("Upload Complete!"); 
      siteURL = "https://mycompany.sharepoint.com"; 
     } 
    } 
    catch(Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
     siteURL = "https://mycompany.sharepoint.com"; 
    } 
} 

コードはうまくいきます。私は、ライブラリ内の特定のフォルダにファイルをアップロードするために機能を追加するだけです。

ありがとうございます。

+0

これは、Windowsを使用して行われますアプリケーションやWebアプリケーション..もしそれがWebアプリケーションを介している場合、あなたは 'ServerMapPath'関数を理解する必要があります。例えば、このリンクを見てください。https://stackoverflow.com/question s/1199486/server-mappath-in-c-sharp-classlibrary – MethodMan

+0

これは、Windowsアプリケーションになるつもりです。 –

+0

新しいワークシェアの場所を取得すると、 ''\\ mySharePointPath \ somefolder \"のようになります。ネットワークチームと一緒にフォルダの場所を完全に読み書き可能にする必要があります。 Async CoyToShairPoint関数もあまりにも標準的なC#.NET CopyTo関数を使用して、ちょうどそれを適切に飾るasychタスクを使用して関数を待つ.. – MethodMan

答えて

0

ここに私が書いたサンプルコードがあります。あなたのニーズにそれを微調整することができます。 SaveBinaryDirectメソッドは、コンテキストとフォルダを含む相対パスを取ります。それはうまくいくはずです。

ClientContext ctx = new ClientContext("http://sp13"); 

Folder folder = ctx.Web.GetFolderByServerRelativeUrl("Shared Documents"); 
string file = String.Concat(Environment.CurrentDirectory, @"\Files\SharePointGuidance2010.pdf"); 

List docLib = ctx.Web.Lists.GetByTitle("Documents"); 
ctx.Load(docLib); 
ctx.ExecuteQuery(); 

using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(file))) 
{ 
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/shared documents/SubFolder/spg.pdf", stream, true); 
} 

たり、コードに次のように編集してみてください。

Folder folder = web.GetFolderByServerRelativeUrl("Documents/Subfolder"); 
FileCreationInformation fci = new FileCreationInformation(); 
fci.Content = System.IO.File.ReadAllBytes(@”..\..\myfile.txt”); 
fci.Url = “myfile.txt”; 
fci.Overwrite = true; 
File fileToUpload = folder.Files.Add(fci); 
ctx.Load(fileToUpload); 

ctx.ExecuteQuery(); 
+0

あなたの答えをありがとうが、私はこの時点でファイルポイントが見つかりません(Sharepointの終わりから)例外を取得しています:ctx.ExecuteQuery(); –

+0

私は追加のオプションで自分の答えを編集しました。私は1つ目のオプションを使用しましたが、フォルダではないかもしれませんが、それはうまくいくはずです。オプション2は、フォルダ自体を取得し、ファイルからファイル作成情報オブジェクトを作成してロードすることです。 –

+0

ありがとうございました。あなたは伝説です!更新されたコードはうまく機能しています。 –

0

をUPDATE:次のコード は動作しますが、私はそれを自分自身をテストした:

var filePath = @"C:\PATH_TO_FILE\test.docx"; 

using (var context = new ClientContext("YOUR_SITE_URL") { Credentials = credentials }) 
{ 
    var folder = context.Web.GetFolderByServerRelativeUrl("/sites/YOUR_SITE/LIBRARY_INTERNAL_NAME/FOLDER_NAME"); 

    context.Load(folder); 
    context.ExecuteQuery(); 

    folder.Files.Add(new FileCreationInformation 
    { 
     Overwrite = true, 
     Content = System.IO.File.ReadAllBytes(filePath), 
     Url = Path.GetFileName(filePath) 
    }); 

    context.ExecuteQuery(); 
} 
+0

あなたのコードも同様に動作します。君たちありがとう。 –

関連する問題