Wednesday 15 October 2008

SharePoint Anonymous Infopath Forms - EASY!

It seems crazy that SharePoint 2007 combined with InfoPath 2007 can't easily work anonymously straight out of the box. There are many cases when you may need to get an InfoPath form to allow a anonymous users fill in a form which is then submitted to your desired Document/Form Library. There seems to be many posts about this topic, all solutions seem to be verbose and quite lengthy, I cant see creating an intermediate web service between the InfoPath form is an acceptable work around, its yet another piece of code that has to be managed, documented and supported.

So here we have what I believe to be a better work around follow the below steps and all should make sense:

  • Allow Anonymous Access on a Central Administration Level. (Central Admin - Authentication Providers - Select Provider in Appriate Web Application - Tick "Enable Anonymous Access")
  • Now enable access to "Lists and Libraries" for anonymous access within the site collection. (Site Actions - Site Settings - Advanced Permissions - Setttings - Anonymous Access)
  • Now create an InfoPath form, the important part here is to get it to run custom code on submit. It will run a few clever lines of code that will place the InfoPath form in the Document/Form Library.
  • Once you have this event open in edit mode craft the below to fit in with your system.

public void FormEvents_Submit(object sender, SubmitEventArgs e)

{

// If the submit operation is successful, set

// e.CancelableArgs.Cancel = False

// Write your code here.

//-----------------------------------------------------------------'

//URL to place the file at, include Site URL, Doc Lib and File Name'

//-----------------------------------------------------------------'

string url = "http://sharepoint2007/TestForm/testing.xml";

//---------------'

//Create XPathNav'

//---------------'

XPath.XPathNavigator xPathNav = this.CreateNavigator();

//---------------------------------------------'

//Convert underlying InfoPath XML to byte array'

//---------------------------------------------'

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

byte[] Data = encoding.GetBytes(xPathNav.OuterXml);

try {

//--------------------------------------------------'

//Create webclient, set credentials and upload data.'

//--------------------------------------------------'

WebClient webclient = new WebClient();

webclient.UseDefaultCredentials = new NetworkCredential("USER", "PASS", "DOMAIN");

webclient.UploadData(url, "PUT", Data);

}

catch (Exception ex) {

}

}


Once this has been done, publish the form, follow the steps upload to sharepoint etc. I think the above code quite clearly shows just how easy submitting forms anonymously in SharePoint can be made, a small piece of managed code in an InfoPath Form and happy days.