I'm new to both InfoPath and C#. I've used the following URL as a primer to
get going:
http://msdn.microsoft.com/office/default.aspx?pull=/library/en-us/odc_ip2003_tr/
html/odc_INF_Lab_15.asp
What I'm trying to do is retrieve the current username which in my
environment would look like "doejo" for the name "John Doe". It's the
lastname plus the first two characters of the users firstname. I then need to
match that username in Active directory by searching the field
"SAMAccountName" which would match the current username (doejo).
The code comppiles correctly but when the InfoPath form is published nothing
happens at startup.
What am I missing?
using System;
using Microsoft.Office.Interop.InfoPath.SemiTrust;
using System.DirectoryServices;
[assembly: System.ComponentModel.DescriptionAttribute("InfoPathStartupClass,
Version=1.0, Class=getADinfo.getADinfo")]
namespace getADinfo
{
[InfoPathNamespace("xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-06-03T14-22-22\"
xmlns:xsf=\"http://schemas.microsoft.com/office/infopath/2003/solutionDefinition\"
xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xdUtil=\"http://schemas.microsoft.com/office/infopath/2003/xslt/Util\"
xmlns:xdXDocument=\"http://schemas.microsoft.com/office/infopath/2003/xslt/xDocument\"
xmlns:xdMath=\"http://schemas.microsoft.com/office/infopath/2003/xslt/Math\"
xmlns:xdDate=\"http://schemas.microsoft.com/office/infopath/2003/xslt/Date\"
xmlns:xd=\"http://schemas.microsoft.com/office/infopath/2003\"")]
public class getADinfo
{
private XDocument thisXDocument;
private Application thisApplication;
public void _Startup(Application app, XDocument doc)
{
thisXDocument = doc;
thisApplication = app;
}
[InfoPathEventHandler(EventType=InfoPathEventType.OnLoad)]
public void OnLoad(DocReturnEvent e)
{
//Test onLoad
thisXDocument.UI.Alert("Hello, World");
//Vars
string strUserName = System.Environment.UserName;
IXMLDOMNode nodeCurrentUserName =
thisXDocument.DOM.selectSingleNode("my:myFields/my:currentUserName");
IXMLDOMNode nodeCurrentUserDisplayName =
thisXDocument.DOM.selectSingleNode("my:myFields/my:currentUserDisplayName");
//Our System.Environment.UserName equals the "SAMAccountName" Active
Directory entry, search for it
DirectoryEntry entry = new
DirectoryEntry("LDAP://ou=people,ou=OMAF,dc=lrc,dc=ad,dc=gov,dc=on,dc=ca");
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + strUserName + ")";
search.PropertiesToLoad.Add("displayName");
SearchResult result = search.FindOne();
//Display results
nodeCurrentUserName.text = strUserName;
nodeCurrentUserDisplayName.text =
result.Properties["displayname"][0].ToString();
}
public void _Shutdown()
{
}
}
}
pierrick-allusse - 07 Jun 2005 00:30 GMT
What' the value of your "strUserName" ? I think you have a null string.
I've got the same problem when i launch my form by Sharepoint. I always
return a null string.I don't understand why.
> using System;
> using Microsoft.Office.Interop.InfoPath.SemiTrust;
[quoted text clipped - 59 lines]
> }
> }
Henning Krause [MVP] - 07 Jun 2005 21:14 GMT
Hello,
instead of Environment.Username, try
System.Security.Principal.WindowsIdentity.GetCurrent().Name
Greetings,
Henning Krause [MVP - Exchange]
==========================
Visit my website: http://www.infinitec.de
Try my free Exchange Explorer: Mistaya
(http://www.infinitec.de/software/mistaya.aspx)
> What' the value of your "strUserName" ? I think you have a null string.
> I've got the same problem when i launch my form by Sharepoint. I always
[quoted text clipped - 63 lines]
>> }
>> }