Skip Ribbon Commands
Skip to main content

Ben There, Done That

:

SharePoint Ben > Ben There, Done That > Posts > InfoPath and User AD Information
March 14
InfoPath and User AD Information
Have you ever wanted to automatically fill in fields in your InfoPath form with user information from AD?  I had to do this recently for an InfoPath form.  The easiest way I found to do this was to use the SharePoint web service.  The only limitations iare that you need to have your SharePoint and AD User synchronization set up and the AD info you need must be syncronized...not all AD fields are linked in the syncronization by default.  This also means this will only work with MOSS (WSS doesn't have AD syncronization as it is part of Shared Services). To pull this info, take the following steps: 1. Add the web service: http://[SharePointURL]/_vti_bin/UserProfileService.asmx 2. To get the current user: using System.DirectoryServices; string CurrentUser = System.Environment.UserName; This will get the current SharePoint user, not the user currently logged into the computer. 3. Declare the InfoPath fields so you can write to them later: XPathNavigator fieldAVPName = MyNavigator.SelectSingleNode("/my:myFields/my:Name", NamespaceManager); 4. Get the properties of the user sharepoint.UserProfileService MyUsers = new sharepoint.UserProfileService(); MyUsers.Credentials = System.Net.CredentialCache.DefaultCredentials; sharepoint.PropertyData[] propdata; propdata = MyUsers.GetUserProfileByName(txtCurrentUser); int NumProperties = propdata.Length; string txtFirstName = null; string txtLastName = null; string txtName = null; for (int i = 0; i < NumProperties; i++) { if (propdata[i].Name == "FirstName" && propdata[i].Values.Length > 0) { txtFirstName = (string)propdata[i].Values[0].Value; } else if (propdata[i].Name == "LastName" && propdata[i].Values.Length > 0) { txtLastName = (string)propdata[i].Values[0].Value; } else { } } txtName = txtFirstName + " " + txtLastName; 5. Write the data to InfoPath fieldName.SetValue(txtName); You can use this with minor modifications to get other AD Properties of the users.
 

 Comments

 
 
 
 
 
 
 
 
 
 
 

Glad you liked it. Would you like to share?

Sharing this page …

Thanks! Close

Add New Comment

 

Showing 0 comments

    Trackback URL
     
    blog comments powered by Disqus
     

     Pre-Disqus Integration Comments

     

    Comments

    InfoPath and User AD Information

    Thank you thank you thank you, this was very useful because I wanted to programmatically call the UserProfileService from code.  You can also get this data from infopath without writing code, see this blog:

    http://blogs.microsoft.co.il/blogs/itaysk/archive/2007/04/05/InfoPath-_2D00_-Get-the-current-user-without-writing-code.aspx
    Benjamin.SteginkNo presence information on 3/18/2011 3:23 PM