2017-08-16 11 views
0

SharePointの複数のリストから、多くの添付ファイルをSQLのBLOBテーブルに移動する必要があります。添付ファイルをローカルドライブにダウンロードできるようなコードが見つかりましたが、バイナリデータをSharePointに直接ロードするように変更することはできませんでした。SPからSQLへの添付ファイルの移行BLOB

using Microsoft.SharePoint.Client; 
using System; 
using System.IO; 
using System.Net; 
using System.Windows.Forms; 
using System.Data; 
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 
using Microsoft.SqlServer.Dts.Runtime.Wrapper; 
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 
    public override void CreateNewOutputRows() 
    { 
    try 
    { 
     int startListID = 1; 

     String siteUrl = "https://mysharepointsite.com/sites/mysite"; 
     String listName = "AttachmentTesting"; 
     NetworkCredential credentials = 
        new NetworkCredential("username", "password", "domain"); 

     using (ClientContext clientContext = new ClientContext(siteUrl)) 
     { 
      clientContext.Credentials = credentials; 

      //Get the Site Collection 
      Site oSite = clientContext.Site; 
      clientContext.Load(oSite); 
      clientContext.ExecuteQuery(); 

      // Get the Web 
      Web oWeb = clientContext.Web; 
      clientContext.Load(oWeb); 
      clientContext.ExecuteQuery(); 

      CamlQuery query = new CamlQuery(); 
      query.ViewXml = @""; 

      List oList = clientContext.Web.Lists.GetByTitle(listName); 
      clientContext.Load(oList); 
      clientContext.ExecuteQuery(); 

      ListItemCollection items = oList.GetItems(query); 
      clientContext.Load(items); 
      clientContext.ExecuteQuery(); 

      foreach (ListItem listItem in items) 
      { 
       if (Int32.Parse(listItem["ID"].ToString()) >= startListID) 
       { 

        Folder folder = 
          oWeb.GetFolderByServerRelativeUrl(oSite.Url + 
          "/Lists/" + listName + "/Attachments/" + 

          listItem["ID"]); 

        clientContext.Load(folder); 

        try 
        { 
         clientContext.ExecuteQuery(); 
        } 
        catch (ServerException ex) 
        { 
        } 

        FileCollection attachments = folder.Files; 
        clientContext.Load(attachments); 
        clientContext.ExecuteQuery(); 

        foreach (Microsoft.SharePoint.Client.File oFile in folder.Files) 
        { 
         FileInfo myFileinfo = new FileInfo(oFile.Name); 
         WebClient client1 = new WebClient(); 
         client1.Credentials = credentials; 

         byte[] fileContents = 
           client1.DownloadData("https://mysharepointsite.com" + 
           oFile.ServerRelativeUrl); 

        } 
       } 
      } 

     } 
    } 
    catch (Exception e) 
    { 
    } 
} 
} 

最後のfilestreamは、前のコードをテストするだけであり、添付ファイルを正常に取得します。今、私は次のステップを踏んで、SQLにプッシュするために何らかのバッファにロードする必要があります。私が使って試してみました:

Output0Buffer.AddRow(); 
Output0Buffer.fileName = oFile.Name; 

を最後にループとOutput0Buffer.SetEndOfRowset();内部が、これはエラーを与える:

[Import Column [2]] Error: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The "Import Column" failed because error code 0xC02090BB occurred, and the error row disposition on "Import Column.Inputs[Import Column Input].Columns[fileName]" specifies failure on error. An error occurred on the specified object of the specified component. There may be error messages posted before this with more information about the failure. 

私は私のSQLテーブルに私のバイト配列FILECONTENTSを押しての最終段階をどのように行うのですかブロブ列? 多くのありがとうございます。

編集:私はfstreamを省略する必要があります。それはテスト目的のためのものでした。私がやろうとしているのは、ローカルに保存する中間的なステップを使わずに、SharePointからSQLに直接添付ファイルをプッシュすることです。

答えて

0

解決済み!

答えはtinamouですが、私はローカルドライブにファイルを置く中間段階を避けようとしていました。

"fileBinary"という名前のスクリプトコンポーネントに別の出力を追加し、AddBlobDataメソッドを使用してそれをバイト配列で埋めて解決しました。

byte[] fileContents = client1.DownloadData("https://mysharepointsite.com/sites/mysite" + 
           oFile.ServerRelativeUrl); 

Output0Buffer.AddRow(); 
Output0Buffer.fileName = oFile.Name; 
Output0Buffer.fileBinary.AddBlobData(fileContents); 

その後、外のすべての私のFORループ(もシギダチョウ科によって提案さ):

Output0Buffer.SetEndOfRowset(); 
0

ローカルドライブからパスを使用する必要があります。

string path = "C:\\Temp\\" + oFile.Name; 
FileStream fStream = new FileStream(path, FileMode.Create); 

fStream.Write(fileContents, 0, fileContents.Length); 
fStream.Close(); 

Output0Buffer.AddRow(); 
Output0Buffer.fileName = path; 

// ... after foreach loop 
Output0Buffer.SetEndOfRowset(); 
関連する問題