【发布时间】:2017-08-16 04:47:22
【问题描述】:
我需要将大量附件从 SharePoint 中的多个列表移动到 SQL 中的 BLOB 表中。我找到了允许我将附件下载到本地驱动器的代码,但我无法修改它以将其作为二进制数据直接加载到 SharePoint 中。
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)
{
}
}
}
最后的文件流只是为了测试之前的代码,它成功地检索到了附件。现在我需要采取下一步并将其加载到某种缓冲区中以推送到 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 表 blob 列的最后一步? 非常感谢。
编辑:我应该省略 fstream。那只是为了测试目的。我正在尝试将附件直接从 SharePoint 推送到 SQL,而无需将其存储在本地的中间步骤。
【问题讨论】:
标签: c# sql-server sharepoint ssis sharepoint-2013