| Dynamically changing URL of Data Connection in InfoPath 2010 (by code) |
| Step | Description |
| 1 | Add using System.Text.RegularExpressions; at the top section of your code |
| 2 | Create a function to attribute the new url to the dataconnexion
/// <summary>
/// This function is used for portability
/// The DataConnexion path are modified to target the current SharePoint Site
/// </summary>
private void setDataConnexionPath()
{
string basepath = this.ServerInfo.SharePointSiteUrl.AbsoluteUri.ToString().Replace("/Sites/","");
Regex regexObj;
foreach (DataConnection dc in this.DataConnections)
{
if (dc is SharePointListRWQueryConnection) {
regexObj = new Regex("(.*)/sites/(.*)");
string cpath = ((SharePointListRWQueryConnection)dc).SiteUrl.AbsoluteUri.ToString();
Match m_src = regexObj.Match(cpath);
Match m_target = regexObj.Match(basepath);
((SharePointListRWQueryConnection)dc).SiteUrl = new Uri(this.ServerInfo.SharePointListUrl.AbsoluteUri.Replace("/FormServerTemplates/", ""));
}
if (dc is SharePointListRWSubmitConnection) {
regexObj = new Regex("(.*)/sites/(.*)");
string cpath = ((SharePointListRWSubmitConnection)dc).SiteUrl.AbsoluteUri.ToString();
Match m_src = regexObj.Match(cpath);
Match m_target = regexObj.Match(basepath);
((SharePointListRWSubmitConnection)dc).SiteUrl = new Uri(this.ServerInfo.SharePointListUrl.AbsoluteUri.Replace("/FormServerTemplates/",""));
}
if (dc is WebServiceConnection) {
if (dc.Name.StartsWith("GetUserProfileByName"))
{
//http://clipsapps.cclulaba.net:81/sites/Computa/Request/_vti_bin/userprofileservice.asmx?WSDL
regexObj = new Regex("(.*)/_vti_bin/(.*)");
string cpath = ((WebServiceConnection)dc).ServiceUrl.AbsoluteUri.ToString();
Match m_src = regexObj.Match(cpath);
((WebServiceConnection)dc).ServiceUrl = new Uri(cpath.Replace(m_src.Groups[1].Value, this.ServerInfo.SharePointListUrl.AbsoluteUri.Replace("/FormServerTemplates/","")));
dc.Execute();
}
if (dc.Name.StartsWith("GetAllUserCollectionFromWeb"))
{
//http://clipsapps.cclulaba.net:81/_vti_bin/usergroup.asmx?wsdl
regexObj = new Regex("(.*)/_vti_bin/(.*)");
string cpath = ((WebServiceConnection)dc).ServiceUrl.AbsoluteUri.ToString();
Match m_src = regexObj.Match(cpath);
((WebServiceConnection)dc).ServiceUrl = new Uri(cpath.Replace(m_src.Groups[1].Value,this.ServerInfo.SharePointServerRootUrl.AbsoluteUri.ToString() ));
dc.Execute();
}
}
if (dc is FileSubmitConnection) {
regexObj = new Regex("(.*)/sites/(.*)");
string cpath = ((FileSubmitConnection)dc).FolderUrl;
Match m_src = regexObj.Match(cpath);
Match m_target = regexObj.Match(basepath);
((FileSubmitConnection)dc).FolderUrl = cpath.Replace(m_src.Groups[1].Value, m_target.Groups[1].Value);
}
}
}
|
No comments:
Post a Comment