SharePoint: Using the SharePoint Object Model with InfoPath Form Code Behind Part 2 of 2


In part 1 we created an InfoPath form which used the SharePoint Object Model to create a new Task list item in a SharePoint site.

In this post we’ll publish that form to SharePoint and hook it into the user interface using a content editor web part.

To publish the form, click “Publish Form Template…” on the Design Tasks pane;

Designer task pane
Designer task pane
Publish the form
Publish the form

Select publish “To a network location” and click OK.

Enter publish file location and name
Enter publish file location and name

Enter a  file path/name for the form to be published to, and enter the published form name. The file/path name you enter must be accessible from the SharePoint web front end at which we’ll upload the published form.

Click OK.

Enter alternate form location
Enter alternate form location

Important: You must clear the alternate form location field here.

Click OK, check the details on the next screen then click Publish to complete the task and a confirmation dialog will display.

Publish confirmation dialog
Publish confirmation dialog

Having published the form to a network location we now need to upload the form to SharePoint, to do this we’ll need to go to Central Administration.

Forms with code-behind and/or which require Full Trust are administrator approved forms and must be uploaded to Central Admin by the farm administrator.

In Central Administration go to Application Management and in the InfoPath Forms Services section click “Manage form templates

Manage form templates
Manage form templates

Now choose “Upload form template

Upload form template
Upload form template
Select the form to upload
Select the form to upload

Click the browse button and select the published form template (.xsn) file you published previoulsy. Make sure you choose the published form (.xsn) and not the designer (.xsn) form.

If you haven’t previously validated the form you can click Verify here to verify the form is compatible form your SharePoint deployment.

Click the Upload button to upload your form. Once upload is complete you need to activate the form against a site collection, from the Manage Form Templates screen, select Activate to Site Collection from the forms drop-down menu.

Activate to a site collection
Activate to a site collection

Having activated the form to a site collection, we’ll move on to calling the form from a site by using a link embedded on a page using the content editor web part.

It’s important to understand that having activated the form to a site collection, a copy of the form now exists in a special folder in the site collection root web called FormServerTemplates, you can see it in SharePoint Designer, or if you browse to the folder in SharePoint.

Lets begin constructing the link URL. Browser enabled forms are displayed by SharePoint using the FormServer.aspx page which is part of InfoPath Forms Services for SharePoint.

The FormServer.aspx page accepts a number of query string parameters, see the MSDN article How to: Use Query Parameters to Invoke Browser-Enabled InfoPath Forms for more information. Of these query string parameters, we are interested in 4;

  1. XsnLocation – This parameters is used to specify the location of the forms .XSN file
  2. SaveLocation – By default this specifies the location to which the submitted form data is saved, in our case we are using this parameter to specify the target site which contain sthe Tasks list we will write to.
  3. Source – The standard parameter used to specify the location to return to once the submit action completes.
  4. DefaultItemOpen – See the MSDN article for more information, basically setting this parameter to 1 forces the form to be opened in the browser, this value corresponds to the SPList.DefaultItemOpen property.

Using this information our constructed HTML link tag looks like this (line breaks added for clarity);

<a href="_layouts/FormServer.aspx?XsnLocation=http://portal/FormServerTemplates/TestObjectModelForm.xsn
			&SaveLocation=http://portal/
			&Source=http://portal
			&DefaultItemOpen=1">Create a new Task.</a>

Paste this code into a content editor web part on a page in your site

The content editor web part form link
The content editor web part form link

Click the link and your form should open, enter the new task title and click Submit

Creating the new Task list item via the form
Creating the new Task list item via the form

Checking the Tasks list confirms the new task list item.

The new task list item
The new task list item

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

3 thoughts on “SharePoint: Using the SharePoint Object Model with InfoPath Form Code Behind Part 2 of 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s