有时候,我们有需要写一个小工具批量下载发布到不同的SharePoint Form Library中的InfoPath template.
 
假设InfoPath template的URL为:
 
那么写出以下代码是顺理成章的:
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPathWebClient wc = new WebClient();
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPath
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPathwc.Credentials = System.Net.CredentialCache.DefaultCredentials;
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPath
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPathwc.DownloadFile("http://Servername/FormLib/template.xsn", "c:\\testwc.xsn");
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPath
 
但是实际上,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从SharePoint Form Library 中下载InfoPath template_InfoPathWebClient wc = new WebClient();
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPath
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPathwc.Credentials = System.Net.CredentialCache.DefaultCredentials;
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPath
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPathwc.DownloadFile("http://Servername/FormLib/template.xsn?noredirect=true", "c:\\testwc.xsn");
使用WebClient从SharePoint Form Library 中下载InfoPath template_InfoPath
注意template的URL后面多了一个参数noredirect=true. 这个参数将防止Form Serivce对InfoPath form 的转换。此时,WebClient.DownloadFile就能下载完整的InfoPath template文件了。