使用WebClient從SharePoint Form Library 中下載InfoPath template

 
有時候,我們有需要寫一個小工具批量下載發佈到不同的SharePoint Form Library中的InfoPath template.
 
假設InfoPath template的URL爲:
 
那麼寫出以下代碼是順理成章的:
WebClient wc = new WebClient();

wc.Credentials = System.Net.CredentialCache.DefaultCredentials;

wc.DownloadFile("http://Servername/FormLib/template.xsn", "c:\\testwc.xsn");

 
但是實際上,testwc.xsn的大小永遠的是14KB,而且顯然不是一個正確的xsn文件。實際上testwc.xsn是一個HTML文件,裏面只有如下內容:
There has been an error while loading the form.A required resource could not be downloaded. To try to resume the download, refresh the page.
 
很顯然下載沒有成功。原因其實是Form Library接到request後,首先會嘗試調用Form Service 將InfoPath form 轉換成HTML頁面。所以,只要防止Form Service對InfoPath form 的轉換即可。使用如下代碼:
WebClient wc = new WebClient();

wc.Credentials = System.Net.CredentialCache.DefaultCredentials;

wc.DownloadFile("http://Servername/FormLib/template.xsn?noredirect=true", "c:\\testwc.xsn");

注意template的URL後面多了一個參數noredirect=true. 這個參數將防止Form Serivce對InfoPath form 的轉換。此時,WebClient.DownloadFile就能下載完整的InfoPath template文件了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章