Anonymously Submit InfoPath Form to SharePoint Library

Written by:

If you’ve landed on this post, then you are probably struggling to submit InfoPath browser-enabled forms to a SharePoint library anonymously. And you’ve already learned that SharePoint doesn’t allow anonymous users to add to document and form libraries. There are some good solutions out there, but I found most to be more complex than I needed.

I had one anonymous access site collection. I needed anonymous users to submit a browser enabled InfoPath form, and I needed the submission to trigger a SharePoint Designer workflow. You may or may not already know this, but SharePoint Designer workflows will not trigger on items added or updated by anonymous users. [Note: Prior to SP1, declarative workflows could run under System Account. If you have upgraded to SP1 then this is no longer possible. See comments below for possible solutions.]

My solution assumes you are already familiar with setting up anonymous access within MOSS 2007. If you are not, you can find information on this at http://technet.microsoft.com/en-us/library/cc263363.aspx and http://blogs.devhorizon.com/reza/?p=498. You will also need to deploy your InfoPath form as an administrator-approved form template. If you are not sure how to do this, check out http://technet.microsoft.com/en-us/library/cc262921.aspx and http://www.infopathdev.com/blogs/janice/archive/2009/03/03/administrator-approved-form-templates.aspx. You should be able to accomplish both of the above before you proceed with this solution.

Below I’ll show you how to setup a SharePoint library to allow anonymous users to open your form template, and how to create a custom button to submit the form under elevated privileges.

Enable Anonymous Access on Form Library (MOSS 2007)

Go to Form Library Settings. Under Permissions and Management, click Permissions for this form library.

af1jt20090602

Under Actions, choose Edit Permissions. When the Settings button appears, choose Anonymous Access.

You will notice that on the Change Anonymous Access Settings page only View Items is selectable. Add, Edit, and Delete Items check boxes are grayed out. As mentioned earlier, by default SharePoint only allows anonymous users to view items in document and form libraries. Without checking Add Items, anonymous users will not be able to open your browser-enabled form. To workaround this issue, look at the end of the URL for this page.

Change DOCLIB to LIST and hit Enter. The page will now allow you to select Add Items for anonymous users.

Select Add Items and hit OK. While anonymous users can now open your form, when they attempt to submit the form they will receive an error. This workaround to allow anonymous access to the form template will not override SharePoint’s default policies regarding anonymous users and libraries.

Preparing Your Administrator-Approved Form Template to Use Custom Code (InfoPath 2007)

Open your form template in InfoPath Design mode. Under Tools, choose Form Options. In the Form Options window, choose Category: Security and Trust. You will need to set the Security Level of your form template to Full Trust.

Next choose Category: Programming. Under Programming Language, select C# and hit OK. My solution uses C#. If you prefer to use VB or another language, there are some good conversion sites on the web. You can try the converter at http://www.developerfusion.com/tools/convert/csharp-to-vb/. If you are already using custom VB code in your form template, then converting the C# to VB will be a must.

Adding Custom Code to Your Administrator-Approved Form Template (InfoPath 2007, Visual Studio 2005 Tools for Applications)

You should have already setup your form template to submit to a document library under Tools: Submit Options. If you have not, you can find more info on this at http://office.microsoft.com/en-us/infopath/HA101070571033.aspx.

Under Insert: More Controls, add a Button control to your form template. Double click your new button control, enter a unique Label and ID, and click Edit Form Code.

This will open your custom code in a Visual Studio environment. If you are not familiar working within Visual Studio, there are numerous primers on the web. However, this solution is straight forward and simple so you should be fine.

If you are developing your form template on a client operating system and not on a SharePoint development server, you will need to add a reference to Microsoft.SharePoint.dll. You can copy this dll from your SharePoint server at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\. Copy the dll into your InfoPath template projects folders which will probably be located under \Documents\InfoPath Projects\.

After adding the dll reference, you will add the following code within your button’s click event.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
this.Submit();
});

The final code should look something like this.

Save All and Build your project. Back in InfoPath, save your template and Publish the administrator-approved form template.

Once the new template is published and activated to your SharePoint site collection, you should be able to open and submit the form anonymously. The form will actually submit under the System account credentials, and as a bonus will successfully trigger SharePoint Designer workflows. [Note: As of SP1, Microsoft deprecated the ability for declarative workflows to run under System Account. See comments below for possible solutions.] You will probably want to adjust some Form Options on your template to ensure that the default Submit button does not show anywhere in the user’s browser. You only want them submitting with your custom button.

Posted on June 4th, 2009 in Technology, Tips & Tricks.