Getting SharePoint Users In InfoPath 2003

Recently I have been doing some work with SharePoint and InfoPath 2003 and one of my tasks was to get a list of SharePoint users into a drop-down list box in an InfoPath form – sound simple right? Wrong! What should be an easy task is made more complicated due to that fact that the SharePoint web services which return user information do not provide complete definitions of their return types, so InfoPath does not know what it can do with the result.

User / Group information can be retrieved from SharePoint via the UserGroup web service located at http://yourserver/_vti_bin/UserGroup.asmx – the web method I am going to be using is GetUserCollectionFromSite, but of course you can use whichever method you require.

In order to be able to get the information and use it in InfoPath 2003 we are going to need to create a new InfoPath Form Template in Visual Studio 2005. Before we can do this we need to install the Office InfoPath 2003 Toolkit For Visual Studio 2005, which can be found in your MSDN subscription – or alternatively you can download the InfoPath 2003 Toolkit For Visual Studio .NET (2003) although I have not tested this. Once you have installed the toolkit and created a new project you will be presented with a wizard which will guide you through the processes of setting up the project and you can either base the project an existing InfoPath form or create a new one, either is OK.

The first thing to do now is to set up our reference to the SharePoint web service; In InfoPath go to Tools -> Data Connections and click Add. Select Receive data, on the next page select Web Service, and on the following page enter in the URI to your UserGroup web service (E.G. http://yourserver/_vti_bin/UserGroup.asmx). On the next page you should be able to select your desired web methods, in this case we want GetUserCollectionFromSite. You can now enter a name for the data connection, I am just going leave it as the default name “GetUserCollectionFromSite“. Underneath the textbox where you can enter in the name there is a checkbox option “Automatically retrieve data when form is opened“, ensure this is ticked (if you do not tick this you can retrieve the manually retrieve the data later by calling the Query() method on the data connection).

Now that we have a connection the SharePoint we need to create a data connection which we can easily use in SharePoint, which will allow us to bind default values or in this example populate a drop-down list box. To do this we are going to create an XML file containing the same format as the return type of the SharePoint web service. In notepad, or your preferred text editor, enter the following:


Save the file as SiteUsersAndGroups.xml. Back in InfoPath create a new receiving data connection, but this time instead selecting Web service select XML document. On the next page click Resource Files… and click Add… -> select theSiteUsersAndGroups.xml file we just saved earlier and click OK twice. On the next page, once again we are going to leave the data connection name as the default “SiteUsersAndGroups“.

To populate the drop-down list box all we have to do now is double-click on the drop-down list box we want to populate and under List box entreis select the option “Look up values in a data connection to a database, Web service, file or SharePoint library or list“. In the Data Connection drop-down select SiteUsersAndGroups. Next to the Entries textbox click the Select XPath button and select the User node. You can now select the attributes we want to display in the Value and Display name fields (the Display name attribute will be the text presented to the user – typically this should be @Name to display the users name). After saving the form we now need to switch back to Visual Studio in order to transfer the data from the web service data connection to the xml data connection.

In Visual Studio open FormCode.cs – by default there should be two methods and two properties: _Startup(…)_Shutdown()thisXDocument and thisApplication. Although we will not need to use the startup or shutdown methods we will need to use the thisXDocument property.

The first thing we need to do is capture the load event of the InfoPath form. In order to handle InfoPath events methods need to have the InfoPathEventHandler attribute specified. For example our OnLoad event handler is going to look like the following:


To test if the method is being executed a dialog prompt can be triggered by calling thisXDocument.UI.Alert(”Hello World”);. When the InfoPath form loads a standard dialog box should be displayed saying “Hello World“.

Now that we have an entry point for our code we need to read the data returned from the SharePoint web service, stored in the GetUserCollectionFromSite data connection, and copy it into our SiteUsersAndGroups data connection which we are binding to the drop-down list. As all of the data is returned in XML format, as is the case with all InfoPath data sources, we are going to need to use XPath to retrieve the data. The first step is to get an XPathNavigator we can use to perform out XPath query, in order to do this we first need to take the DataObject from thisXDocument and place it into an XmlDocument and from the XmlDocument create a navigator.


If you look back at the xml file we created earlier you will notice that the Users node is under the http://schemas.microsoft.com/sharepoint/soap/directory/ namespace. Along with this namespace InfoPath data connections are placed under thehttp://schemas.microsoft.com/office/infopath/2003/dataFormSolution namespace. In order to access these namespaces in our XPath query we need to create an XmlNamespaceManager.


Our actual XPath query, to get the nodes we are interested in, is going to look similar to the follow:

Now that we have the all of the users (and groups) returned from the SharePoint web service we simply need to load the xml in to the SiteUsersAndGroups data connection as follows:


That’s it! When the form is opened the drop-down list should not be populated with the users and groups from SharePoint. The final class should look similar to the following:


Note: Instead of writing managed code behind the InfoPath form you could write a proxy web service which contains a complete return type in the WSDL, which InfoPath can then use directly; However, note that the SharePoint web services require windows authentication so you will either need to set up a service account to access the web services, or more preferably set up Kerberos to allow authentication to be delegated to your proxy web service.