October 24, 2016
Hot Topics:

Build a Custom SharePoint Web Service for Your InfoPath 2003 Documents

  • March 24, 2005
  • By Jeffrey Juday
  • Send Email »
  • More Articles »

If you see multiple w3wp.exe processes, select the process with the .NET type running the vti_bin virtual directory. Some experimentation may be necessary to find the correct process.

Once you've built the Web service and made the Web service available from within SharePoint, you can integrate with InfoPath 2003 Service Pack 1. After you've created an new InfoPath form, select Submitting Forms on the InfoPath Tools menu. The dialog in Figure 2 will appear.

Figure 2: Result of Select Submitting Forms on the InfoPath Tools Menu

Select the Web service option from the Submit to menu and then press the Add button to configure the Web service options. Follow the Data Connection wizard entering the URL for the Web service and selecting the method you wish to invoke. When prompted for Web service parameters, select the options to send the entire XML document as shown in Figure 3.

Click here for a larger image.

Figure 3: Select the Options to Send the Entire XML Document

Process Incoming Data

At this point, you've built everything you'll need to discover the Web service and transmit the XML document from InfoPath 2003. The remaining tasks are performing some processing on the incoming XML and writing the data to SharePoint.

First, you must remove the root node from the incoming document. Processing information (PI) in the InfoPath document exists outside of the root of the InfoPath document. The underlying Web service technology creates a root outside of the InfoPath document PI. Before saving the incoming XML document, you must remove the root node outside of the PI. The following code illustrates how to remove the root information and write the XML data to a MemoryStream object:

strm = new MemoryStream();

root = xmldoc.FirstChild;

foreach( XmlNode child in root.ChildNodes )
   xmldata = encode.GetBytes(child.OuterXml);


Once you have the data in a Stream object, you can extract the bytes and write the file to the SharePoint site. SharePoint offers two APIs for manipulating data: a Web service and a .NET assembly. The .NET assembly is the more complete API. We elected to use the Microsoft.SharePoint.dll assembly API in case we decided to extend our solution. Microsoft.SharePoint.dll includes functions for reading, writing, and versioning files in SharePoint. In the example code, all of the code to read and write to SharePoint is embedded in the SharePointSiteMediator class.

Three critical classes navigate and manipulate files in SharePoint. SPSite provides the navigation in SharePoint. SPWeb provides methods for accessing objects in a given site. SPFolder allows you to manipulate documents in a document library or list. In the example, SPSite, SPWeb, and SPFolder are declared as follows:

private SPSite _siteHome = null;
private SPWeb _subsite = null;
private SPFolder _folder;

The WriteFile function below utilizes the Stream you created to add or update the XML document in SharePoint:

public void WriteFile (string fileName,MemoryStream strm )
   byte[] fileContent = null;
   SPFile file;

   fileContent = strm.GetBuffer();

   if ( FileExists(fileName ) )
      file = _folder.Files[fileName];

      file = _folder.Files.Add(fileName, fileContent,true);


To determine whether the document already exists, you must use the SPQuery object and a pseudo SQL query. FileExists illustrates querying SharePoint for the existence of a document. The following is an excerpt from FileExists:

query = new SPQuery();

docLib = (SPDocumentLibrary)_subsite.Lists[_folder.

fileExistsQry = _fileExists.Replace("{filename}", fileName);

query.Query = fileExistsQry;

items = docLib.GetItems(query);

With the Web service writing to SharePoint complete, you can extend the code to perform other duties, including:

  • Perform validation on the formatting of the XmlDocument
  • Perform an additional security check
  • Initiate a workflow process in a workflow tool such as BizTalk 2004 Human Workflow or K2
  • Place the XML Document in multiple libraries

Now You Can Perform Other Actions

Building a SharePoint Web service for your InfoPath 2003 documents adds a layer of software to your application, allowing you to reliably perform other actions whenever a user saves an InfoPath document. Among the other actions are initiating workflow processes and performing some additional security validation.

Download the Code

To download the accompanying source code for this article, click here.

About the Author

Jeffrey Juday is a software developer with Crowe Chizek in South Bend, Indiana. He has been developing software with Microsoft tools for more than 12 years in a variety of industries. Jeff currently builds solutions using BizTalk 2004, ASP.NET, Sharepoint, and SQL Server 2000. You can reach Jeff at jjuday@crowechizek.com.

Tags: podcast

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel