Wednesday, November 24, 2010

Don't convert to connection file in InfoPath 2010 if you are developer

Thanks for the new feature "this.ServerInfo.SharePointSiteUrl" in InfoPath 2010, we don't need to convert data connections into "Connection Files" any more.

The benefit is:

1. Don't need to create and maintain data connection file library for SharePoint site;
2. Don't need to fix the URL and list GUID in the data connection files;

In simple environment, these advantages may be not enough to convince you to move, but, if you have separate development server, test server, production server and disaster recovery server, and if the forms need to be published to several site collections, then we definitely want to let the InfoPath form to pick up the correct sever and path automatically.

(This is for developer only.  Cannot execute data connection from Rules based on relative path.)

It is easy:

        public Uri getCurrentUrl(string strOriginal)
        {
            return getCurrentUrl(new Uri(strOriginal));
        }

        public Uri getCurrentUrl(Uri uriOriginal)
        {
            if (Application.Environment.IsBrowser == false)
                return uriOriginal;

            Uri uriSharePointSite = this.ServerInfo.SharePointServerRootUrl;
            string strNewUrl = uriSharePointSite.AbsoluteUri;
            if (strNewUrl.EndsWith(@"/"))
            {
                strNewUrl = strNewUrl.Substring(0, strNewUrl.Length - 1);
            }
            strNewUrl += uriOriginal.AbsolutePath;
            Uri uriNew = new Uri(strNewUrl);

            return uriNew;
        }

        public void FormEvents_Submit(object sender, SubmitEventArgs e)
        {
......

                WebServiceConnection dcGetUserProfileByName = (WebServiceConnection)this.DataConnections[@"GetUserProfileByName"];
                if (Application.Environment.IsBrowser)
                {
                    Uri uriService = dcGetUserProfileByName.ServiceUrl;
                    dcGetUserProfileByName.ServiceUrl = getCurrentUrl(uriService);
                }
                dcGetUserProfileByName.Execute();


                FileSubmitConnection connMainSubmit = (FileSubmitConnection)this.DataConnections[@"MainSubmit"]; //"Submit To Library"
                            if (Application.Environment.IsBrowser)
                {
                    connMainSubmit.FolderUrl = getCurrentUrl(connMainSubmit.FolderUrl).ToString();

                }
                connMainSubmit.Execute();
......

        }


Reference links:


Update (15/03/2011):

The data connection type list:
    Microsoft.Office.InfoPath.AdoQueryConnection
    Microsoft.Office.InfoPath.AdoSubmitConnection
    Microsoft.Office.InfoPath.BdcQueryConnection
    Microsoft.Office.InfoPath.BdcSubmitConnection
    Microsoft.Office.InfoPath.EmailSubmitConnection
    Microsoft.Office.InfoPath.FileQueryConnection
    Microsoft.Office.InfoPath.FileSubmitConnection
    Microsoft.Office.InfoPath.SharepointListQueryConnection (depreciated)
    Microsoft.Office.InfoPath.SharePointListRWQueryConnection
    Microsoft.Office.InfoPath.SharePointListRWSubmitConnection
    Microsoft.Office.InfoPath.SubmitToHostConnection
    Microsoft.Office.InfoPath.WebServiceConnection

Reference link:
http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.dataconnection.aspx

Bug fixed (15/03/2011):

Use "SharePointServerRootUrl" to replace "SharePointSiteUrl". Or else the Url is wrong if the site is under "/sites/sitecollection1";

No comments:

Post a Comment