2017-06-30 56 views
0

SharepointオンラインlistItemまたはそのURLの添付ファイルを取得しようとしています。これまで私は解決策を見つけたばかりですliketheseSharepointオンラインからの添付ファイルを取得するListItem

しかし、問題は、添付ファイルが必要なフォルダを参照するときに空であることです。 SharepointClientBrowserでは、Folderが空ではないので、非常に混乱します。また、がURL https://scundp.sharepoint.com/sites/list_overview_test/f0/Lists/Testlist2/Attachments/2のフォルダに保存されていない場合は、どこに保存する必要がありますか?私のコードで

私は、任意の添付ファイルがある場合、ブロックにのみ実行されるので、正しくnewListItem["AFiles"]. listItem["Attachments"].ToString() == "True"作品にlistItemのの添付ファイルのURLを記述しようとしています。 しかし、bool hはfalseに設定され、Folderも空であると解釈されます。

私が取り組んでいるSharepointアドインは権限があります:Web - manage、Site Collection - fullControl。サイトコレクション全体で、[クライアントアプリケーションでドキュメントを開く]設定が有効になっています。

    if (listItem["Attachments"].ToString() == "True") 
        { 
         bool h = listItem.AttachmentFiles.AreItemsAvailable; 


         try 
         { 
          Web oWeb = context.Web; 
          Folder folder = oWeb.GetFolderByServerRelativeUrl(siteName + "/Lists/" + listName + "/Attachments/" + listItem["ID"]); 
          context.Load(folder); 
          context.ExecuteQuery(); 
          FileCollection attachments = folder.Files; 
          context.Load(attachments); 
          context.ExecuteQuery(); 
          newListItem["AFiles"] = ""; 

          foreach (Microsoft.SharePoint.Client.File oFile in folder.Files) 
          { 
           newListItem["AFiles"] = newListItem["AFiles"] + " " + oFile.ServerRelativeUrl; 
          } 
         } 
         catch (ServerException ex) 
         { 

         } 
        } 
       } 

答えて

0

SharePointサイトからファイルをダウンロードするためにWebRequestを使用してこのタスクを達成することができます

public void DownloadFile(string serverFilePath, string destPath) 
     { 
      var url = string.Format("{0}/{1}", ServerURL, serverFilePath); 
      createDirectiory(Path.GetDirectoryName(destPath)); // this method creates your directory 
      var request = System.Net.HttpWebRequest.Create(url); 
      request.Credentials = System.Net.CredentialCache.DefaultCredentials; 
      using (var sReader = new StreamReader(request.GetResponse().GetResponseStream())) 
      { 
       using (var sWriter = new StreamWriter(destPath)) 
       { 
        sWriter.Write(sReader.ReadToEnd()); 
       } 
      } 
     } 

あなたがここにClient-object-modelを使用したい場合は、いくつかの良い例です: How to get a file using SharePoint Client Object Model with only an absolute url at hand?

+0

はい、私はCSOMしか使用できません。私はそれを試していただきありがとうございますが、私は完全な道を持っていない場合は動作しますか?つまり、私のコードでは、ファイルの名前までその部分だけを取得します。 – Nikto

関連する問題