InfoPath: How To Populate a List Control With SharePoint Users

Since the Service Pack 1 Preview of InfoPath it is really simple to fill for example a drop-down list box on a InfoPath form with data that is stored in a SharePoint list. But it’s not quite straight forward to fill that drop-down list box with all the users of a SharePoint site. Although this is quite easy to accomplish in a custom SharePoint list by creating a Lookup column and indicating that you want to get User Information. But (almost) nothing is impossible with InfoPath, so let’s give it a try!

First of all: how can we get a hold of a list with all the users of a specific SharePoint site? Well it happens to be that SharePoint exposes quite a lot of functionality through a web services layer; to be more specific: we can use the Users and Groups Service. In this service are several methods that you can use to retrieve all kinds of information about users and groups, we’ll use the GetUserCollectionFromSite method. So far so good, we know where to get our data, now let’s create the InfoPath form. The solution I’ll describe here will be using some custom code which can be written in script or in .NET code. This time, let’s make use of the InfoPath 2003 Toolkit for Visual Studio.NET so we can write .NET code.

Thus let’s fire up Visual Studio .NET and choose to create a new project based on the InfoPath Form Template. Drop a drop-down list box on your InfoPath form and open the Properties window. In the “List box entries” section choose to “Look up values in a data connection to a database, Web service, file or SharePoint library or list”. At this point the InfoPath form doesn’t have any data connections, hence click the Add button to create a new one. We want to get our data from the GetUserCollectionFromSite web method, so select the “Web service” option in the wizard. For the WSDL location use a URL formatted as follows: http://Server_Name/[Site_Name/]_vti_bin/UserGroup.asmx?wsdl (for example: http://myServer/SharePoint/_vti_bin/UserGroup.asmx?wsdl). Next the wizard will show you a list containing all the web methods available on the web service, select the GetUserCollectionFromSite method. Finally you can give the data connection a name, let’s call it “Users”. When you click the Select XPath button (right to the Entries text box), the troubles begin… As you can see on the screenshot, the web service return value does not contain a repeating group or field, which is necessary to be able to fill a list.

Why is that? Well if we take a look at the GetUserCollectionFromSite documentation we can see that the return value is of the System.Xml.XmlNode type. That means that you’ll receive XML of which the WSDL doesn’t describe how it will look like, so InfoPath can’t show your either. Although the documentation shows an example of how the XML will look like, it’s quite a bummer. The problem would be solved if you could type the XPath value in the Entries text box, but unfortunately this text box is read-only. From now on, there are several possible solutions:

  • Manually edit the XSLT of the InfoPath form and fill in the correct XPath value.
  • Create a wrapper web service that exposes the GetUserCollectionFromSite information strongly typed.
  • Use the little trick I’ll show you in a moment. :-)

The third solution will make use of a dummy data connection that has the same structure as the return value of the web method. This will give you the advantage of using InfoPath functionality to select the correct repeating group. Afterwards the contents of that dummy data connection are replaced by return value of the web method. So let’s work further on our InfoPath form but first download this XML file to your harddisk; it contains a fictitious return value of the GetUserCollectionFromSite web method. Click the Add (data connection) button to add a new one data connection: receive data from an XML document (point to the file you’ve just downloaded) and name it “DummyUsers”. Un-check the “Automatically retrieve data when the form is opened” check box and choose No if InfoPath suggest to file to your form. Then open the Properties window of the drop-down list box control again, select the “Look up values in a data connection to a database, Web service, file or SharePoint library or list” option, and choose to DummyUsers data connection. Then if you choose the “Select XPath” button you’ll see that you can navigate to a repeating group! Also in the Properties window, you need to specify the Value and “Display name” field, for example choose the LoginName field for both ones.

We’re not finished yet, we need to replace the data of the DummyUsers data connection with the data of the Users data connection (which is automatically fetched through the web service). We must do this in the OnLoad event of the InfoPath form, so in the Tools/Programming menu click the “On Load Event” menu item. Visual Studio.NET will pop up, and a the OnLoad method is created for you. You need only to add one line:

// The following function handler is created by Microsoft Office InfoPath. Do not
// modify the type or number of arguments.
[InfoPathEventHandler(EventType=InfoPathEventType.OnLoad)]
public void OnLoad(DocReturnEvent e)
{
            // Write your code here.
            thisXDocument.DataObjects["DummyUsers"].DOM.loadXML(
                        thisXDocument.DataObjects["Users"].DOM.xml);
}

That’s it! If you preview the InfoPath form, first the data of the Users data connection is fetched automatically. Then the OnLoad event is triggered, so we replace the data of the DummyUser data connection, and finally the form is showed and the drop-down list box contains all the login names of the users of the SharePoint site. You can use this little trick also for fetching other data through the SharePoint web services. If you want to easily create dummy XML documents you can use the WebServiceStudio 2.0 that can construct SOAP request messages.

27 Comments

  • It's great, Jan i've search this trick around the world :)

    Tanks

    mgi@cdhsrl.it, italy

  • very slick. thanks!

  • Is possible to change views during the OnLoad event?, thanks.

  • Daniel, can you elaborate a little bit more what you are trying to do?

  • dfsdafsdf

    asd

    fsd

    sddf

    s

  • Thanks for the tip! I was trying to figure out how to do this with Sharepoint 2 and it doesnt appear that I need the extra steps but it showed me how to get the list to the usernames URL.

  • Hey, when I try this I get:



    System.Runtime.InteropServices.COMException

    An item with the following index does not exist in the collection: Users

    at



    Any ideas?

  • Never mind, figured it out. I never renamed the first data source "Users"


  • HI



    its possible to open a popup window in infopath form and select value from that popup window to parent infopath form.



    Regards



    Vivek.T


  • HI



    its possible to open a popup window in infopath form and select value from that popup window to parent window.



    Regards



    Vivek.T


  • Thanks a lot. It made my day.

  • I was successful with this demo but I would like to use the GetUserInfo Method to retreive the current user opening the form and then present customized user specific information.
    When I preview the form with the data connection to this method autoloading however I get an error that the form tmeplate is invalid and an error message stating that the parameter loginName cannot be empty.
    Maybe I am using the wrong method or there are some other things that must be done.
    If anycome can give me come advice on how to get the current user of the form from sharepoint please let me know.
    cefox at energy-northwest.com

    Thank You.

  • It is really a very good example, Is it possible that we can fetch users from a perticular Sitegroup.

  • I'm desperate. I need to get current user name. Pls help

  • I need to be able to populate a dropdownlist programmatically.
    There are some articles out there that explains how, but i am unable to make it works. Please help?


    Thanks

    John

  • After promoting the infopath user property to Sharepoint site , Is it possible to filter a view using [Me] in WSS 3.0 or MOSS 2007?

  • I just like to say that your website is absoulutely brilliant!!! Do you think my sites too brilliant ;) ? Thanks!,I just like to say that your website is absoulutely brilliant!!! Do you think my sites too brilliant ;) ? Thanks!

  • I am also having a problem using a few of the more advanced web services. I am trying to pull only users with a certain level of access to the site and just like Clay Fox:

    "When I preview the form with the data connection to this method autoloading however I get an error that the form tmeplate is invalid and an error message stating that the parameter loginName cannot be empty."

    If anyone knows how to rectify this problem or how I can store a value to this parameter before the data is queried, please let me know.

  • Hi, I publised my infopath form in Sharepoint portal server.Whenever my infopath form opens i need current logged user name of sharepoint server.How to get current logged user of sharepoint portal server? Please help me.

    Thanks.

    Senthil Kumar D

  • Hello,
    Please help, I'd like to ask this: what format do I need to use so that the data in a field show's in a sharepoint list as an actual user?
    Just like the "Modified By" default column.
    I update the data as DOMAIN\user and it doesn't work.
    I update the data as user@domain.com and it doesn't work either.
    Do I have to define the XML elemente type as something special?
    Is there a special format to use? do I need to modify the XSL somehow?
    Thanks,

  • Lucy! Please call me,Lucy! Please call me

  • Try this in 2007

    this.DataSources["DummyUsers"].CreateNavigator().InnerXml = this.DataSources["Users"].CreateNavigator().InnerXml;

  • sales03@lp88.com\sales03@dots.com.cn

  • Okay, I got the code to work but my drop down list box is blank. Why?

  • Okay, I got the code to work but my drop down list box is blank on preview. What is wrong?

  • [URL=http://www.poscali.cn/castello-di] castello di [/URL] castello di [URL=http://www.poscali.cn/www-eva-henger-it] www eva henger it [/URL] www eva henger it [URL=http://www.poscali.cn/incontri-italia] incontri italia [/URL] incontri italia [URL=http://www.poscali.cn/trenitalia-it] trenitalia it [/URL] trenitalia it [URL=http://www.poscali.cn/infermiere-porche] infermiere porche [/URL] infermiere porche [URL=http://www.poscali.cn/plug-in-nero] plug in nero [/URL] plug in nero [URL=http://www.poscali.cn/kiss-the-girl-ashley-tisdale] kiss the girl ashley tisdale [/URL] kiss the girl ashley tisdale [URL=http://www.poscali.cn/traduzioni-on-line] traduzioni on line [/URL] traduzioni on line [URL=http://www.poscali.cn/giocare-online] giocare online [/URL] giocare online [URL=http://www.poscali.cn/roma-calzature] roma calzature [/URL] roma calzature [URL=http://www.poscali.cn/futurama-xxx] futurama xxx [/URL] futurama xxx [URL=http://www.poscali.cn/filmati-porno] filmati porno [/URL] filmati porno [URL=http://www.poscali.cn/nomi] nomi [/URL] nomi [URL=http://www.poscali.cn/google-fr] google fr [/URL] google fr [URL=http://www.poscali.cn/audio-divx] audio divx [/URL] audio divx [URL=http://www.poscali.cn/www-goole-com] www goole com [/URL] www goole com [URL=http://www.poscali.cn/geogle-earth] geogle earth [/URL] geogle earth [URL=http://www.poscali.cn/donne-che-cagano] donne che cagano [/URL] donne che cagano [URL=http://www.poscali.cn/ranma-1-2] ranma 1 2 [/URL] ranma 1 2 [URL=http://www.poscali.cn/video-pompino] video pompino [/URL] video pompino [URL=http://www.poscali.cn/alessandra-mastronardi-nuda] alessandra mastronardi nuda [/URL] alessandra mastronardi nuda [URL=http://www.poscali.cn/arcaton-com] arcaton com [/URL] arcaton com [URL=http://www.poscali.cn/wwww-google] wwww google [/URL] wwww google [URL=http://www.poscali.cn/www-giochi-gratis-it] www giochi gratis it [/URL] www giochi gratis it [URL=http://www.poscali.cn/www-mediaset-premium-it] www mediaset premium it [/URL] www mediaset premium it [URL=http://www.poscali.cn/www-cartoon-network-com] www cartoon network com [/URL] www cartoon network com [URL=http://www.poscali.cn/italiacercami-com] italiacercami com [/URL] italiacercami com [URL=http://www.poscali.cn/italia-chiamami-com] italia chiamami com [/URL] italia chiamami com [URL=http://www.poscali.cn/google-hearth] google hearth [/URL] google hearth [URL=http://www.poscali.cn/carta-geografica] carta geografica [/URL] carta geografica [URL=http://www.poscali.cn/pics-porno] pics porno [/URL] pics porno [URL=http://www.poscali.cn/playstation-gratis] playstation gratis [/URL] playstation gratis [URL=http://www.poscali.cn/italiacercami] italiacercami [/URL] italiacercami [URL=http://www.poscali.cn/video-porno-sesso-gratis-gay] video porno sesso gratis gay [/URL] video porno sesso gratis gay [URL=http://www.poscali.cn/escort-forum-it] escort forum it [/URL] escort forum it [URL=http://www.poscali.cn/simona-ventura-nuda] simona ventura nuda [/URL] simona ventura nuda [URL=http://www.poscali.cn/film-porno] film porno [/URL] film porno [URL=http://www.poscali.cn/valentino-rossi-shop] valentino rossi shop [/URL] valentino rossi shop [URL=http://www.poscali.cn/ranma-hentay] ranma hentay [/URL] ranma hentay [URL=http://www.poscali.cn/filmati-di-sesso] filmati di sesso [/URL] filmati di sesso [URL=http://www.poscali.cn/www-calendari-it] www calendari it [/URL] www calendari it [URL=http://www.poscali.cn/latino-online] latino online [/URL] latino online [URL=http://www.poscali.cn/ampland-it] ampland it [/URL] ampland it [URL=http://www.poscali.cn/player-wmv] player wmv [/URL] player wmv [URL=http://www.poscali.cn/tim-cellulari] tim cellulari [/URL] tim cellulari [URL=http://www.poscali.cn/racconti-cuckold] racconti cuckold [/URL] racconti cuckold [URL=http://www.poscali.cn/sex-chat] sex chat [/URL] sex chat [URL=http://www.poscali.cn/gioco-scaricare] gioco scaricare [/URL] gioco scaricare [URL=http://www.poscali.cn/sesso-donna-matura] sesso donna matura [/URL] sesso donna matura [URL=http://www.poscali.cn/gogle-it] gogle it [/URL] gogle it [URL=http://www.poscali.cn/www-lady-sonia-com] www lady sonia com [/URL] www lady sonia com [URL=http://www.poscali.cn/calciatori-nudi] calciatori nudi [/URL] calciatori nudi [URL=http://www.poscali.cn/ciaoamigos-it] ciaoamigos it [/URL] ciaoamigos it [URL=http://www.poscali.cn/teen-sex] teen sex [/URL] teen sex [URL=http://www.poscali.cn/sito-porno-gratis] sito porno gratis [/URL] sito porno gratis [URL=http://www.poscali.cn/iomilu-com] iomilu com [/URL] iomilu com [URL=http://www.poscali.cn/mondo-convenienza] mondo convenienza [/URL] mondo convenienza [URL=http://www.poscali.cn/www-film-pornogratis-donne-mature-it] www film pornogratis donne mature it [/URL] www film pornogratis donne mature it [URL=http://www.poscali.cn/test-concorso] test concorso [/URL] test concorso [URL=http://www.poscali.cn/naked-girls] naked girls [/URL] naked girls

  • Hey all. For those of you that are about to pull your hair out from the ‘Unexpected XML declaration…..’ and other similar errors, I came up with what seems to be an acceptable solution.
    In my situation I wanted to pass some contact selector xml from my workflow association form to my task form. Then I wanted it to be usable in a dropdown box for the user to select one of the predetermined people.

    I accomplished this by:
    - Building xml string the task form secondary data source is expecting
    - Get the InnerXml of the root node. This may technically be invalid xml because of more than one root node but this should not matter.
    - Then pass the xml string though the ExtendedProperties[“MyXmlTaskData”]
    - On form loading event run the following code

    DataSource fromWorkflow = this.DataSources["ItemMetadata"];
    DataSource toForm = this.DataSources["ContactSelectorSecondaryDataSource"];

    string str = fromWorkflow.CreateNavigator().SelectSingleNode("/z:row/@ows_MyXmlTaskData", this.NamespaceManager).Value;

    if (!string.IsNullOrEmpty(str))
    {
    XPathNavigator nav = toForm.CreateNavigator();
    nav.MoveToFollowing(XPathNodeType.Element);
    nav.InnerXml = str;
    }

    This seems to have done the trick for me. I hope it can help you!

    Best of luck!
    Adam

Comments have been disabled for this content.