Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

Using Relative URLs with the ServerInfo Class in InfoPath 2010

Using Relative URLs with the ServerInfo Class in InfoPath 2010

  • Comments 4

Hi, my name is Jill Anderson and I am a tester on the InfoPath team. In this post, I would like to introduce you to the one of the newest members of the InfoPath Object Model, the "ServerInfo" class.  For InfoPath forms published to a SharePoint server, the members of this class give you context about that particular site. Documentation about this new class can be found at: http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.serverinfo.aspx.

In this short video demo, I show you how you can use the members of this class to enable your form to submit data using a relative URL.  This enables you to package a Site Collection as a WSP and move it to a new server.  With these changes, the submit data connection moves to the new server as well.

Initial Setup:

To make sure that my form template was correctly associated with my SharePoint Form Library both before and after the Site Collection Migration, I published it as a Content Type (http://office.microsoft.com/en-us/infopath-help/create-a-sharepoint-library-or-site-content-type-for-your-form-template-HA010103005.aspx ). The ability to publish a Sandboxed Solution as a Content Type is a new feature for Office 2010.

Afterwards, I associated this content type with the correct SharePoint Form Library on the SharePoint Server. The steps to do this are as follows: 

  1. Create a new "Form Library"
  2. Open the newly created "Form Library"
  3. Open the "Library Settings" located under the "Library" Tab on the Ribbon
  4. Open "Advanced Settings" and set "Allow management of content types?" to "yes".
  5. Under "Content Types" select "Add from existing content types"
  6. Add the content type you published to the SharePoint server.

Sample Code:

This is the code I used to relativize the submit to the SharePoint site.

/// <summary>
        /// Submits the form to the SubmittedTimeCards form library on SharePoint and calls a function to update the Vacation 
        /// and Sick Balance for the employee.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">Provides data for the Microsoft.Office.InfoPath.FormEvents.Loading event.</param>
        
        public void FormEvents_Submit(object sender, SubmitEventArgs e)
        {
            FileSubmitConnection fileSubmit = (FileSubmitConnection)this.DataConnections["SharePoint Library Submit"];
            // Refreshing the Total number of hours and total pay for this given form.
            this.CalculateWorkWeekHours();
            this.CalculateTotalTypeHours();
            this.CalculateTotalPay();
            // Update the new Sick leave and Vacation Balance.
            this.DeductVacationAndSickPay();
            // Relative the SharePoint Submit location to current SharePoint site.
            fileSubmit.FolderUrl = this.ServerInfo.SharePointSiteUrl.ToString() + "SubmittedTimeCards/";
            fileSubmit.Execute();
            // If the submit operation is successful, set
            e.CancelableArgs.Cancel = false;
        }

Additional Links

For more information about creating and publishing Sandboxed Solutions see Phil Newman's post on
Introduction to Sandboxed Solutions - Sort data in repeating tables using managed code
Jill

Leave a Comment
  • Please add 8 and 1 and type the answer here:
  • Post
  • ServerInfo = Pure Gold. Thanks for the heads up.

  • Great post.

    Is there any way to do this with data connections? What is the solution there?

  • After trying to implement this in a test form, when publishing the form I get the following error message:

    The form template has been published to the server but it can only be opened in InfoPath Filler.

    There is an error in the InternalStartup method of form code.

    There may be additional error details in the Design Checker task pane.

    At the ULS logs,I also see the following error:

    Solution Deployment : Missing one or more of the following attributes from the root node in solution InfoPath Form_ER2010b.xsn_eAgwASyS_1.0.0.44.wsp: assembly '', type ''.

    Any ideas on root cause / workarounds?

  • Hi.

    I've set up a Content type with InfoPath Document Information panel. When adding this content type to document libraries, serverinfo will return sharepointsiteurl from sitecollection where content type is stored - not the sharepointsiteurl from where the content type is used.

    Any suggestions?

Page 1 of 1 (4 items)