先決條件
- 下載從CodePlex SharePoint列表源適配器它消失之前。我還沒有找到他們將要託管它的新位置。
爲數據流
- 添加 「SharePoint列表來源」 指向你的 「首頁 - 新聞」 列表中。
- 添加一個「條件拆分」,將檢查是否有任何附件。
- 根據SharePoint列表的自然鍵添加「查找」以從
NewsArticle
表中匹配Id
列。 NewsArticleFK
列將需要此。在這種情況下,我們將使用SharePoint列表的Title
列來匹配SQL表的Title
列。
- 創建以下變量
- 添加一個「腳本組件」實際從SharePoint下拉附件。
- 在「腳本」部分,將步驟4中的變量添加到「ReadOnlyVariables」列表中。
- 在「輸入列」選項卡上,選擇SharePoint列表中的
ID
列和NewsArticle
表中的Id
列。
- 在「輸入和輸出」選項卡上添加一個名爲「附件」的新輸出。我們需要創建一個新的輸出,因爲我們可能有多個附件的新聞文章,這將允許我們從一個輸入行創建多個輸出行。這裏是列需要:
- 的UniqueID(DT_GUID)
- AttachmentFile(DT_IMAGE)
- 文件名(DT_WSTR)
- NewsArticleId(DT_I4)
- 回去的 「腳本」 選項卡然後單擊「編輯腳本...」按鈕。詳情請參閱下面的代碼。
//This is referring to a Service Reference that will need to be created
using SC_be93317446654a518077a828ff747bd1.SharePointListsService;
using System;
using System.IO;
using System.Net;
using System.ServiceModel;
using System.Xml;
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
BasicHttpBinding listsSoapBinding = new BasicHttpBinding();
listsSoapBinding.Name = "ListsSoap";
listsSoapBinding.Security.Mode = BasicHttpSecurityMode.Transport;
listsSoapBinding.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Ntlm;
EndpointAddress listsSoapAddress =
new EndpointAddress(Variables.SharePointListServiceUrl);
ListsSoapClient listsSoapClient =
new ListsSoapClient(listsSoapBinding, listsSoapAddress);
XmlElement attachmentCollection = listsSoapClient.GetAttachmentCollection(
Variables.SharePointListGuid, Row.ID.ToString());
foreach(XmlNode node in attachmentCollection.ChildNodes)
{
using (WebClient client = new WebClient())
{
AttachmentsBuffer.AddRow();
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(
new Uri(Variables.SharePointUrl),
"NTLM",
new NetworkCredential(
Variables.SharePointAccountUsername,
Variables.SharePointAccountPassword,
Variables.SharePointAccountDomain));
client.Credentials = credentialCache;
string attachmentUrl = node.InnerText;
byte[] data = client.DownloadData(attachmentUrl);
AttachmentsBuffer.AttachmentFile.AddBlobData(data);
AttachmentsBuffer.FileName = Path.GetFileName(node.InnerText);
AttachmentsBuffer.NewsArticleId = Row.NewsArticleId;
AttachmentsBuffer.UniqueID = Guid.NewGuid();
}
}
}
這裏有兩點需要注意有關代碼:
- 創建服務引用到SharePoint Lists.asmx。該網址應該與
SharePoint_List_ServiceUrl
變量相同。
- 使用代碼創建服務綁定,因爲SSIS包實際上無法從創建的app.config加載。
- 使用服務引用的實例調用
GetAttachmentCollection
實際從SharePoint中提取附件URL。使用WebClient
從他們的url下載附件。
- 在
AttachmentFile
列上使用AddBlobData
方法來實際設置新列中的附件數據。