Getting Current User in InfoPath 2007 via C#


Folks,


Got a little code sample from a fellow watcher of Pashman's Goldmine (thanks Huseyin) on how to retreive the username of the current user via C# code.  Since all code samples are useful, I've posted it below:


public void FormEvents_Loading(object sender, LoadingEventArgs e)


{


string xpath = "/my:oneriForm/my:nameTxt";


string userName1;


XPathNavigator field1 = MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);


//string oldValue = field1.Value; // Read


string strUserName = System.Environment.UserName;


//userName1 = System.Environment.


field1.SetValue(strUserName);


// Write


In this case the user wanted the full user name too, and I advised him to try and replace the System.Environment.UserName with System.Environment.FriendlyName.   I'll let you know if that worked.

Comments (2)

  1. Bob_Chauvin says:

    System.Environment.UserName requires Full trust, or more precisely a

    permission that only Full trust forms would get.

    Note that System.Environment.UserName will not return the real WSS user

    name, which I assume your code actually needs. Instead, we recommend using

    the InfoPath OM to get the user name.

  2. jackinthegreen says:

    Pashman,

    I know and used infopath 2003 c# code toolkit with vs.net, now I see that this is the case with infopath 2007 and VS2005. Unfortunately I have ot use infopath2003 for now, but I am also using VS 2005. Is their support for creating infopath2003 c# apps in VS 2005 or am I screwed till my company ugrades to office 2007?  I tried to open the infopath 2003 toolkit for vs.net in vs 2005 hoping it would convert, but it threw an error instead saying it required vs.net.

    Thanks for any help on this!!

Skip to main content