InfoPath 2003 SP1 and Active Directory
I figured I'd be the only one in the developer division not blogging about Beta 1!
When I was on the InfoPath team, I wrote a hands on lab (Lab 15)which talked about using managed code with InfoPath. Excercise 3 of the lab talked about using managed code to access information in Active Directory using System.DirectoryServices.
While this seemed like a great idea at the time, I've since realized that there's a better approach to the problem.There's really only one big problem, and that's that System.DirectoryServices requires FullTrust permissions in order to run. Since InfoPath forms by default do not have FullTrust permissions granted, you'll have to jump through all sorts of hoops in order to run the form (that's a blog for another time).
A better approach is to have a machine with a Web Service on it which exposes the information from Active Directory that you need. This way, you don't need to make your form fully trusted, making it easier to deploy. In addition, other forms can hit your webservice, so you can reuse your code. Also, by using a web service, you don't even need InfoPath SP1! You can use data adapters to connect to the web service, and script to populate the fields you want populated.
Here's the code for the web service, which is almost verbatim taken from the lab.
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Web;using System.Web.Services;using System.DirectoryServices;
namespace WhoAmI
{
/// <summary>
/// Summary description for Service1.
/// </summary>
[WebService(Namespace="<davfries.foobar/whoami/>")]
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
...
[WebMethod]
public string GetName()
{
return System.Environment.UserName;
}
[WebMethod]
public string GetDomain()
{
return System.Environment.UserDomainName;
}
[WebMethod]
public string GetManager()
{
// get current user name
string strUserName = System.Environment.UserName;
// search for user whose mailNickname is the logon name
DirectorySearcher ds = new DirectorySearcher("(mailNickname=" + strUserName + ")");
SearchResult result = ds.FindOne();
if(result == null)
{
return "Error finding user " + strUserName + " using Active Directory.";
}
DirectoryEntry employeeEntry = result.GetDirectoryEntry();
// find manager's LDAP DN from the DirectoryEntry
string strManagerDN = employeeEntry.Properties["manager"].Value.ToString();
// use the DN to create a new DirectoryEntry
DirectoryEntry managerEntry = new DirectoryEntry("LDAP://" + strManagerDN);
// get the alias of the manager
string strManagerAlias = managerEntry.Properties["mailNickname"].Value.ToString();
return strManagerAlias;
}
}
}
Then, in InfoPath business logic, you would have (something like) the following code:
IXMLDOMNode nodeManager =
thisXDocument.DOM.selectSingleNode("my:SalesReport/my:manager");
nodeManager.text = XDocument.GetDOM("GetManager").selectNodes("/dfs:myFields/dfs:dataFields/d:GetManagerResponse/d:GetManagerResult").text;
I hope this is helpful...
Comments
Anonymous
June 29, 2004
Also, if you have SharePoint Portal Server in your environment, you can use a low code solution to get the user's information by using the GetUserProfileByName web service: http://msdn.microsoft.com/library/en-us/spptsdk/html/mUserProfileServiceGetUserProfileByName.aspAnonymous
June 29, 2004
Even better!Anonymous
July 07, 2004
This is great, thanks! We get a lot of questions on this.Anonymous
October 09, 2006
When building a workflow solution using InfoPath, it is often necessary to enforce data validation forAnonymous
January 21, 2009
PingBack from http://www.keyongtech.com/1238885-infopath-and-active-directoryAnonymous
May 29, 2009
PingBack from http://paidsurveyshub.info/story.php?title=david-fries-weblog-infopath-2003-sp1-and-active-directoryAnonymous
June 08, 2009
PingBack from http://quickdietsite.info/story.php?id=3885